Breakout ball game using Java
A step by step process to build this game from scratch
In this blog, we are going to discuss step by step process to create a Breakout ball game using Java. In this game, there is a layer of bricks lines that covers the top third of the screen, a ball, and a paddle. A paddle moves horizontally to bounce the ball back upward keeping it playing. When a ball collides with a brick, it bounces back and destroys the brick. The player loses a turn when the ball touches the bottom of the screen. The goal is to break all the bricks without missing the ball. You should have a basic knowledge of Java Swing and OOPS concepts to make this project.
First of all, to create this project you need to set up the development environment for the Java GUI project. For this, if you don’t already have it, install Java 8 or higher. Additionally, you need a code editor or IDE (Eclipse/Netbeans/IntelliJ) to write the code.
References for the download:
Steps involve:
- Design the game window
- Design the gameplay (background, bricks, ball, and paddle)
- Implement the actions of the keys (to move the paddle sideways).
- Implement the action performed by the ball on the bricks and paddle.
You should be able to achieve these goals at the end of the game:
- The player should be able to start the game by pressing the ENTER key.
- The player should be able to move the paddle horizontally using left and right arrow keys on the keyboard.
- Once the player loses the ball, i.e. when the ball touches the bottom of the window, the game ends.
- Breaking each brick should provide the player a certain score. Let’s say each brick contains 5 points, if a player breaks 10 such bricks, he gets a score of 50.
- At the end of the game, the terminal will present the player’s final score and give him the option to restart the game again.
In our game, we have one paddle, one ball, and 36 bricks. I have created a ball, paddle, and brick using graphics class in java. To create the game cycle, we will use the Timer class. For the sake of simplicity we are not working with angles, we simply change directions to top, bottom, left, and right. The game consists of three files: Main.java, Gameplay.java, and MapGenerator.java. We will talk about each class in detail. So, let's get started.
1. Design the game window
Firstly create a java project name Breakout-Ball into your ide. Inside that project folder, create a package and class Main.java with the main method. In this main method, we will create and design a game window. Here, I have also created an object of gameplay to run the game which will be discussed further in detail. So, don't worry.
2. Design the gameplay
Create a new class Gameplay.java inside your package which extends JPanel. This class will be the panel where the game works. Firstly, we will define some common properties of the game. We are using the Timer class for setting the speed for the ball. Then create a public method paint to draw paddle and ball using Graphics.
To draw the bricks, create MapGenerator.java class inside your project folder. Here we are using a generic map for drawing bricks, so it does not remain static and can be changed if you want. We have to call the draw method in the above paint method to draw bricks.
Now, we have to create an object of MapGenerator in the Gameplay class and initialize it into the Gameplay constructor. For the Timer class in this constructor, you have to import javax.swing.Timer package to call Timer class with parameters.
3. Implement the action of the keys
Now, to detect keys and their actions, we have to implement KeyListener and EventListener interfaces to our Gameplay class and add all the unimplemented methods. We will implement key pressed to move paddle horizontally using left and right arrow keys.
4. Implement action performed by the ball on the bricks and paddle
When the game is started, in order to move the ball, we have to detect if it’s touching left, right, or top of the panel and change its direction accordingly. Then, we have to find the collision between the ball and the paddle, and the ball and the map. For detecting collisions between two objects, we need a rectangle, so we have to create a rectangle around the ball as it is oval.
When the ball intersects the brick, we have to break the loop so we have used a label to break the loop. If we use the break keyword compiler will only take you out of the inner loop but we want to out of the outer loop that is why we have used labels. We have called the repaint() method to redraw paddle, ball, and map after every movement.
5. Displaying result
Inside the paint() method of Gameplay class, add a score and result when the game is over.
Well done! You have completed the Breakout Ball game.
This is a very simple version of the breakout ball game. You can try to implement the following modifications by yourself:
- Add audio sound whenever ball touches the brick and paddle
- Improve the user control over bounces i.e., speed of the ball increases with time and keeping direction uncertain every time.
- Assign different scores for different colours of brick.
- Add in the kicker i.e., Add bonus bricks that contain an extra score and a pop-up message.
- Create an application for the game which have game levels.
You can find this source code in my Github repository.