GAMES TWO:
LESSON Twenty: Color Train: Using Generic Classes

Color Train uses the four generic classes created for the last lesson (with only very slight changes to a couple of them). In fact, the difference between Color Train and the last version of Truck Dodge probably comes down to about 40 lines of modified code. (You should spend time hunting for places where the code differs.)
Here are the classes used for Color Train:
  1. GENERIC_CELL
  2. GENERIC_ENEMY
  3. GENERIC_GRID
  4. GENERIC_CELL_GAME
  5. TRAINGRID
  6. TRAIN
  7. TRAIN_GAME

NOTE: Take time to experiment with this game. In this game, you can move the player in four directions. To score you cause a collision between the player (blue) and a cell in a train which is blue. A collision between the player and a non-blue cell causes a deduction of points.

Here are a few of the changes which were made to the generic classes:

  1. GENERIC_CELL: moveUp() method added.
  2. GENERIC_ENEMY: change to check_collision to consider color.
  3. GENERIC_GRID: change made to move_player to allow vertical movement.
  4. GENERIC_CELL_GAME: change made to events to allow vertical movement.
  5. TRAINGRID (which extends GENERIC_GRID): most important change made in crash() method to facilitate new scoring rules.
  6. TRAIN (which extends GENERIC_ENEMY): changes made to increase length of enemy and to randomize cell color assignments.
  7. TRAIN_GAME (which extends GENERIC_CELL_GAME): only very superficial changes made.


ASSIGNMENT:

Make the following changes to this applet:

  1. If a train gets by without being scored (makes it to the bottom of the grid), deduct a point from the score.
  2. Figure out why you can't move up in the left-most column and fix this problem.
  3. Allow the player to horizontally wrap around the grid. (That is, when the player reaches the left-most side of the grid, a left-arrow press will wrap the player to the extreme right side of the grid... and vice versa.)
  4. Declare a win for the player at 20 points and a loss at -10. Stop the game on a win or a loss. Provide a reset to allow replay.
  5. Ensure that no two adjacent cells of the train are of the same color.