Breakout Tutorial using Pygame: Adding a Brick Wall

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

The final stage of our tutorial focuses on adding a brick wall and a scoring system to our Breakout game:

  • The player will score a point if the ball bounces against a brick.
  • The player will lose a life if the ball bounces against the bottom edge of the screen.
  • Both the score and number of lives will be displayed at the top of the screen.
  • A “Level Complete” message will be displayed if all bricks have been removed.
  • A “Game Over” message will be displayed if the number of lives reaches zero.
  • The final code for the main.py is provided below. We made several changes to the code as follows:

    • On line 6 we import the Brick class. (Code provided in the brick.py tab)
    • On lines 39 to 57 we create three rows of bricks and add them to a group called all_bricks.
    • On lines 93 to 103 we take a life away when the ball hit the bottom edge of the screen. If the number of lives reaches zero, we display a “Game Over” message..
    • On lines 114 to 129 we detect if the ball hits a brick. If so we remove the brick (using the kill() method) and increment the score by one.
    main.pypaddle.pyball.pybrick.py
    The code for the Paddle class remains unchanged.
    The code for the Ball class remains unchanged.
    This is the code for the new Brick class, to be saved in a new file called brick.py.
    Share Button
    Tagged with: