Java Reference
In-Depth Information
private: color: Color
fill: boolean
shape: RectangularShape
public: BreakoutShape(shape: RectangularShape, color: Color, fill: boolean)
getColor(): Color
changeColor(color: Color)
draw(g2: Graphics2d)
getX(): double
getY(): double
getWidth(): double
getHeight(): double
move(dx: int, dy: int)
class BreakoutShape
pivate: dx:int
dy: int
public: Brick(row: int, col: int, color: Color)
public: Paddle(color: Color)
move(dx: int)
Ball(color: Color)
class Brick
class Paddle
class Ball
FIGURE 11.1: UML Diagram of the game Breakout.
stop the ball from escaping. When the player loses a life, a stickman should be removed
from the screen. The game ends when all lives are exhausted.
11.2 Game Design
First, we need to decide on the classes and the data and methods in them. We will
demonstrate an approach that was first proposed by Abbott in 1983. The approach states to
identify the classes from the textual description of the problem. Looking at the description,
we will create the Breakout class where the main method is written. We will also include
classes for the main participants of the game: Ball , Brick , Paddle ,and Player . We will
create two enumeration types: BallColor and Speed . There, we will store the possibilities
for the ball color and ball speed. We will need to create two additional classes that relate
to Java: BreakoutFrame and BreakoutPanel . These will be the window and the drawing
panel for our application, respectively. Lastly, we will create a BreakoutShape class that
will be the superclass of all the shapes that will be displayed. Since drawing the paddle,
ball, and brick are similar, it is reasonable to create a single superclass. Part of our initial
design is shown in Figure 11.1.
The BreakoutShape class is used to represent a shape that is bounded by a rectangle,
that is, a ball, a paddle, or a brick. A breakout shape can have a color and can be filled or
not filled. In our case, all breakout shapes are filled. However, the field fill is added just
in case we want to create a breakout shape that is not filled in the future. Writing a class
that is versatile and that supports features that may not be currently needed is a common
programming approach. However, one should not go overboard implementing features that
are not presently required.
Search WWH ::

Custom Search