GAMES TWO:
LESSON Nineteen:
Truck Dodge: Creating Generic Classes

The idea behind this lesson is to decompose Truck Dodge into general classes, some of which are specific to the game and some which can be extended in different ways for use by different games which can be built with generic cells and a generic grid playing area. There are seven classes used here; four are generic and three are game-specific.
The following classes are used in this version of Truck Dodge:
    GENERIC CLASSES:
  • GENERIC_CELL - Defines basic cell as a 10x10 grid with a color as specified. Contains the following methods:
    1. moveX(int) - moves cell 10 units to the right or the left.
    2. moveDown() - moves cell 2 units down.
    3. moveUp() - moves cell 2 units up.
    4. draw(Graphics) - draws cell.
  • GENERIC_ENEMY - Defines stack of three cells with colors as specified. Contains the following methods:
    1. moveDown() - moves enemy cells down 2 units (through call to GENERIC_CELL).
    2. getY(int) - returns current y value.
    3. check_collision(int,int) - checks for collision.
    4. draw(Graphics) - draws enemy cells.
  • GENERIC_GRID - Defines basic grid with player, but without enemy. Contains the following methods:
    1. getScore() - returns score.
    2. move_player(int) - changes position of player.
    3. keepPlaying() - returns true or false indicating game state.
    4. endGame() - changes game state to false.
    5. draw(Graphics) - draws basic grid and player.
  • GENERIC_CELL_GAME - This Applet class implements the Runnable interface and controls basic game tasks. Contains the following methods:
    1. init() - basic game set up and key events.
    2. reset() - reset game.
    3. run() - game thread control.
    4. update(Graphics) - standard.
    5. paint(Graphics) - draws basic grid components and player.
  • GAME-SPECIFIC CLASSES:
  • TRUCK_ENEMY - Extends GENERIC_ENEMY. It adds only a constructor which specifies the appearance of the enemy.
  • GENGRID - Extends GENERIC_GRID and adds game-specific behavior. Contains the following methods:
    1. spawn() - creates new instances of TRUCK_ENEMY.
    2. move_enemy() - moves enemy down 2.
    3. drawEnemies() - draws enemies.
    4. crash() - calls check_collision() and returns true if collision occurs.
    5. draw(Graphics) - calls super.draw and then calls drawEnemies.
  • GENTRUCK - Extends GENERIC_CELL_GAME. All it really accomplishes is to set the game title in the init() method.


ASSIGNMENT:

In addition to all the features added in the last two lessons, change the scoring method: The highway is divided into six lanes. Allow the player to score a point each time the car is moved all the way across the highway in either direction. Also ensure that the game is restarted only when the r key is pressed.