Skip to content Skip to sidebar Skip to footer

Pygame.time.wait() Makes The Window Freez

I have a simple animation to handle where I draw a rectangle and when I click on the surface, the rectangle slid left and wait for 2 seconds and then redraw it with a slid motion t

Solution 1:

Two things:

When you run a loop like this:

for x in range(10, 101, 10):
        pygame.draw.rect(DS, BGCOLOR, (150 - x,50,x,100))
        pygame.display.update()

you draw your animation, but pygame has no opportunity to handle the messages it gets from your operation system. To avoid that, you should call pygame.event.pump() or pygame.event.get() inside the loop.

From the docs:

pygame.event.pump()

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. If you are not using other event functions in your game, you should call pygame.event.pump() to allow pygame to handle internal actions.

This function is not necessary if your program is consistently processing events on the queue through the other pygame.event functions.

There are important things that must be dealt with internally in the event queue. The main window may need to be repainted or respond to the system. If you fail to make a call to the event queue for too long, the system may decide your program has locked up.

Also, while moving your window around, your game will freeze. This is a known limitation of SDL (remember that pygame is just a thin wrapper around SDL) and caused by the windows message loop: when you move or resize a window, windows uses a modal loop, which will block the thread your game runs in.


Solution 2:

I recommend you to use only one loop and use variables or user events to keep track of game state.

Example:

game_state = 0

def main():
    global game_state
    rectangle_ticks = 0

    while 1:
        events = pygame.event.get()   # Get all pending events

        for ev in events:
            # Process events
            if ev.type == QUIT:
                pygame.quit()
                sys.exit()

            elif ev.type == MOUSEBUTTONUP:
                game_state = 1
                pygame.time.set_timer(pygame.time.set_timer(pygame.USEREVENT + 1, 1000)

            elif ev.type == pygame.USEREVENT + 1:
                pygame.time.set_timer(pygame.time.set_timer(pygame.USEREVENT + 1, 0)
                game_state = 2

        # Clear screen
        DS.fill(BGCOLOR)

        # Draw game state
        pygame.draw.rect(DS, RECTCOLOR, (50, 50, 100, 100))
        animation()

        # Update screen
        pygame.display.flip()

        CLOCK.tick(FPS)

def animation():
    if game_state == 1:
        for x in range(10, 101, 10):
            pygame.draw.rect(DS, BGCOLOR, (150 - x,50,x,100))

    if game_state == 2:
        for x in range(10, 101, 10):
            pygame.draw.rect(DS, RECTCOLOR, (50,50,x,100))

It may look weird to draw the whole screen in every tick, but it's the usual way to work in pygame in order to get rid of older objects you don't want to keep drawing.


Solution 3:

pygame.time.wait(1000) # 2 seconds

This causes code to pause here, for 2 whole seconds. So you will not get any events till after 2 seconds.

You need to use get_ticks() for animation or a timer.


Post a Comment for "Pygame.time.wait() Makes The Window Freez"