Just wondering if anyone could help me with the collision method in this program
ID: 3736242 • Letter: J
Question
Just wondering if anyone could help me with the collision method in this program. It doesn't stop the game, like it should when both the runner and the chaser collide.
import java.util.*;
import java.awt.*;
public class SurvivorI {
public static void main(String[] args) {
System.out.println("Project 2 written by Oscar Ortiz");
// 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, 800);
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);
// Initialize positions of runner and chaser.
Point runner = new Point(200, 400);
Point chaser = new Point(600, 400);
// Variable for input from user to move runner.
char keyInput = ' ';
// The 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 pixels each time step.
int moveSize = 10;
// Display players using Color.GREEN and Color.RED (or whatever colors you want).
// Wait one second before start of game.
panel.sleep(1000);
// NEED TO PUT SOME OF THESE STATEMENTS IN A FOR LOOP
// Erase players from the panel using Color.WHITE.
for (int i=0; i<=300; i++) {
erasePlayers(panel, runner, chaser);
// Get input from user if any.
char newKeyInput = panel.getKeyChar();
if (newKeyInput == 'w' || newKeyInput == 'a' || newKeyInput == 's' || newKeyInput == 'd') {
keyInput = newKeyInput;
}
legalMove(runner,chaser);
// Move the players according to parameters.
movePlayers(runner, chaser, keyInput, boxSize, moveSize);
// Display players using Color.GREEN and Color.RED (or whatever colors you want).
displayPlayers(panel, runner, chaser);
// Game is over if the chaser catches up to the runner.
if (true == collision(runner, chaser)) {
System.out.println("YOU LOST!!!");
// panel.close();
break;
}
// Wait sleepTime ms between moves.
panel.sleep(sleepTime);
// NEED AN IF STATEMENT TO DETERMINE WHEN TO PRINT THIS.
if (i == 300 && false == collision(runner, chaser)) {
System.out.println("YOU WON!!!");
}
}
}
public static void displayPlayers(DrawingPanel panel, Point runner, Point chaser) {
Graphics g = panel.getGraphics();
g.setColor(Color.GREEN);
g.fillRect(runner.x,runner.y,10,10);
g.setColor(Color.RED);
g.fillRect(chaser.x,chaser.y,10,10);
}
public static void erasePlayers(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);
}
public static void movePlayers(Point runner, Point chaser, char keyInput, int boxSize, int moveSize){
if (keyInput == 'a'){
runner.translate(-moveSize, 0);
}
if(keyInput == 'w'){
runner.translate(0, -moveSize);
}
if (keyInput == 's'){
runner.translate(0, moveSize);
}
if (keyInput == 'd'){
runner.translate(moveSize, 0);
}
chaserMovement(runner,chaser,moveSize);
}
public static double distanceMethod(Point point1, Point point2){
return Math.sqrt(Math.pow(point1.getX() - point2.getX(),2) + Math.pow(point1.getY() - point2.getY(),2));
}
public static void chaserMovement(Point runner, Point chaser, int moveSize){
double distanceW = distanceMethod(runner, new Point(chaser.x,chaser.y-10));
double distanceA = distanceMethod(runner, new Point(chaser.x-10,chaser.y));
double distanceS = distanceMethod(runner, new Point(chaser.x, chaser.y+10));
double distanceD = distanceMethod(runner, new Point(chaser.x+10,chaser.y));
if(distanceW<=distanceA && distanceW<=distanceS && distanceW<=distanceD){
chaser.translate(0, -(moveSize-1));
}
if(distanceA <=distanceS && distanceA <= distanceD && distanceA<=distanceW){
chaser.translate(-(moveSize-1),0);
}
if(distanceS <= distanceD && distanceS<= distanceA && distanceS<=distanceW){
chaser.translate(0,(moveSize-1));
}
else if(distanceD<=distanceA && distanceD<=distanceW && distanceD<=distanceS){
chaser.translate((moveSize-1),0);
}
}
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;
}
}
public static boolean collision(Point runner, Point chaser){
if(chaser.getX() <= runner.getX()+11 && chaser.getY() <= runner.getY()){
return true;
}
else {
return false;
}
}
}
I am having trouble getting the collision method to actually stop the game. Help would be appreciated. Thanks!
Explanation / Answer
import java.util.Scanner; public class Test { public static void main (String args[]) { int player_safe = 0; int cpu_safe = 0; int player_temp = 0; int cpu_temp = 0; boolean cpu_turn = false; boolean player_turn = false; Scanner name = new Scanner(System.in) ; System.out.println ( "Enter your name : " ) ; String player = name.nextLine(); System.out.println ( "Dice Game" ) ; System.out.println ( "RULES" ) ; System.out.println ( "" ) ; System.out.println ( "If a 1 is rolled player's turn ends and earns no points." ) ; System.out.println ( "If player chooses to hold, player will gain all the points in that turn and loose turn." ) ; System.out.println ( "" ) ; System.out.println ( "To determine who will start the game " +player+ " and CPU will roll a dice. The one who rolls higher will start the game." ) ; System.out.println ( "" ) ; System.out.println ( "If you are ready to roll a dice press 1." ) ; // ROLL A DICE TO DETERMINE WHO WILL START THE GAME int dice; int dice_player = 0; int dice_cpu = 0; Scanner begin = new Scanner(System.in); int player_roll = begin.nextInt(); if (player_roll == 1 ) { for (int i = 0; i < 1; i++ ) { dice_player = (int) (Math.random()*6+1) ; dice_cpu = (int) (Math.random()*6+1) ; System.out.println ( "You rolled " +dice_player ) ; System.out.println ( "CPU rolled " +dice_cpu ); if ( dice_player > dice_cpu) { System.out.println ("Player starts the game."); cpu_turn = false; player_turn = true; } else if ( dice_playerRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.