pygame events are queued | Don't make this mistake ================================================== pygame events are stored in a queue, by default the most suggested way shown in all tutorials "" the queue, which removes all the messages. Date: April 7, 2022 pygame events are stored in a queue, by default the most suggested way shown in all tutorials "`pumps`" the queue, which removes all the messages. ## start up pygame You don't necessarily need a full [boilerplate](https://waylonwalker.com/til/pygame-boilerplate-apr-2022/) to start looking at events, you just just need to `pygame.init()` and to capture any keystrokes you need a window to capture them on, so you will need a display running. ```python import pygame pygame.init() pygame.display.set_mode((854, 480)) ``` ## get some events Let's use pygames normal `event.get` method to get events. ```python events = pygame.event.get() ``` printing the events reveal this ```python [ , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] ``` ## Lets get some more events. Let's say that we have multpile sprites all asking for the events from different places in our game. If we assume that our game loop runs very fastand we get events one after another the second one will have no events. ```python events_one = pygame.event.get() events_two = pygame.event.get() ``` printing the events reveals that there are no events, well i ```python [] ``` ## Making things more maddening Even simple games don't quite run infinitely fast there is some delay, with this delay most events will go to event_one, while any that occur in the short time between event_one and two will be in event_two's queue. ```python import time events_one = pygame.event.get() time.sleep(.05) # simulating some delay that would naturally occur events_two = pygame.event.get() ``` ## How to Resolve this Store events for each frame in memory. ## Pump I thought `pump=False` would be the answer I was looking for, but I was proven wrong. It does not behave intuitivly to me. ```python events_one = pygame.event.get(pump=False) # all events since last pump events_two = pygame.event.get(pump=False) # no events events_three = pygame.event.get() # all events since last pump ``` `events_one` and `events_three` will have a list of events, while `events_two` will be empty. It seems that `pump=False` leaves the events there for the next `event.get()`, but appears cleared to any `event.get(pump=False)`. ## Keep a Game State If you want objects to do their own event handling, outside of the main game, you will need to give them some game state with the events in it. However you decide, you may only call `event.get()` once per game loop otherwise weird things will happen.