Jun-25-2021, 09:47 AM
The loop is needed to get user input and to update the display.
The loop will exit if the following statement is true
The loop will exit if the following statement is true
if event.type == pygame.QUIT: running = False
https://pygame.readthedocs.io/en/latest/...event-loop Wrote:Show the event loop
The most essential part of any interactive application is the event loop. Reacting to events allows the user to interact with the application. Events are the things that can happen in a program, such as a
The following is an infinite loop which prints all events to the console:
- mouse click,
- mouse movement,
- keyboard press,
- joystick action.
while True: for event in pygame.event.get(): print(event)
https://pygame.readthedocs.io/en/latest/...p-properly Wrote:Quit the event loop properly
In order to quit the application properly, from within the application, by using the window close button (QUIT event), we modify the event loop. First we introduce the boolean variablerunning
and set it toTrue
. Within the event loop we check for the QUIT event. If it occurs, we setrunning
toFalse
:
running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = Falsepygame.quit()
Once the event loop, we call thepygame.quit()
function to end the application correctly.