This tutorial is the first tutorial in a series of five Pygame tutorials:
- Breakout Tutorial 1: Getting Started
- Breakout Tutorial 2: Adding the Paddle
- Breakout Tutorial 3: Controlling the Paddle
- Breakout Tutorial 4: Adding a Bouncing Ball
- Breakout Tutorial 5: Adding a Brick Wall
- Extra: Pygame How To’s?
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:
#Import the pygame library and initialise the game engine import pygame pygame.init()
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.
# Define some colors WHITE = (255,255,255) DARKBLUE = (36,90,190) LIGHTBLUE = (0,176,240) RED = (255,0,0) ORANGE = (255,100,0) YELLOW = (255,255,0) score = 0 lives = 3
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 = (800, 600) screen = pygame.display.set_mode(size) pygame.display.set_caption("Breakout 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 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 WHITE = (255,255,255) DARKBLUE = (36,90,190) LIGHTBLUE = (0,176,240) RED = (255,0,0) ORANGE = (255,100,0) YELLOW = (255,255,0) score = 0 lives = 3 # Open a new window size = (800, 600) screen = pygame.display.set_mode(size) pygame.display.set_caption("Breakout Game") # 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 dark blue. screen.fill(DARKBLUE) pygame.draw.line(screen, WHITE, [0, 38], [800, 38], 2) #Display the score and the number of lives at the top of the screen font = pygame.font.Font(None, 34) text = font.render("Score: " + str(score), 1, WHITE) screen.blit(text, (20,10)) text = font.render("Lives: " + str(lives), 1, WHITE) screen.blit(text, (650,10)) # --- 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()
Your background is ready? Let’s add the first sprite to your project by completing the next tutorial: