This tutorial is the first tutorial in a series of five Pygame tutorials:
- Pong Tutorial 1: Getting Started
- Pong Tutorial 2: Adding the Paddles
- Pong Tutorial 3: Controlling the Paddles
- Pong Tutorial 4: Adding a Bouncing Ball
- Pong Tutorial 5: Adding a Scoring system
- Extra: Pygame How To’s?
Pong is one of the earliest arcade video games, first released in 1972 by Atari. It is a two-player game based on table tennis. The game features simple 2D graphics. It consists of two paddles used to return a bouncing ball back and forth across the screen. The score is kept by the numbers at the top of the screen.
In this tutorial we are going to recreate a game of Pong using Python and the Pygame library. The Pygame library is the perfect library to build basic 2D arcade games and to start developing your OOP skills. (Object-Oriented Programming)
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 import pygame pygame.init()
Note that you will first need to install the Pygame library on your computer. Alternatively you can complete this challenge online using the following Trinket/Pygame IDE
Step 2: Defining 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. Pong is a very basic game and only uses two colours: black and white.
# Define some colors BLACK = (0,0,0) WHITE = (255,255,255)
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("Pong")
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 uses 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.
# Import the pygame library and initialise the game engine import pygame pygame.init() # Define some colors BLACK = (0,0,0) WHITE = (255,255,255) # Open a new window size = (700, 500) screen = pygame.display.set_mode(size) pygame.display.set_caption("Pong") # The loop will carry on until the user exits 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 ----------- while carryOn: # --- 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 black. screen.fill(BLACK) #Draw the net pygame.draw.line(screen, WHITE, [349, 0], [349, 500], 5) # --- Go ahead and update the screen with what we've drawn. pygame.display.flip() # --- Limit to 60 frames per second clock.tick(60) #Once we have exited the main program loop we can stop the game engine: pygame.quit()
Next Step?
Your background is ready? Let’s add the first sprite to your project by completing the next tutorial:
Pong Tutorial using Pygame:Adding the Paddles