O'Reilly logo

Learning Java Through Games by Lubomir Stanchev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11
The Breakout Game (Complete Version)
11.1 Overview of the Game .................................................................. 239
11.2 Game Design ............................................................................ 240
11.3 Moving the Ball ......................................................................... 241
11.4 Adding the Paddle ...................................................................... 244
11.5 Drawing the Stickmen .................................................................. 250
11.6 Adding the Menus ...................................................................... 252
11.7 Adding the Bricks ...................................................................... 256
11.8 Summary ................................................................................ 263
11.9 Syntax .................................................................................. 263
11.10 Important Points ....................................................................... 264
11.11 Exercises ................................................................................ 264
11.12 Lab ...................................................................................... 265
11.13 Project .................................................................................. 265
Implementing the Breakout game is relatively challenging. We will use a for statement
to display the blocks. We will use a combination of if statements to determine if the ball
intersects one of the other objects in the game. We will remind ourselves how to use the enum
construct by defining a new type that contains the constants FAST, NORMAL,andSLOW,that
is, the possible ball speeds. We will use a mouse listener and a keyboard listener to move
the paddle. We will override methods and rely on dynamic binding to simplify the code. In
summary, we will cover most of the material that is presented so far in the book. Needless
to say, our solution will be a multi-class solution that utilizes a lot of nested classes. Most
importantly, this chapter will show an example of the whole process of designing and writing
non-trivial software. Our code will contain eight classes, two enum types, and approximately
700 lines of code. Although there are infinitely many choices of how to implement the game,
we will strive to create a design that is elegant, easy to understand, easy to modify, and
that conforms to good software practices.
11.1 Overview of the Game
Our game will have a menu bar with three menus: Game, Ball Color,andBall Speed.
From the game menu, the player can start and pause the game or entirely quit the game.
From the ball color menu, the player can select the color of the ball. We will allow the ball
to be red, blue, or green. From the speed menu, the player can select the speed of the ball:
FAST, NORMAL,orSLOW. A blue paddle will appear at the bottom of the screen and it can
be moved left or right with the mouse or keyboard. The ball will jump left if it hits the left
part of the paddle and right if it hits the right part of the paddle. The ball will also change
direction when it hits one of the walls or one of the bricks. The bricks are arranged in a
rectangular grid. When a brick is hit, it should be removed from the screen. We will use a
random generator to select the color of every brick. At the bottom left part of the screen,
we will display stickman icons. A life is lost when the user is unable to use the paddle to
239

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required