This tutorial is the first tutorial in a serie of five Pygame tutorials:
- Tutorial 1 – Getting Started with Pygame
- Tutorial 2 – Creating Sprites using Pygame
- Tutorial 3 – How to control your sprites
- Tutorial 4 – Adding More Sprites
- Tutorial 5 – Pygame – How-To’s
These instructions will guide you through the first few steps required to set up the foundation for a Pygame project.
Now that you have learned the basics of Python you are most likely willing to start creating your own games. Pygame is one of the best libraries to create mainly 2D retro arcade games such as Tetris, PacMan or Space Invaders.
Let’s see what are the first few steps needed to create your first game in Python.
Make sure you have access to the Pygame library. If not you will need to download and install it following the instructions provided on the Pygame website: http://www.pygame.org/.
Step 1: Importing and initialising the Pygame library
Your Python code will need to start with the following two lines of code:
# Import the pygame library and initialise the game engine
Step 2: Define the colours you will use in your game
You will have to declare a constant for each of the main colours used within your game. To help you identify colour codes you may use a colour picker.
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
Step 3: Open a new window
Your game will run in its own window, for which you can decide of a title, a width and a height.
# Open a new window
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption(“My First Game")
Step 4: The main program loop
The main program loop is the key wrapper for your game.
The main program loop will contain 3 main sections:
- Capturing Events: Used to constantly “listen” to user inputs and react to these. It could be when the user use the keyboard or the mouse.
- Implementing the Game Logic. What happens when the game is running? Are cars moving forward, aliens falling from the sky, ghosts chasing you, etc.
- Refreshing the screen by redrawing the stage and the sprites.
The main program loop will also use a frame rate to decide how often should the program complete the loop (& refresh the screen) per second. To implement this we will use the clock object from the pygame library.
The main program loop will use a timer to decide how many times it will be executed per second.
# The loop will carry on until the user exit the game (e.g. clicks the close button).
carryOn = True
# The clock will be used to control how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
# --- Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
carryOn = False # Flag that we are done so we exit this loop
# --- Game logic should go here
# --- Drawing code should go here
# First, clear the screen to white.
#The you can draw different shapes and lines or add text to your background stage.
pygame.draw.rect(screen, RED, [55, 200, 100, 70],0)
pygame.draw.line(screen, GREEN, [0, 0], [100, 100], 5)
pygame.draw.ellipse(screen, BLACK, [20,20,250,100], 2)
# --- Go ahead and update the screen with what we've drawn.
# --- Limit to 60 frames per second
#Once we have exited the main program loop we can stop the game engine:
Use the code above to create your first Pygame project.
Complete the code to draw the following background:
Your background is ready? Let’s add the first sprite to your project by completing the next tutorial: