Breakout

project / August 9th, 2021

Breakout Game Screen

GitHub

Background

Breakout is a desktop single-player game app that is based on the popular arcade game of the same name. Similar to Pong, the player controls a paddle to prevent a bouncing ball from bypassing it and attempts to direct the ball towards the rows of blocks above it. For every block that is knocked down, the user will add points to their current score.

In this version of Breakout, the rows of blocks are initially worth 1 to 5 points - with the blue row at the bottom containing 1-point blocks and gradually increasing row-by-row to the red row at the top containing 5-point blocks. After an entire wall of blocks is cleared, a new wall of blocks is loaded onto the screen and the initial block values are multiplied by 2. A third wall of blocks will contain blocks with initial values multiplied by 3. The block values will continue to increase until the sixth wall, which will still contain point values from the fifth wall. All subsequent walls will remain the same value. Similarly, the ball will move at a higher speed after each new wall loads until the sixth wall and stay at that speed thereafter.

In each game, the player will have 5 balls (or lives) to accumulate the highest possible score before the game ends. After a game is finished, if the player has a final score that is higher than the current high score, their final score will become the new high score displayed on the scoreboard. The player will then have an option to play another game or exit.

This is a professional portfolio project that was created after completing the 100 Days of Code Python Bootcamp.

Purpose

After completing the 100 Days of Code Python Bootcamp, this project was offered as a suggested project idea to apply my knowledge of the Python Turtle Iibrary from the course to build games. Just like other professional portfolio projects, developing skills in planning and executing a project from scratch without guidance was the most valuable lesson. Additionally, this project provided an opportunity to apply object-oriented programming and class inheritance concepts to each game component.

Concepts and Tools Used

  • Python

  • Turtle

  • Object-oriented programming

Process

For this project, I wanted to follow a similar model to the Pong game I built in the 100 Days of Code Bootcamp where each component is an object modeled from a class blueprint, while adding new skills that I've learned from other parts of the course. After familiarizing myself with the gameplay and setup of Breakout by playing it, I began to plan out the classes that the game would use.

My main script will import four different classes:

  • A Paddle class which will contain the attributes and methods needed to create a moving paddle at the bottom of the screen that can be controlled by the player.

  • A Ball class which will create a live, moving ball that will bounce off the top and side walls, the paddle, and the bricks. The ball will spawn between the bricks and the paddle.

  • A Block class that will create each brick in the upper half of the screen.

  • A Scoreboard class that will update and display the balls remaining, the current high score, and the player's current score at the top of the screen. If player reaches a new high score, it will be saved in a text file and updated on the screen.

Each of these classes will be created in Python files separate from the main file, and inherit attributes and methods from the Turtle class from the Turtle library.

Breakout Start Game Pop-Up

After planning, step 1 of the project is to create the game screen. A screen is created using the Screen class from the Turtle library and the size, background colour, title, and animation settings are specified.

Step 2 is to create another Python file that will contain the Paddle class. The Paddle class will inherit attributes and methods from the Turtle class using the super method. This enables the methods from the Turtle class to be called inside the Paddle class. After specifying the shape, methods are created to enable the paddle to move left and right. The Paddle class is then exported to the main script and a paddle is created from it on the bottom of the screen. Each Paddle method to move left or right is bound to the left and right arrow keys with the Screen onkeypress method. During the game, the screen will listen for the left or right arrow key to be pressed and execute a method to move the paddle left or right.

Step 3 is to create the Ball class. After inheriting the Turtle class and specifying the shape and colour of the ball, more attributes are added to specify the distance the ball will move in both the x and y direction for each second and the speed this will occur at. A move method using these attributes to move the ball is subsequently created. To handle situations in which the ball makes contact with the top or side walls, two methods are created to ensure the ball bounces down (if it hits the top wall) and bounces in the opposite direction if it hits a side wall. Additional methods are also written to handle contact with the paddle and the blocks to ensure they bounce in the appropriate direction. Finally, when the ball bypasses the paddle and makes contact with the bottom wall, the ball will reset to the middle of the screen and move upwards as if it hit the paddle. The Ball class is then exported to the main script and a Ball object is created. Inside the main script, the ball's move method will be called inside a while loop to ensure continuous movement. Conditions are also set to decide which ball methods will be called, based on the ball's location and distance from the walls, blocks, or paddle.

Step 4 is to create the Block class. Each block will appear in the shape of a rectangle and be positioned to whichever coordinates are passed to it. In the main script, arrays to store the block colours and the x and y-coordinates of each block are created. This is followed by writing a function called load_blocks that will loop through each x and y-coordinate array and instantiate a new Block object at each coordinate, until 5 rows with 7 blocks in each row are created. Each row will have a different colour.

Step 5 is to create a Scoreboard class. When the game starts and a Scoreboard object is created, the first thing it will do is open a text file called high_score that should contain a number in string format. The string will be read, converted into an integer, and will be assigned to an attribute containing the high score. The current score is set to 0 and the number of balls available is set to 5 at the start of the game. An update method is then written to display these values at the top of the screen. A scoring method is written to add points to the player's score. The point amount is based on the row of the removed block and the current number of walls the player has cleared. Another method is written to deduct the number of balls by one when a paddle misses a ball. Finally, a method is written to write a new high score in the text file if the player's final score is higher than the current high score displayed. The Scoreboard class is then exported to the main script and its methods are called to update the score when the ball hits a block or goes out of bounds. If the player has no balls left, the scoreboard text will change to "Game Over!"

Breakout Game Over Pop-Up

Step 6 involves completing the remainder of the game logic. First, pop-up screens are added at the start and end of the game to ask the player if they wish to start the game or play again. If the player writes 'n' for either pop-up screen, the screen's bye method is triggered and the screen closes. If the current wall of blocks is cleared, the ball speed will increase (until the sixth wall) and another wall of blocks is loaded by calling load_blocks. After completing the game logic, the entire code is placed inside a function called play_game that will be called when the program is run. If the player wishes to play again, the screen will clear and play_game will call itself to start another game.

Breakout New High Score Screen

Outcome and Takeaways

Initially, I was hesitant about this project because I found the Turtle projects from the bootcamp like Pong and Snake to be challenging. However, these projects were made at the start of the bootcamp and being able to create Breakout on my own has shown how much my skills have improved since then. It was fun to learn about a game I had never played before and add my own touches (like the scoring system) to make it more interesting as the game gets longer.

Possible Improvements Going Forward

My version of Breakout can be further improved by continuing to work on the ball and bouncing mechanics. While I am satisfied with the end result, the occasional glitch does occur where the ball bounces too early/late or does not bounce in the intended direction. It would also help to add more new layers to the scoring system (like having golden bricks that are worth more points) to add more variation to the scoring. Another idea to motivate players is to enable players to attach a name to their final score after a game is finished and display a list of the highest final scores.


External Link