Breakout Tutorial using Pygame – Getting Started

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

Breakout is one of the earliest arcade video games, first released in 1976 by Atari. It was derived from the game Pong (released by Atari in 1972). This one-player game features simple 2D graphics. It consists of one paddle (in more recent version the paddle was replaced by a spaceship) used to return a bouncing ball back and forth across the screen. The aim of the game is to break the bricks of a brick wall by getting the ball to hit/bounce on the bricks. The score correspond to the number of bricks being hit.

In this tutorial we are going to recreate a game of Breakout 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. We will also initialise two global variables, score and lives that we will use later on in the game.

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 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.

Next Step?


Your background is ready? Let’s add the first sprite to your project by completing the next tutorial:
Breakout Tutorial using Pygame:Adding the Paddle
Share Button
Tagged with: