pygame screen failing to dislpay
<p>I have the following code in Python 3 (and pygame), but the white surface fails to display and I don't understand why. Has it got something to do with where it has been placed? I tried de-indenting, but that didn't work either? The code is as below:</p>

<pre><code>import pygame
from pygame.locals import*
pygame.init()

screen=pygame.display.set_mode((800,600))


# Variable to keep our main loop running
running = True

# Our main loop!
while running:
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for KEYDOWN event; KEYDOWN is a constant defined in pygame.locals, which we imported earlier
        if event.type == KEYDOWN:
            # If the Esc key has been pressed set running to false to exit the main loop
            if event.key == K_ESCAPE:
                running = False
            # Check for QUIT event; if QUIT, set running to false
            elif event.type == QUIT:
                running = False

            # Create the surface and pass in a tuple with its length and width
            surf = pygame.Surface((50, 50))
            # Give the surface a color to differentiate it from the background
            surf.fill((255, 255, 255))
            rect = surf.get_rect()

            screen.blit(surf, (400, 300))
            pygame.display.flip()
</code></pre>

<p>So it does appear that your indentation is wrong.</p>

<p>You need to define the surface and update the screen etc. outside of the event loop.</p>

<p>At the very least you must move the <code>screen.blit(surf, (400, 300))</code> and <code>pygame.display.flip()</code> outside of the event loop.</p>

<p>This is it fixed:</p>

<pre><code># Our main loop!
while running:
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for KEYDOWN event; KEYDOWN is a constant defined in pygame.locals, which we imported earlier
        if event.type == KEYDOWN:
            # If the Esc key has been pressed set running to false to exit the main loop
            if event.key == K_ESCAPE:
                running = False
            # Check for QUIT event; if QUIT, set running to false
            elif event.type == QUIT:
                running = False

    # Create the surface and pass in a tuple with its length and width
    surf = pygame.Surface((50, 50))
    # Give the surface a color to differentiate it from the background
    surf.fill((255, 255, 255))
    rect = surf.get_rect()

    screen.blit(surf, (400, 300))
    pygame.display.flip()
</code></pre>

python pygame surface