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:
- GENERIC_CELL
- GENERIC_ENEMY
- GENERIC_GRID
- GENERIC_CELL_GAME
- TRAINGRID
- TRAIN
- 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:
- GENERIC_CELL: moveUp() method added.
- GENERIC_ENEMY: change to check_collision to consider color.
- GENERIC_GRID: change made to move_player to allow vertical
movement.
- GENERIC_CELL_GAME: change made to events to allow vertical
movement.
- TRAINGRID (which extends GENERIC_GRID): most important change made in
crash() method to facilitate new scoring rules.
- TRAIN (which extends GENERIC_ENEMY): changes made to increase length of
enemy and to randomize cell color assignments.
- TRAIN_GAME (which extends GENERIC_CELL_GAME): only very superficial
changes made.
|
ASSIGNMENT:
Make the following changes to this applet:
- If a train gets by without being scored (makes it to the bottom of the
grid), deduct a point from the score.
- Figure out why you can't move up in the left-most column and fix this
problem.
- 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.)
- 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.
- Ensure that no two adjacent cells of the train are of the same
color.