Search code examples
pythonpygame

How does the for event in pygame.event.get() in python work?


My problem is that during the execution of this loop hundreds of events may be happening and for every event it is yielding an Event object...

So, how is the loop able to finish and the interpreter come out of the loop?


Solution

  • The events are queued. When a new event occurs, it is added to the end of the queue. When pygame.event.get is called, the events currently in the queue are returned and the queue is emptied. See pygame.event.get:

    This will get all the messages and remove them from the queue.

    New events that occur while the event loop is executed are added to the queue. You will get these events the next time pygame.event.get is called in the next frame.