Question: Java Programming a game with chaser and zombies. Note: Current code I have is found at the very bottom. Lab 9 and Project 3 are
Java Programming a game with chaser and zombies.
Note: Current code I have is found at the very bottom.
Lab 9 and Project 3 are the same thing, no need for 2 seperate programs. Please use code at very bottom and build onto it.
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 four times in the while loop. 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.
Rubric for Lab 9
Your program should compile without any errors. A program with more than one or two compile errors will likely get a zero for the whole assignment.
The following criteria will also be used to determine the grade for Lab 9.
General [2 points]
Your submission was a Zip file named lab9.zip containing at least the two files called SurvivorII.java and DrawingPanel.java.
Your program contains a comment that describes what the program does and contains a descriptive comment for each method.
Your program is indented properly.
Your program displays your name appropriately.
Creation of zombies [6 points]
[2 points] The user can enter the desired number of zombies.
[2 points] The correct number of zombies appear in the game. The color of the zombies is different from the runner.
[2 points] The zombies appear in locations that are randomly generated.
Movement of the zombies [6 points]
[4 points] The zombies move in a random walk.
[2 points] The zombies do not move out of the box.
Movement of runner [4 points]
[2 points] The user can control the runner using the WASD keys.
[2 points] The runner cannot move into the box boundary or move outside the box.
End of the game [2 points]
[2 points] The game ends when the runner collides with any zombie.
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!.
Rubric for Project 3
Your program should compile without any errors. A program with more than one or two compile errors will likely get a zero for the whole assignment.
The following criteria will also be used to determine the grade for Project 2.
The program satisfies the rubric for Lab 9 [10 points]
See the Lab 9 rubric above.
User-Specified Values [4 points]
[2 points] The speed of the runner and the chaser is determined by the move size and sleep time parameters entered by the user.
[2 points]The size of the panel and the box and the initial positions of the runner and chaser are determined by the box size entered by the user. Also, all the zombies are in the box.
Movement of the chaser [2 points]
[2 points] The chaser always moves toward the runner. The chaser is a different color from the runner and zombies. The chaser is slightly slower than the runner. If there is a collision between the runner and the chaser, the game immediately ends and results in a YOU LOSE!!! message.
Eliminating zombies [4 points]
[2 points] When the runner collides with a zombie, the zombie is eliminated from the game.
[2 points]When the runner has eliminated all the zombies, the game immediately ends with a YOU WIN!!! message.
Current code:
import java.util.*; import java.awt.*; import javax.swing.JOptionPane; // i added this to make it look nicer when you win/lose
public class SurvivorI { public static void main(String[] args) { // Create DrawingPanel and draw a box in the panel. // The box is a square of this size. int boxSize = 760; DrawingPanel panel = new DrawingPanel(800, 700); Graphics g = panel.getGraphics(); g.fillRect(10, 10, 10, 780); g.fillRect(10, 10, 780, 10); g.fillRect(780, 10, 10, 780); g.fillRect(10, 780, 780, 10); // Initalize positions of runner and chaser. Point runner = new Point(200, 400); Point chaser = new Point(600, 400);
boolean runGame = true; // Variable for input from user to move runner. char keyInput = ' '; // This program should wait sleepTime ms between moves. int sleepTime = 100; // The runner should move moveSize (or zero) pixels each time step. // the chaser should move moveSize - 1 pizels each time step. int moveSize = 10; //this calls the showRunnerChaser method so the box will show the players showRunnerChaser(panel, runner, chaser); //how long program will sleep for before showing player and chaser panel.sleep(10); //this calls the movePlayers method so the user input will move the runner movePlayers(runner, chaser, keyInput, boxSize, moveSize); int count = 0; while (runGame = true) { showRunnerChaser(panel, runner, chaser); char newKeyInput = panel.getKeyChar(); if (newKeyInput == 'w' || newKeyInput == 'a' || newKeyInput == 's' || newKeyInput == 'd') { keyInput = newKeyInput; goAwayDemon(panel, runner, chaser); makeEfficient(runner,chaser,moveSize); movePlayers(runner, chaser, keyInput, boxSize, moveSize); legalMove(runner, chaser); count++; } if(count == 300) { // win message if moves = 300 JOptionPane.showMessageDialog (null, "\"YOU WON!!\"", "Result", JOptionPane.INFORMATION_MESSAGE); panel.close(); break; } if(true==collision(runner, chaser)) { // lose message if the chaser gets the runner JOptionPane.showMessageDialog (null, "\"YOU LOST!!\"", "Result", JOptionPane.INFORMATION_MESSAGE); panel.close(); break; } } // end of the loop that runs the game } // end of main method
// this method displays the movements of the runner and chaser public static void showRunnerChaser(DrawingPanel panel, Point runner, Point chaser) { Graphics g = panel.getGraphics(); g.setColor(Color.BLUE); g.fillRect(runner.x, runner.y, 10, 10); g.setColor(Color.RED); g.fillRect(chaser.x, chaser.y, 10, 10); }
// this method will delete the previous moves of the runner and chaser so the user will know // the exact position and not lose sight of the moves public static void goAwayDemon(DrawingPanel panel, Point runner, Point chaser) { Graphics g = panel.getGraphics(); g.setColor(Color.WHITE); g.fillRect(runner.x, runner.y, 10, 10); g.setColor(Color.WHITE); g.fillRect(chaser.x, chaser.y, 10, 10); } //added this code to make the point between runner and chaser closest as possible public static void makeEfficient(Point runner,Point chaser,int moveSize){ // calculates the distance between runner and chaser double distance = calcDistance(runner,chaser); double distw = calcDistance(runner,new Point(chaser.x,chaser.y-10)); double dista = calcDistance(runner,new Point(chaser.x-10,chaser.y)); double dists = calcDistance(runner,new Point(chaser.x,chaser.y+10)); double distd = calcDistance(runner,new Point(chaser.x+10,chaser.y)); if(distw<=dista && distw<=dists && distw<=distd){ chaser.translate(0, -moveSize); } else if(dista<=distw && dista<=dists && dista<=distd){ chaser.translate(-moveSize, 0); } else if(dists<=distw && dists<=dista && dists<=distd){ chaser.translate(0, moveSize); } else if(distd<=distw && distd<=dista && distd<=dists){ chaser.translate(moveSize, 0); } }
// formula to get the distance public static double calcDistance(Point runner,Point chaser){ return ((runner.x-chaser.x)*(runner.x-chaser.x))+((runner.y-chaser.y)*(runner.y-chaser.y)); } // moves the players using the moveSize command when user uses WASD keys public static void movePlayers(Point runner, Point chaser, char keyInput, int boxSize, int moveSize) { if (keyInput == 'w') { runner.translate(0, -moveSize); } if (keyInput == 's') { runner.translate(0, moveSize); } if (keyInput == 'a') { runner.translate(-moveSize, 0); }
if (keyInput == 'd') { runner.translate(moveSize, 0); } }
// code to make sure that moves of runner and chaser public static void legalMove(Point runner, Point chaser) { if (runner.x >= 780 - 20) { runner.x = 780 - 20; }
if (runner.x <= 10 + 20) { runner.x = 10 + 20; }
if (runner.y >= 780 - 20) { runner.y = 780 - 20; }
if (runner.y <= 10 + 20) { runner.y = 10 + 20; } if (chaser.x >= 780 - 20) { chaser.x = 780 - 20; }
if (chaser.x <= 10 + 20) { chaser.x = 10 + 20; }
if (chaser.y >= 780 - 20) { chaser.y = 780 - 20; }
if (chaser.y <= 10 + 20) { chaser.y = 10 + 20; }
} // checks to see if chaser and runner collide with eachother private static boolean collision(Point runner, Point chaser) {
if((runner.getX()==chaser.getX())&&(runner.getY()==chaser.getY())) return true; return false; } } // end of program
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
