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

Re: [pygame] Last Frame of Animation/Game Loop Problem



If I understand you correctly, you are expecting the print() statements to execute immediately when they are encountered in the loop, and not all at once at the end of the program.

To do this, you need to call sys.stdout.flush() after each print() statement.



Here is your code with my suggestions added:

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)
        sys.stdout.flush()
        frame_count += 1
        if frame_count >= 5:
            done = True
        print(done)
        sys.stdout.flush()
        # set fps
        clock.tick(1)
        # Bring drawn changes to the front
        pygame.display.update()

    print("Out of loop")
    sys.stdout.flush()
    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()





On Tue, Jan 16, 2018 at 10:37 AM, dejohnso <dejohnso@xxxxxxxxxxx> wrote:
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()