Design Pattern Exercises with the Asteroids Game Example [from a book chapter “Teaching Design Patterns” by Bernd Brügge and Timo Wolf] Asteroids Game Description The Asteroids player controls a space shuttle on a game board, representing the outer space. Two different types of asteroids are also present on the game board: (a) small and fast asteroids, which change their direction only when they hit the edge of the game board, and (b) big and slow asteroids, which may change their direction any time. All asteroids can change their speed, ranging from a minimum to a maximum speed. The player can fire rockets and change the direction and speed of the space shuttle by using the keyboard. Striking a big asteroid with a rocket replaces the big asteroid with three small asteroids. Striking a small asteroid removes the asteroid from the game board. The player wins if he destroys all asteroids. He loses if his space shuttle collides with any asteroid. Figure 1 shows a screenshot of the initial version of Asteroids. Figure 1. Screenshot of the initial Asteroids game. Fig. 2 shows the initial class diagram obtained from the game description. We focus on the main classes and concepts and omit unnecessary details. In the following we describe the classes of Fig. 2: Page 1
Figure 2. Initial class model of Asteroids. Class : Game The Game class represents the main window of the asteroids application. It consists of a GameBoard and a ToolBar class. It is the root component for all graphical user interface classes. Class : ToolBar The class ToolBar is a graphical component that presents a start and stop button to the Asteroids player. Class : SolidBody The class SolidBody represents an abstraction of all objects that move over the GameBoard. The SolidBody has a body attribute that represents an image used for painting. The flight direction is stored in the direction attribute. Values from 0 and 360 are valid and represent the direction in degree. Zero degree is the direction pointing to the top. The position attribute has a horizontal and a vertical coordinate and holds the current position of a SolidBody. Each SolidBody has a speed attribute. All attributes are protected to prevent modifications from foreign objects. Getter operations are provided to access the attributes of the SolidBody. The operation updatePosition () computes and sets a new position, based on the speed and direction attribute values. If the new position reaches the boundary of the GameBoard, the direction of the solid body gets changed with the rule: the angle of incidence is the angle of reflection. Class: Asteroid The class Asteroid represents an asteroid that moves over the GameBoard. The Asteroid class overwrites the updatePosition () operation and randomly increments or decrements the speed. Class: BigAsteroid Page 2
The class BigAsteroid specializes the Asteroids class by adding extra functionality that randomly changes the direction. Class: SmallAsteroid This represents a small asteroid that moves with higher speed than do objects of the BigAsteroid class. Class: SpaceShuttle The space shuttle is the solid body, controlled by the player. The player loses the game if the space shuttle is destroyed. He wins if the space shuttle is the only solid body that is left on the GameBoard. The class provides public operations to change the speed, the direction, and to fire rockets. Class: Rocket Rocket objects get created by the fireRocket() operation of the SpaceShuttle class. Their direction is the space shuttle’s direction and do not change. Rockets destroy any other solid bodies. They get removed from the GameBoard when reaching the GameBoard boundaries or when hitting other solid bodies. Class: GameBoard The GameBoard represents the outer space of the game asteroids. It consists of many SolidBody objects. During a game, the solid bodies are moving on the GameBoard. The GameBoard paints the images of the SolidBodies at their position. The positions of the solid bodies are always within the surface of the GameBoard. Class: Referee The Referee class is the main controller of the game Asteroids. It is responsible for starting, controlling, and stopping the game. Starting the game creates a separate control thread that invokes the operation moveSolidBodies() in frequent intervals. The operation moveSolidBodies() calls the updatePosition() operation of all solid-body objects and forces the GameBoard to repaint, thus enabling the SolidBody movement. After moving all solid bodies, the operation - detectCollision (. . .) : SolidBody is used to identify collisions. It computes if the two solid bodies body1 and body2 intersect and decides which solid body gets destroyed. The destroyed solid body is returned. If two asteroids intersect, none of them get destroyed. If an asteroid or the space shuttle intersects with a rocket, the asteroid or space shuttle gets destroyed. If an asteroid intersects with a space shuttle, the space shuttle gets destroyed. After detecting collisions, the Referee tests if the player has won or lost the game. He wins if no asteroids are left on the GameBoard. He loses if the space shuttle gets destroyed. In these cases, or if the stopGame () operation is invoked, the control thread and the game get stopped. Class: SpaceShuttleController The SpaceShuttleController listens to keyboard and mouse events. Depending on the events, space shuttle operations are invoked, to change the direction and speed or to fire a new rocket. Page 3
Recommend
More recommend