1️⃣

3.1 - Detecting Events

3.1 Detecting Events

Pygame events are things like keystrokes or mouse clicks. They are basically interactions that the user makes with our application. We can use them to make our game more interactive. For a list of event methods, click here.

Event Queue

In pygame, there is a certain method we use that will give us a queue of all the events that have occurred. By looping through this queue, we can process or ignore these events accordingly. Once an event is processed or ignored, it is removed from the queue. We can retrieve this queue by calling pygame.event.get().
for event in pygame.event.get(): if event.type == pygame.QUIT: print("quit") if event.type == pygame.KEYUP: print("up") # etc...
💡
Note: pygame.event.get() returns a list of all the events in the event queue and clears the event queue. Because it returns a list, we can use a for loop to iterate through each item, which we are aliasing as event, in the list of events.
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.