|
RHS |
Assignment 01
Here is a version of the WormChase game that are stripped for all gathering of statistics (including score and everything). The worm can't use wrap-around: Obstacles.java, Worm.java, WormChase.java and WormPanel.java.
Now you're going to add a class, Mines, that is a dangerous kind of obstacle.
If the worm hits a mine the game is over as it explodes!
Here's the class-diagram that basicly shows that Mines is known by WormPanel :
![]() |
Class Mines
It would be nice to extend the Obstacles class as a mine is-a special kind of obstacle (it explodes!).
Unfortunately the Obstacles class is very porly designed for inheritance (variables are private, but as there is no get-methods the sub-classes will inherit variables they can't access). Let that be a lesson!
So - the easiest way is actually to copy the Obstacles.java into Mines.java and then do minor corrections.
But having a mine class is just first step. Below is recommandations for what you should do to make the mines a part of the game.
if (!fred.touchedAt(x,y)) // was the worm's body untouched?
if (Math.random() < 0.2) {
obs.add(x,y);
} else {
min.add(x,y);
}//if-else
20% of the mouse-clicks gives an obstacle, 80% gives a mine.
private void gameUpdate() {
if (!isPaused && !gameOver) {
fred.move();
gameOver = min.hits(fred);
}
} // end of gameUpdate()
This will effectively stop the game if the worms move made the head touch a mine!