The game loop is the most important part of a game. We will be going over the parts shown below.
As you can see, the game loop revolves around 3 main parts: handling events, updating the game state, and drawing the screen
Updating the game state means specifying which part of the game the player is currently in. This could be states like “paused”, “menu”, or “playing”.
Example: In the next chapter's basic application (2.2 - Basic Application), we set the game state of “run” to true and execute the game loop until “run” becomes false.
Drawing the screen means updating the window (or printing to the console) to display the current events in the game.
Example: In the next chapter's comprehensive example (2.5 - Example ), we use
pygame.display.update()to update the display to show the movement of the rectangle.
Finally, the third component in the game loop is handling events. This means taking in user input and handling them accordingly.
Example: In the next chapter's basic application (2.2 - Basic Application), we check for the1.2 - Example
pygame.QUITevent. If this event had occurred, we’d exit out of the game loop.