1. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
Advanced Programming Lab Notes
Brick Breaker Game In Java
1 RESOURCES
• Package java.awt
o http://docs.oracle.com/javase/8/docs/api/java/awt/package-frame.html
• Package javax.swing
o http://docs.oracle.com/javase/8/docs/api/javax/swing/package-frame.html
• Defining and starting thread
o https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
2 LAB SESSION
2.1 CREATE A NEW PROJECT
• Select File->New->Java Project
• “Under Create a Java Project” dialog box, for the
Project Name field, type in BrickBreaker-add as project
name
2.2 CREATE A MAIN CLASS
• Under the “src” package
create Main class
• Add the code snippet shown below to the Main class.
2. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Now you can get the screen
• We will use 8 brick at each row
• Each brick has a dimension 60x25
• Therefore the dimension of the frame is
490x400
• We will use pillButton image for brick
image
• There are 4 brick color
o Blue
o Green
o Red
o Yellow
2.3 BRICK CLASS
• We need to create Brick class to create brick object
• Create new class called Brick under the “src” package.
3. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• This class also use as a
container for ball (brick
breaker) and paddle (user
controller)
o x, y: Brick’s coordinate on
the frame
o width, height: width and
height of the brick
o pic: pill button image
that display on the brick.
o s: is the path for the
image file
2.4 BRICKBREAKERPANEL CLASS
• We need to add BrickBreakerPanel class as a container for the bricks.
• First we need to create BrickBreakerPanel class as a new class under the “src” package
• 2 pixel spaces between two bricks.
4. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• To show the bricks we need to
add code that shown on the left side to
Main class
• Than we get the screen shown below
2.5 ANIMATE THE OBJECTS
• We need to create runnable Animate class to animate the objects on the screen.
5. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Animate class implements Runnable because we want to use threads.
• Create the Animate class under the “src” package that implements Runnable interface.
• We need to add “update” method to BrickBreakerPanel class to repaint the screen. And also need to add the
“super.paintComponent(g);” in paintComponent methods to erase and repaint the screen.
2.6 ADD USER INPUTS (ADD PADDLE)
• Now we‘re going to add user input.
• User controls the ball by paddle during the game. Then we need to add paddle to game.
• Draw the paddle on the screen
6. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Add the KeyListener interface to the BirckBreakerPanel class.
• Also add unimplemented
methods.
• To control the paddle we need to get user input from keyboard through key listener. When the user press Left key
paddle going to move to the left (15 px), and when the user press the right button paddle going to move to the
right (15px).
• Add animate object and also the thread to the BrickBreakerPanel class constructor.
• Add key listener to the BrickBreakerPanel class constructor to read the keys.
7. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• To start the game when the user press the “Enter”
key. Add code to the keyPressed method.
• Create animate object, create thread with the
animate object and then start the thread to start the
animation/game.
• When you run the code you can see that the paddle is going to out of the screen. To prevent this;
2.7 ADD BALL
• Add array list for ball object in BrickBreakerPanel Class.
• Add the first ball. Width and height of the ball is 25 px.
8. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Paint the ball on the screen
• Run the project.
2.8 INTERACTION WITH THE PADDLE AND SCREEN BORDERS
• We can specify the displacement dx and dy in each coordinate direction by the difference in 3 px.
• Add dx, dy integer variables for the movement in Brick class.
• In update thread, add the code snippet for the ball movement between the screen borders. If the ball intersects
the paddle, ball going to flipped the Y coordinate direction.
9. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
• Run the project. You can see that the ball moving between the screen borders but not destroy any bricks.
• Ball can start the movement and left the screen.
2.9 DESTROYING THE BRICKS
• Create Boolean variable “destroy” in Brick class. Default value is “False”.
• If the destroy field equals to “False” then the brick will appear on the screen, otherwise won’t.
• In update thread, if the ball intersects the brick and the brick is not destroyed then this brick going to be
destroyed. Then change the direction of the ball.
• Run the project and have fun.
10. THIS WORK IS LICENSED UNDER A CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE |
3 CONTACT
Dr. Celal Murat KANDEMİR
Eskisehir Osmangazi University
Faculty of Education
kandemir@ogu.edu.tr
twitter.com/cmkandemir