Home Ask Login Register

Developers Planet

Your answer is one click away!

Luis Ramirez February 2016

Pygame, Draw the same frame for a set amount of time

I want to display a message for two seconds.

The logic that im using right now is making the code wait using pygame.time.delay(2000) after pygame.display.flip.

A short example: (I use this flip-delay on my code a lot)

    write_gui("{0} has appeared".format(monster.name), BLUE, 24, TEXT_CORNER_X, TEXT_CORNER_Y)

This does work but it tends to "hang" the entire process, so when this happens I get some bugs because of this, mainly some frame loss because the program can keep up with the sleep-awake cycle.

So what I'm thinking right now is to draw the same frame for two seconds.

So what do you guys recommend I should do?

Because one of my answers was to put every flip on a while loop, so there has to be a better line-conservative approach to solve this.


Fredrik February 2016

your program "hangs" because you are not calling pygame.event.get() when you are sleeping, pygame.event.get()lets pygame handle its internal events.

The simplest way to solve is to use the return value from dt = clock.tick(), in this case dtwill be the time since your last call to clock.tick(), this value will be in milliseconds. You could then use that value to increment a counter on how long to show the message.

You could write a function like this if you wanted and call it with how long to wait:

def waitFor(waitTime): # waitTime in milliseconds
    screenCopy = screen.copy()
    waitCount = 0
    while waitCount < waitTime:
        dt = clock.tick(60) # 60 is your FPS here
        waitCount += dt
        pygame.event.pump() # Tells pygame to handle it's event, instead of pygame.event.get()
        screen.blit(screenCopy, (0,0))

This function will wait for the specified time and keep the screen as it was before the call.

Post Status

Asked in February 2016
Viewed 3,405 times
Voted 9
Answered 1 times


Leave an answer

Quote of the day: live life