Skip to content
Related Articles

Related Articles

Improve Article

How to set up the Game Loop in PygGame ?

  • Last Updated : 21 Apr, 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:

  1. Update our game window to show visual changes
  2. Update our game states based on user inputs
  3. Handle different types of events
  4. 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.

Setting up the Game Loop

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 package
import pygame
  
# initiallizing imported module
pygame.init()
  
# displaying a window of height
# 500 and width 400
pygame.display.set_mode((400, 500))
  
# Setting name for window
pygame.display.set_caption('GeeksforGeeks')
  
# creating a bool value which checks 
# if game is running
running = True
  
# Game loop
# keep game running till running is true
while 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 package
import pygame
  
# initiallizing imported module
pygame.init()
  
# displaying a window of height
# 500 and width 400
window = pygame.display.set_mode((400, 500))
  
# creating a bool value which checks 
# if game is running
running = True
  
# setting variable to storecolor
color = "red"
  
# keep game running till running is true
while 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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :