Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA PROGRAM HELP I need to create a method for a program that makes the chaser (the red square) chase the runner (green square) for

JAVA PROGRAM HELP

I need to create a method for a program that makes the chaser (the red square) chase the runner (green square) for a certain amount of time. If the chaser never reaches the runner in x amount of time, the game stops and a "YOU WON!!!" appears.

I am mainly struggling with creating a method that makes the chaser chase the runner. Right now the square does not move. This is the only issue to my program that I have so far.

If you need more information on the assignment as a whole it is found here: http://www.cs.utsa.edu/~cs1063/projects/index.html (Project 2: Survivor I) And the subsection that the question I'm asking is coming from the "movePlayers Method" subsection

This is my code so far

import java.util.*; import java.awt.*; import javax.swing.JOptionPane; 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); // Initialize positions of runner and chaser.  Point runner = new Point(200, 400); Point chaser = new Point(600, 400); boolean gameState = true; // 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; // Wait one second before start of game.  panel.sleep(1000); // 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); // NEED TO PUT SOME OF THESE STATEMENTS IN A FOR LOOP  while (gameState = true) { displayPlayers(panel, runner, chaser); char newKeyInput = panel.getKeyChar(); if (newKeyInput == 'w' || newKeyInput == 'a' || newKeyInput == 's' || newKeyInput == 'd') { keyInput = newKeyInput; erasePlayers(panel, runner, chaser); legalMove(runner, chaser); movePlayers(runner, chaser, keyInput, boxSize, moveSize); } if(true==isReached(runner, chaser)) { JOptionPane.showMessageDialog (null, "\"YOU LOST!!!\"", "Result", JOptionPane.INFORMATION_MESSAGE); panel.close(); break; } } } /*Method that displays the runner and chaser in the graphics panel. Green and red.*/  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); } /*Method that turns the runner and chaser white*/  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 == '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); } } 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; } } private static boolean isReached(Point runner, Point chaser) { //check runner and chaser points are equal or not  if((runner.getX()==chaser.getX())&&(runner.getY()==chaser.getY())) return true;//if equal return true  return false;//other wise false  } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions

Question

Question Can a Roth IRA invest in stock of the IRA owners business?

Answered: 1 week ago