Question: Hello. I'm currently working on a Project for my Java course and have made a lot of progress. The program compiles successfully and the animation
Hello. I'm currently working on a Project for my Java course and have made a lot of progress. The program compiles successfully and the animation responds to my keystrokes as required. Unfortunately, the game does not end when the two objects collide, as it should. I've written my collision methods and am unsure of where to go from here. I'll also include the DrawingPanel file, incase the issue lies there. (The DrawingPanel file will be located after a few rows of ///// and is its own SEPARATE java file)
import java.util.*; import java.awt.*;
public class SurvivorII { public static void main(String[] args) { Scanner numScan = new Scanner(System.in); int boxSize = 760; DrawingPanel panel = new DrawingPanel(800, 800); Graphics g = panel.getGraphics(); char keyInput = ' '; int sleepTime = 100; int moveSize = 10; panel.sleep(sleepTime); char newKeyInput = panel.getKeyChar(); if (newKeyInput == 'w' || newKeyInput == 'a' || newKeyInput == 's' || newKeyInput == 'd') { keyInput = newKeyInput; } Point runner = new Point(200, 400); Point chaser = new Point(600, 400);
g.fillRect(10, 10, 10, boxSize - 20); g.fillRect(10, 10, boxSize - 20, 10); g.fillRect(boxSize - 20, 10, 10, boxSize - 20); g.fillRect(10, boxSize - 20, boxSize - 20, 10);
// using green and red displayPlayers(panel, runner, chaser);
// one sec panel.sleep(1000);
for(int i = 1; i<= 300; i++) { // having issues here im not sure why if (i != 300) { erasePlayers(panel, runner, chaser);
// Get input from user if any. char validKeyInput = panel.getKeyChar(); if (validKeyInput == 'w' || validKeyInput == 'a' || validKeyInput == 's' || validKeyInput == 'd') { keyInput = validKeyInput; }
// 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. panel.sleep(sleepTime); } else if(collision(runner, chaser)) { runner.translate(0, 0); chaser.translate(0, 0); System.out.println(" YOU LOST!!!"); break; }
else{ System.out.println(" YOU WON!!!"); runner.translate(0, 0); chaser.translate(0, 0); break; } } } public static void displayPlayers(DrawingPanel panel, Point runner, Point chaser){ Graphics draw = panel.getGraphics(); draw.setColor(Color.GREEN); draw.fillRect(runner.x, runner.y, 10, 10); draw.setColor(Color.RED); draw.fillRect(chaser.x, chaser.y, 10, 10); }
public static void erasePlayers(DrawingPanel panel, Point runner, Point chaser){ Graphics draw = panel.getGraphics(); draw.setColor(Color.WHITE); draw.fillRect(runner.x, runner.y, 10, 10); draw.fillRect(chaser.x, chaser.y, 10, 10); }
//if( && runner.x >= moveSize * 3 && runner.y <= boxSize && runner.y >= moveSize * 3 ){ public static void movePlayers(Point runner, Point chaser, char keyInput, int boxSize, int moveSize) { int chaserMoveSize = moveSize - 1; int chaserChange = 0;
if (keyInput == 'w' && runner.y >= 30) runner.translate(0, -moveSize); else if (keyInput == 's' && runner.y <= boxSize - 30) runner.translate(0, moveSize); else if (keyInput == 'a' && runner.x >= 30) runner.translate(-moveSize, 0); else if (keyInput == 'd' && runner.x <= boxSize - 30) runner.translate(moveSize, 0);
for(int i = 1; i<=4; i++) { int xChaser = 0; int yChaser = 0;
if (i == 1) xChaser += 10; else if (i == 2) xChaser -= 10; else if (i == 3) yChaser += 10; else yChaser -= 10;
double sPoint = distanceBetween(chaser, runner); double dPoint = distanceBetween(new Point(chaser.x + xChaser, chaser.y + yChaser), runner);
if (i == 1 && dPoint < sPoint) chaserChange = 1; else if (i == 2 && dPoint < sPoint) chaserChange = 2; else if (i == 3 && dPoint < sPoint) chaserChange = 3; else if (i == 4 && dPoint < sPoint) chaserChange = 4; }
if (chaserChange == 1) chaser.translate(chaserMoveSize, 0); else if(chaserChange == 2) chaser.translate(-chaserMoveSize, 0); else if (chaserChange == 3) chaser.translate(0, chaserMoveSize); else chaser.translate(0,-chaserMoveSize); }
public static double distanceBetween(Point runner, Point chaser){ double xSubSquared = Math.pow(chaser.getX() - runner.getX(), 2); double ySubSquared = Math.pow(chaser.getY() - runner.getY(), 2); return Math.sqrt(xSubSquared + ySubSquared);
}
public static boolean collision(Point runner, Point chaser){ if(distanceBetween(runner, chaser) <= 10) return true; else return false; }
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; import java.util.ArrayList;
public class DrawingPanel implements ActionListener { private static final String versionMessage = "Drawing Panel version 1.1, January 25, 2015"; private static final int DELAY = 100; // delay between repaints in millis private static final boolean PRETTY = false; // true to anti-alias private static boolean showStatus = false; private static final int MAX_KEY_BUF_SIZE = 10; private int width, height; // dimensions of window frame private JFrame frame; // overall window frame private JPanel panel; // overall drawing surface private BufferedImage image; // remembers drawing commands private Graphics2D g2; // graphics context for painting private JLabel statusBar; // status bar showing mouse position private volatile MouseEvent click; // stores the last mouse click private volatile boolean pressed; // true if the mouse is pressed private volatile MouseEvent move; // stores the position of the mouse private ArrayList
// return panel width public int getWidth() { return width; }
// return panel height public int getHeight() { return height; } // return the X position of the mouse or -1 public int getMouseX() { if (move == null) { return -1; } else { return move.getX(); } } // return the Y position of the mouse or -1 public int getMouseY() { if (move == null) { return -1; } else { return move.getY(); } } // return the X position of the last click or -1 public int getClickX() { if (click == null) { return -1; } else { return click.getX(); } } // return the Y position of the last click or -1 public int getClickY() { if (click == null) { return -1; } else { return click.getY(); } } // return true if a mouse button is pressed public boolean mousePressed() { return pressed; } public synchronized int getKeyCode() { if (keys.size() == 0) return 0; return keys.remove(0).keyCode; } public synchronized char getKeyChar() { if (keys.size() == 0) return 0; return keys.remove(0).keyChar; } public synchronized int getKeysSize() { return keys.size(); } private synchronized void insertKeyData(char c, int code) { keys.add(new KeyInfo(c,code)); if (keys.size() > MAX_KEY_BUF_SIZE) { keys.remove(0); // System.out.println("Dropped key"); } } private class KeyInfo { public int keyCode; public char keyChar; public KeyInfo(char keyChar, int keyCode) { this.keyCode = keyCode; this.keyChar = keyChar; } } private class DrawingPanelKeyListener implements KeyListener { int repeatCount = 0; public DrawingPanelKeyListener() { panel.addKeyListener(this); panel.requestFocus(); } public void keyPressed(KeyEvent event) { // System.out.println("key pressed"); repeatCount++; if ((repeatCount == 1) || (getKeysSize() < 2)) insertKeyData(event.getKeyChar(),event.getKeyCode()); } public void keyTyped(KeyEvent event) { } public void keyReleased(KeyEvent event) { repeatCount = 0; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
