Pong Tutorial using Pygame – Getting Started

pong-gameThis tutorial is the first tutorial in a series of five Pygame tutorials:

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:

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.

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.

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.

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
Tagged with: