Objectives

Objectives include:

  • Call and write methods with parameters and return values.
  • Use while loops.
  • Use arrays.

Introduction

In this project you will add more features to the program you developed for Project 2. Lab 9 will be to implement some of the the new features. Project 3 will be to implement all the new features.

As in Project 2, this game will have a runner and a chaser. The new element in this game is that the game will include zombies. A zombie moves in a random walk (see Lab 7). The object of the game is for the runner to eliminate all the zombies (by colliding with them) and to avoid being caught by the chaser. The runner wins when all the zombies are eliminated. The runner loses if the chaser catches the runner before all the zombies have been eliminated.

Running the starter code for SurvivorII.java shows how the game works with one zombie and no chaser. Note that you also need DrawingPanel.java for this project. Also note that the game does not start until the user has entered a move.

Lab 9

Lab 9 is for partially implementing this game.

The goal of Lab 9 is to implement the game with a user-specifed number of zombies. Lab 9 does not require a chaser. Lab 9 also does not require user-specified values for box size, move size, and sleep time. You may implement these elements in Lab 9 if you wish, but credit for them will be given in your Project 3 score.

Lab 9 should prompt the user for the number of zombies as follows:

Enter the number of zombies: 10

Suppose this value is stored in numZombies. Then your program will need to create an array of Points:

Point[] zombies = new Point[numZombies];

Each element of the array needs to initialized to a Point object. If i is an int that is greater or equal to 0, and is less than numZombies, then the following initializes the array at index i to a Point with coordinates X=600 and Y=400:

zombies[i] = new Point(600,400);

This needs to be done for every value of i from 0 to numZombies-1.

Rather than putting each zombie at the same location, the coordinates should be random.

The X and Y coordinates for each zombie should be random values between 30 and 770.

In the starter code, the variable zombie appears in the while loop and in methods. Because there are numZombies zombies, each of these appearances need to be replaced with a loop over the zombies array:

for (int i = 0; i < numZombies; i++) {
Point zombie = zombies[i];
// do stuff to zombie
}

In Lab 9, a collision between the runner and any zombie should result in setting gameOver to true. In Project 3, we will worry about making sure that all the zombies are eliminated before the game is over.

Project 3

Project 3 will finish the game. As in Project 2, the game needs a chaser and user-specified values for box size, move size, and sleep time. Also, all the zombies must be eliminated for the user to win the game.

Chaser

As in Project 2, the chaser should be a Point object starting at a fixed location.

Point chaser = new Point(600,200);

The chaser should be displayed using a different color from the runner and zombies.

displayPlayer(chaser, Color.RED);

A move by the chaser means using the translate method to change the X or Y coordinate of the chaser, but not both. A single move of moveSize - 1 pixels must be up, down, left, or right. For example, chaser.translate(0, -(moveSize - 1)) would move the chaser up. The move of the chaser is determined by choosing the move (out of the four possible moves) that gets the chaser chosest to the runner.

User-Specified Values

The starter code currently has fixed values for the size of the box, the size of each move, and amount of sleep time each iteration. The user should be asked for these values as well as the number of zombies using a single prompt.

Enter the number of zombies, box size, move size, and sleep time: 10 760 10 100

If the user enters a 0 or negative number, use the default value for that variable. Other magic numbers in the starter code might need to replaced with appropriate expressions to take the user's inputs into account. For the same reason, methods in the starter code might need more parameters.

There needs to be a chaser that always follows the runner. See the Project 2 description for how the chaser should work.

Eliminating Zombies

For each zombie, the program needs to remember whether it is eliminated or not. This can be done using a boolean array.

boolean[] eliminated = new boolean[numZombies];

Java initializes each element of this array to false. When zombies[i] has been eliminated, then element i of the eliminated array needs to be set to true.

eliminated[i] = true;

If zombies[i] has been eliminated, then that zombie should not be displayed on the window. This can be controlled with an if statement.

if (! eliminated[i]) {
displayZombie(panel, zombies[i]);
}

End of the Game

The game should end if the chaser collides with the runner. In this case, the program should print YOU LOSE!

The game should also end if the runner eliminates all the zombies. This requires checking that every value in the eliminated array has been set to true. If any value in the eliminated array is false (and if the chaser does not collide with the runner), then the game should continue. If the runner eliminates all the zombies, then the program should print YOU WIN!.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.