[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Last Frame of Animation/Game Loop Problem



I have having trouble with a basic game/animation loop. 

It seems like the final frame doesn't show up until after the game loop is done and pygame.quit() is called. An example is below - if it is run, I see the "Out of loop" output, then a pause, then the final frame, then the final pause. 

Am I doing something wrong? Is this specific to my setup/machine? 

import pygame, sys

def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 400))

    frame_count = 0

    # The clock helps us manage the frames per second of the animation
    clock = pygame.time.Clock()

    square = pygame.Rect((0,100),(50,50))
    done = False
    while not done:
        # Erase the screen
        screen.fill((50, 50, 50))
        # Process events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        # The main snake code
        square.move_ip((50,0))
        pygame.draw.rect(screen, (200,50,50), square)
        print(square)
        frame_count += 1
        if frame_count >= 5:
            done = True
        print(done)
        # set fps
        clock.tick(1)
        # Bring drawn changes to the front
        pygame.display.update()

    print("Out of loop")
    pygame.time.delay(2000)
# This also works to bring up the last frame.    
#    pygame.event.clear()
#    pygame.event.wait()
    pygame.quit()
    print("After quit")
    pygame.time.delay(2000)
    sys.exit()

main()