pygame.time.wait() makes the window freez

大兔子大兔子 提交于 2020-01-15 06:23:31

问题


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 to the right, but if I reduce or move the window and make a try the window freezes on the waiting moment.

Here is the code:

import sys, pygame
from pygame.locals import *

pygame.init()

FPS       = 30
BGCOLOR   = (255, 255, 255)
RECTCOLOR = (10, 0, 199)
DS        = pygame.display.set_mode( (200,200) )
CLOCK     = pygame.time.Clock()
pygame.display.set_caption('Demo')

def main():
    DS.fill(BGCOLOR)
    pygame.draw.rect(DS, RECTCOLOR, (50, 50, 100, 100))
    pygame.display.update()

    while 1:
        ev = pygame.event.wait()
        if ev.type == QUIT:
            pygame.quit()
            sys.exit()
        elif ev.type == MOUSEBUTTONUP:
            animation()


def animation():
    pygame.event.set_blocked(MOUSEBUTTONUP)
    for x in range(10, 101, 10):
        pygame.draw.rect(DS, BGCOLOR, (150 - x,50,x,100))
        pygame.display.update()
        CLOCK.tick(FPS)
    pygame.time.wait(2000) # 2 seconds
    for x in range(10, 101, 10):
        pygame.draw.rect(DS, RECTCOLOR, (50,50,x,100))
        pygame.display.update()
        CLOCK.tick(FPS)
    pygame.event.set_allowed(MOUSEBUTTONUP)

if __name__ == '__main__':
    main()

Why we have this sort of behavior ?, maybe the system don't synchronize between the pause moment and the ticks in the second for loop

PS: i'm on windows 7 and sorry for my english it's not my native language


回答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.




回答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.




回答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.



来源:https://stackoverflow.com/questions/18697481/pygame-time-wait-makes-the-window-freez

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!