Just like a GUI program our game will run in an endless loop. On every loop cycle we'll need to check for new events.
So, it's good practice to define a function to hold all your event handling code. Then it's a simple matter to call this function every loop cycle.
Here's a barebones example:
# loop through all events
for event in pygame.event.get():
if event.type == pygame.QUIT:
At any particular moment there may be many different events. We'll need to test them all, so there's a for loop to iterate through the full event list.
In this barebones example we only test for one event, namely the
QUIT event. If this has occurred the game is closed using the
sys.quit() function call.
A post from my Learn Python on the Raspberry Pi tutorial.