How to set up the Game Loop in PygGame ? - GeeksforGeeks

01 Oct, 2021
In this article, we will see how to set up a game loop in PyGame. Game Loop is the loop that keeps the game running. It keeps running till the user wants to exit. While the game loop is running it mainly does the following tasks:
Update our game window to show visual changesUpdate our game states based on user inputsHandle different types of eventsKeep game window running
Update our game window to show visual changes
Update our game states based on user inputs
Handle different types of events
Keep game window running
Simply game loop is a while loop having only one condition to check whether our boolean condition to keep the game running is true.
Step 1: Declare a Boolean variable to true which will be used to check whether our player wants to keep playing our game or not.  
keepGameRunning=true
Step 2: Create a while loop and check our above Boolean variable that whether it is true or not. If true keep the loop running which suggests keeping our game loop running. In this while loop check for events and if the event is quit then set the above variable too false to exit our game loop and end our pygame window.
while keepGameRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  
           keepGameRunning = False
Below is the complete implementation.
In the below code, we are creating a simple game loop that creates a pygame window and checks if the event type is quit and, if it is true then quit the game.
Python3
# import pygame packageimport pygame # initializing imported modulepygame.init() # displaying a window of height# 500 and width 400pygame.display.set_mode((400, 500)) # Setting name for windowpygame.display.set_caption('GeeksforGeeks') # creating a bool value which checks# if game is runningrunning = True # Game loop# keep game running till running is truewhile running:       # Check for event if user has pushed    # any event in queue    for event in pygame.event.get():               # if event is of type quit then set        # running bool to false        if event.type == pygame.QUIT:            running = False
Output:
In the below code, we added one more task to our game loop i.e. to update our screen in each loop. Here we change the background color for each loop by updating our screen in each loop.
Python3
# import pygame packageimport pygame # initializing imported modulepygame.init() # displaying a window of height# 500 and width 400window = pygame.display.set_mode((400, 500)) # creating a bool value which checks# if game is runningrunning = True # setting variable to storecolorcolor = "red" # keep game running till running is truewhile running:       # Check for event if user has pushed    # any event in queue    for event in pygame.event.get():                 # if event is of type quit then set        # running bool to false        if event.type == pygame.QUIT:            running = False         # set background color to our window    window.fill(color)         # Update our window    pygame.display.flip()         # if color is red change it to green and    # vice-versa    if(color == "red"):        color = "green"             else:        color = "red"
Output:
anikaseth98
Picked
Python-PyGame
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()