Pygame needs “for event in pygame.event.get()” in order not to crash

Basically, the OS expects pygame to handle events during your program. If the OS notice that events aren’t handled, it’ll alert the user. The program doesn’t actually crash or freeze, the OS is just saying that your program has become unresponsive (which it has because you’re not responding to any user events), but it still works.

When your game is entering small scenes you might think that you don’t need to handle events, but there is one event you should always check for: the pygame.QUIT event (sent when the user press the close button at the top corner). In your example you’re not allowing the user to quit during the game over sequence (you’re providing the player a button to click but a user would also expect clicking the close button would close the game as well).

Another reason is that the event queue is constantly filling up. So if the user would spam multiple keys and press multiple areas with the mouse, nothing would happen until he/she enters the game again (where you have an event loop). Then every event would be executed. Therefore it’s important to regularly empty the queue. The queue is emptied every time you call pygame.event.get() or pygame.event.clear().

The function pygame.event.pump() is the function that put all events into the event queue (it doesn’t clear the previous events, it just adds). The event queue won’t be filled/updated with any events if the function isn’t called. However, the function is implicitly called inside the functions pygame.event.get(), pygame.event.clear(), pygame.event.poll(), pygame.event.wait() and pygame.event.peek(), so there is rarely a reason to call it explicitly. If you are sure you don’t want to handle events at some time, you could use pygame.event.clear() so the event queue is empty when you start processing events again. If you don’t want to handle events at all, then use pygame.event.pump().

Leave a Comment