I am learning Python's pygame module so I can make games and I'm fairly new to it. I am watching a tutorial and in the following code, when the for loop is checking for a pygame.QUIT event the run variable is turned to false and pygame.quit() quits pygame.
My question is, why is the pygame.QUIT being checked when the 'X' button will close the window?
When the X button is pressed, an event object is created and added to the event queue. The event objects are retrieved with pygame.event.get()
. The type
property indicates the type of the event.
If you do not check event.type == pygame.QUIT
and do not set run = False
, the loop will never end (endless loop), the application will never terminate and the window will not close.
pygame.quit()
deinitializes all pygame modules, which also deinitializes the display module and causes the window to close. The window is also closed when the application exits after your script is finished.