Why does pygame freeze at pygame.event.get() when

2019-06-28 05:59发布

问题:

Basically I have a loop (tick, set_caption, screen_fill, event.get(), send_frame_event, flip, repeat)

When I drag the window around on windows 7, the loop stops looping, I ended up stuck in pygame.event.get(), I have tried to define certain events only for get e.g. get([pygame.QUIT]) to no avail.

Simply calling pygame.event.clear() has the same freeze effect when dragging/moving the window.

Is there a workaround?

Not full code, but should be enough:

def start(self):
    self.running = True
    Clock = pygame.time.Clock()
    while self.running:
        self.p += 25
        tickFPS = Clock.tick(self.fps)
        pygame.display.set_caption("Press Esc to quit. FPS: %.2f" % (Clock.get_fps()))
        self.screen.fill([self.p&0xFF,(255-self.p)&0xFF,255])
        self.handleEvents()
        self.raiseEvent("updateFrame")
        pygame.display.flip()
def handleEvents(self):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            self.running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self.running = False

full code at: http://pastie.org/private/wm5vqq3f7xe0xlffy1fq

回答1:

Try placing a call to pygame.event.pump() inside your mainloop (or handleEvents function)



回答2:

No idea if this will help or not:

I realize the problem is with moving the window window, not with re-sizing the window. Perhaps there is some similarity between moving and re-sizing?

I found this in the documentation on re-sizing:

Then the display mode is set, several events are placed on the pygame event queue. pygame.QUIT is sent when the user has requested the program to shutdown. The window will receive pygame.ACTIVEEVENT events as the display gains and loses input focus. If the display is set with the pygame.RESIZABLE flag, pygame.VIDEORESIZE events will be sent when the user adjusts the window dimensions. Hardware displays that draw direct to the screen will get pygame.VIDEOEXPOSE events when portions of the window must be redrawn.

and:

Note that when the user resizes the game window, pygame does not automatically update its internal screen surface. You must call set_mode() every time VIDEORESIZE is sent. This really should be more clear in the documentation.

Perhaps when moving the game window, something similar happens?



标签: python pygame