GAMES TWO: LESSON Eighteen: Truck Dodge: Reworking the Code
Truck dodge reuses the cell class used in the next to last CANTRIS
lesson and it uses a modified GRID class with an applet class which is
pretty much the same as that used in the last two CANTRIS lessons.
The following classes are used in this version of Truck Dodge:
- RETRUCK (applet class)
- GRID5 (grid class)
- TRUCK_CLASS (truck class)
- CELL3 (cell class)
The major change in this version of Truck Dodge is that a Vector is used to
keep track of the trucks. Here is the declaration of the Vector from the
GRID5 class:
Vector truck_vector= new Vector();
The following methods from the vector class are used in GRID5:
get(int index) - returns the object stored at the index
lastElement() - returns the last object stored in vector
size() - returns the number of elements stored in vector
remove(int index) - removes object stored at specified index
trimToSize() - trims size of vector to number of objects stored in it
addElement() - adds element to end of vector
The get(int) and lastElement() method of the Vector class return objects and
so it is necessary to caste these objects so they will be recognized as the
proper type of object. Here's a line where casting is performed:
TRUCK_CLASS holder = (TRUCK_CLASS) truck_vector.get(i);
ASSIGNMENT:
In addition to the changes you made in the last assignment, make the
following changes to this version of Truck Dodge (using the Vector class):
- allow six trucks to be on screen at once
- ensure that no two consecutive cars are in the same lane
- create a class to handle movement of the car