Coders Packet

Brick Breaker using Java AWT and Swing

By Shreya Shukla

Brick Breaker is an arcade game in which a player has to break a wall of bricks by controlling a bouncing ball with a paddle that moves horizontally with the help of left and right arrow keys.

Brick Breaker game output

Brick Breaker is a 1 player video game. The code for this project is divided into 3 classes that work together to implement this game.

            1) Board class: This class is responsible to create the bricks, and assigning the bricks a code so that the bricks can be drawn on the screen with different colours.

            2) Game class: This class draws the elements required for this game, manages these elements with the help of event handlers, manages the speed of the game with the help of the Timer class, and enacts the rules of this game.

            3) BrickBreaker class: The main function or the entry point function defined inside this class creates the JFrame object and the Game class object and adds the Game class object to the JFrame object.

 

About the Board class:

            Features of Board class:

                        1) Instance variables are declared inside this class to set the width of the bricks, the height of the brick, and the number of rows and columns for the bricks.

                        2) A data structure is also used for storing the code of all the bricks.

                        3) getCode(): This method uses the random function to generate random codes for all the bricks, lying between 0 and 6.

                        4) brickColour(): The brickColour() method assigns different colours to the bricks depending on the code they are allotted by the getCode() method.

                        5) draw(): The draw() method draws those bricks on the screen that have been allotted code other than zero. Thus, giving the effect of random level generation. The Graphics2D class type object is passed as a parameter to the draw() method.

                        6) setBrickValue(): This method assigns the value passed to it, to the specified brick.

 

About the Game class:

           Features of Game class:

                        1) Instance variables have been declared inside this class to control the game flow, the movement of the ball, the paddle's location, and manipulate the score.

                        2) The Timer class type object helps to control the speed of this game.

                        3) paint(): This method is responsible for painting the ball, the paddle, and the strings to be displayed on the screen when the game ends.

                        4) actionPerformed(): This method is called automatically when the arrow keys are pressed to start the game. It is called repeatedly, throughout the game to repaint the GUI elements after a delay according to the Timer class type object. This method manipulates the direction of the ball when it hits the paddle and the bricks. This method also ensures that the ball does not go out of the JFrame object. The score is updated, the number of bricks displayed on the screen is reduced, and increases the speed of the ball after the user or player reaches a particular score.

                        5) keyPressed(): This method calls the moveRight() and moveLeft() methods when the right and left arrow keys are pressed, respectively. If the game is over, a new game can be played by pressing the ENTER key. All the necessary instance variables are reset so that a new game can be played.

                        6) moveRight(): This method moves the paddle in the right direction.

                        7) moveLeft(): This method moves the paddle in the left direction.

About the BrickBreaker class:

            Features of BrickBreaker class:

                        1) main(): The main() function is the entry point function, declared inside BrickBreaker class. It instantiates the JFrame class type object and the Game class type object and adds it to the JFrame class type object. This function further sets the size, title, resizeability, visibility, default close operation, and location on the screen of the JFrame class-type object.

 

The program works as follows: 

           1) The player presses any of the arrow keys to start the game.

           2) Press the left and right arrow keys to control the paddle so that the ball bounces back and hits the bricks.

           3) Bricks disappear after the ball hits the bricks and the score increases.

           4) If the ball misses the paddle or the ball hits all the bricks, the game ends and the score is displayed on the screen.

           5) Press ENTER key to start a new game.

 

Algorithm of Brick Breaker game:

           1) Initialize the game board with some bricks, a paddle and a ball.

           2) Start the game.

           3) if(ball hits the paddle) reverse the vertical direction of the ball;

           4) if(ball hits a brick)

               {

                        (total number of bricks)--;

                        score = score + c;

                        if(ball hits the brick on its left or right side) reverse the horizontal direction of the ball;

                        else if(ball hits the brick on the top or bottom side) reverse the vertical direction of the ball;

                }

           5) Update the position of the paddle and the ball.

           6) if(ball misses the paddle) end the game;

           7) if(total number of bricks == 0) end the game;

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Shreya Shukla (Shreya35)

Download packets of source code on Coders Packet