Question
Java program - Repost of question with entire code as text. Sorry, I know it's not an elegant/good code - I'm just starting, this is
Java program - Repost of question with entire code as text. Sorry, I know it's not an elegant/good code - I'm just starting, this is my first try at creating a simple game in java.
I am trying to find a way to restart the mainscript, if the player crashes. So it would say "You crashed. Press space to return to menu." When I try to insert it, the loop causes it to reload the menu infinitely.
import java.util.ArrayList;
import java.util.List;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.*;
public class RaceTrack {
final static int X = 0;
final static int Y = 1;
final static int n = 40; // Track size
final static int r1 = 9, r2 = 3, r3 = 1, r4 = 7; // Radius of turns in track 2
final static int x1 = r1+1, x2 = n/2, x3 = n-(r1+1); // x- values for tracks
final static int y1 = r1+1, y2 = n-2*r4-2*r3, y3 = x3; // y-values for tracks
static List lineCoordinates = new ArrayList(); // List with all the lines between dots
static int moves; // Count the number of moves used to finish the course
static int[] pos = new int[2]; // The current position
static int[] oldPos = new int[2]; // The previous position
static int[] distance = new int[2]; // Vector between old position and new position
static boolean reverse; // Traces which way around track is used.
static boolean middleCrossed; // Checkpoint halfway through tracks
static boolean restart;
public static void main(String[] args) throws InterruptedException {
middleCrossed = false; // Checkpoint has not been crossed from start
reverse = false; // Assume clockwise movement
restart = false;
pos[X] = (int)(n/2.0); // Implements start position
pos[Y] = n-4;
distance[0] = 0; // Implements distance of zero
distance[1] = 0;
Menu(); // Runs menu
while(!StdDraw.mousePressed()) { // Waits for mouse click
int corX = (int)Math.round(StdDraw.mouseX()); // Read position of mouse
int corY = (int)Math.round(StdDraw.mouseY());
// Implements track 1, if first button is pressed
if (StdDraw.mousePressed() && (corX >= n/4-3 && corX = n/2-1 && corY
Track1();
do {
if (Crash1(pos[X],pos[Y])) { // Adds loop for new input if position is within the track
Keys();
if (!middleCrossed && pos[X] > n/4 && pos[X]
middleCrossed = true;
reverse = pos[X] - oldPos[X] > 0; // Checks if moves are counterclockwise
} else if ((middleCrossed && !reverse && pos[X] >=n/2 && pos[Y] > n-n/4) || (middleCrossed && reverse && pos[X] n-n/4)) {
PenOption(6);
StdDraw.text(n/2, n/2, "You Finished!"); // Breaks loop and shows the number of moves used for
PenOption(7); // completing the track.
StdDraw.text(n/2, n/3, "You used " + moves + " moves.");
break;
}
} else {
StdDraw.text(n/2, n/2, "You crashed!");
// Breaks loop if position hits or passes the outline of the track
break;
}
} while (true);
// Implements track 1, if second button is pressed
} else if (StdDraw.mousePressed() && (corX >= n-n/4-3 && corX = n/2-1 && corY
Track2();
do {
if (Crash2(pos[X],pos[Y])) { // Adds loop for new input if position is within the track
Keys();
if (!middleCrossed && pos[X] > n/4 && pos[X]
middleCrossed = true;
reverse = pos[X] - oldPos[X] > 0; // Checks if moves are counterclockwise
} else if ((middleCrossed && !reverse && pos[X] >=n/2 && pos[Y] > n-n/4) || (middleCrossed && reverse && pos[X] n-n/4)) {
PenOption(6);
StdDraw.text(n/2, n/2, "You Finished!"); // Breaks loop and shows the number of moves used for
PenOption(7); // completing the track.
StdDraw.text(n/2, n/3, "You used " + moves + " steps.");
break;
}
} else {
PenOption(6);
StdDraw.text(n/2, n/2, "You crashed!"); // Breaks loop if position hits or passes the outline of the track
break;
}
} while (true);
}
}
}
public static void Size() { // Set size of the track and window
StdDraw.setCanvasSize(800, 800); // Adjust the size of the window
StdDraw.setScale(0,n); // Set size of the coordinate system
}
public static void PenOption(int s) { // Pen-sizes used
if (s == 1) {
StdDraw.setPenColor(StdDraw.WHITE);
Font font = new Font("Arial", Font.BOLD, 48);
StdDraw.setFont(font);
} else if (s == 2) {
StdDraw.setPenColor(StdDraw.BLACK);
Font font = new Font("Arial", Font.BOLD, 32);
StdDraw.setFont(font);
} else if (s == 3) {
Font font = new Font("Arial", Font.BOLD, 80);
StdDraw.setFont(font);
StdDraw.setPenColor(StdDraw.BOOK_RED);
} else if (s == 4) {
StdDraw.setPenColor(StdDraw.GREEN);
StdDraw.setPenRadius(4/((double)n*10.0));
} else if (s == 5) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(9.0/((double)n*50.0));
} else if (s == 6) {
StdDraw.setPenColor(StdDraw.BOOK_RED);
Font font = new Font("Arial", Font.BOLD, 80);
StdDraw.setFont(font);
} else if (s == 7) {
StdDraw.setPenColor(StdDraw.BOOK_RED);
Font font = new Font("Arial", Font.BOLD, 50);
StdDraw.setFont(font);
} else if (s == 8) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(4.0/((double)n*10.0));
} else if (s == 9) {
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.setPenRadius(8.0/((double)n*100.0));
}
}
public static void Menu() { // Implements start menu
Size(); // Sets size
StdDraw.clear();
StdDraw.setPenColor(StdDraw.FORREST_GREEN); // Background
StdDraw.filledSquare(n/2.0, n/2.0, n/2.0);
StdDraw.setPenColor(StdDraw.PINK); // Buttons
StdDraw.filledRectangle(n/4, n/2, 6, 2);
StdDraw.filledRectangle(n-n/4, n/2, 6, 2);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.rectangle(n/4, n/2, 6, 2);
StdDraw.rectangle(n-n/4, n/2, 6, 2);
PenOption(1); // Prompt for choice of track
StdDraw.text( n/2, n-n/4, "Please choose track");
PenOption(2); // Text on buttons
StdDraw.text( n/4, n/2, "Track 1");
StdDraw.text( n-n/4, n/2, "Track 2");
}
public static void drawDot(int x, int y) { // Function for drawing dots
PenOption(8);
StdDraw.point(x, y);
}
public static void drawLines(int[] pos, int[] oldPos) { // Function for drawing lines
lineCoordinates.add(new Integer[] {oldPos[X], oldPos[Y], pos[X], pos[Y]}); // Add coordinates of positions to list
for(Integer[] line : lineCoordinates) { // For-loop, so lines between all points are drawn
StdDraw.setPenColor(StdDraw.MAGENTA);
StdDraw.setPenRadius(3.0/((double)n*10.0));
StdDraw.line(line[0], line[1], line[2], line[3]); // Draw lines between positions
drawDot(line[0],line[1]); // Draw previous dots
}
drawDot(pos[X],pos[Y]); // Draw dot at current position
}
public static void Distance() { // Calculates the vector between positions
distance[X] = pos[X] - oldPos[X];
distance[Y] = pos[Y] - oldPos[Y];
}
public static void Update() throws InterruptedException {
moves ++; // Counts number of moves
drawLines(pos,oldPos); // Draw lines between points
drawDot(pos[X], pos[Y]); // Draw the current position
Distance(); // Update the vector before next input
Thread.sleep(150); // Ensures key is not pressed twice by accident
}
public static void Keys() throws InterruptedException { // Implements the numeric keys as controls
if (StdDraw.isKeyPressed(KeyEvent.VK_1)) { // Key 1 is implemented
oldPos = pos.clone(); // Saves current position as oldPos
pos[X] += distance[X] - 1; // Updates coordinates for current position
pos[Y] += distance[Y] - 1;
Update(); // Updates made before next input
} else if ( StdDraw.isKeyPressed(KeyEvent.VK_2)) { // Key 2 is implemented
oldPos = pos.clone();
pos[X] += distance[X];
pos[Y] += distance[Y] - 1;
Update();
} else if ( StdDraw.isKeyPressed(KeyEvent.VK_3)) { // Key 3 is implemented
oldPos = pos.clone();
pos[X] += distance[X] +1;
pos[Y] += distance[Y] -1;
Update();
} else if ( StdDraw.isKeyPressed(KeyEvent.VK_4)) { // Key 4 is implemented
oldPos = pos.clone();
pos[X] += distance[X] - 1;
pos[Y] += distance[Y];
Update();
} else if ( StdDraw.isKeyPressed(KeyEvent.VK_5)) { // Key 5 is implemented
oldPos = pos.clone();
pos[X] += distance[X];
pos[Y] += distance[Y];
Update();
} else if (StdDraw.isKeyPressed(KeyEvent.VK_6)) { // Key 6 is implemented
oldPos = pos.clone();
pos[X] += distance[X] + 1;
pos[Y] += distance[Y];
Update();
} else if (StdDraw.isKeyPressed(KeyEvent.VK_7)) { // Key 7 is implemented
oldPos = pos.clone();
pos[X] += distance[X] - 1;
pos[Y] += distance[Y] + 1;
Update();
} else if (StdDraw.isKeyPressed(KeyEvent.VK_8)) { // Key 8 is implemented
oldPos = pos.clone();
pos[X] += distance[X];
pos[Y] += distance[Y] + 1;
Update();
} else if (StdDraw.isKeyPressed(KeyEvent.VK_9)) { // Key 9 is implemented
oldPos = pos.clone();
pos[X] += distance[X] + 1;
pos[Y] += distance[Y] + 1;
Update();
}
}
public static boolean Crash1(int x, int y) { // Checks if current position is within track 1
PenOption(3);
boolean Crash1 = false;
if (x >= n/4 && x = n/4 && y
Crash1 = false; // a crash is registered
} else if (x >= n || x = n || y
Crash1 = false;
} else {
Crash1 = true; // Accepts position as within the track
}
return Crash1;
}
public static boolean Crash2(int x, int y) { // Checks if current position is within track 2
PenOption(3);
double Euklid1 = Math.hypot(x3-x, y3-y); // Euclidean distance is calculated for each corner
double Euklid2 = Math.hypot(x3-x, y1-y);
double Euklid3 = Math.hypot(x2-x, y2-y);
double Euklid4 = Math.hypot(x1-x, y1-y);
double Euklid5 = Math.hypot(x1-x, y3-y);
boolean Crash2 = false;
if (x = x1 && y y3+r2) { // Ensures position is within track at all straight
Crash2 = true; // sections of the track
} else if (x > 1 && x = y1) {
Crash2 = true;
} else if (x > x3+r2 && x = y1) {
Crash2 = true;
} else if ( x > x2+r3 && x = y1 && y
Crash2 = true;
} else if (x > x2-r4 && x = y1 && y
Crash2 = true;
} else if (Euklid1 > r2 && Euklid1
Crash2 = true;
} else if (Euklid2 > r2 && Euklid2
Crash2 = true;
} else if (Euklid3 > r3 && Euklid3
Crash2 = true;
} else if (Euklid4 > r2 && Euklid4
Crash2 = true;
} else if (Euklid5 > r2 && Euklid5
Crash2 = true;
} else {
Crash2 = false; // Crash is registered if conditions are not met
}
return Crash2;
}
public static void Grid(int n) { // Draw grid
StdDraw.setPenRadius(4.0/((double)n*100.0));
for(int i = 1; i
StdDraw.line(i, 0, i, n); // Vertical
StdDraw.line(0, i, n, i); // Horizontal
}
}
public static void Track1() {
Size();
StdDraw.clear(); // Clears canvas for menu
StdDraw.setPenColor(StdDraw.LIGHT_GRAY); // Sets track
StdDraw.filledSquare(n/2.0, n/2.0, n/2.0);
PenOption(5); // Draw perimeter of track
StdDraw.square(n/2.0, n/2.0,n/2.0);
Grid(40); // Draw grid
PenOption(4); // Draw start line / finish line
StdDraw.line(n/2.0, (3.0*n)/6.0, n/2.0, n);
PenOption(9); // Draw inner circle
StdDraw.filledSquare(n/2.0, n/2.0, n/4.0);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.square(n/2.0, n/2.0, n/4.0);
drawDot(pos[X],pos[Y]); // Draw starting position
}
public static void Track2() {
Size();
StdDraw.clear();
StdDraw.setPenColor(StdDraw.PINK); // Draw the large circles
StdDraw.filledCircle(x1, y3, r1);
StdDraw.filledCircle(x3, y3, r1);
StdDraw.filledCircle(x3, y1, r1);
StdDraw.filledCircle(x1, y1, r1);
StdDraw.setPenColor(StdDraw.WHITE); // Draw the small circles to create corners
StdDraw.filledCircle(x1, y3, r2);
StdDraw.filledCircle(x3, y3, r2);
StdDraw.filledCircle(x3, y1, r2);
StdDraw.filledCircle(x1, y1, r2);
StdDraw.filledRectangle(x2, n-2*r2, (x3-x1)/2, n/4); // Removes excess gray areas
StdDraw.filledRectangle(x2, x2, n/2, n/4);
StdDraw.setPenColor(StdDraw.PINK); // Draw middle circle
StdDraw.filledCircle(x2, y2, r4);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledCircle(x2, y2, r3); // remove inner circle
StdDraw.filledRectangle(x2, x2, r3, r1/2); // Removes excess gray area
StdDraw.setPenColor(StdDraw.PINK); // Draw all straight sections of the track
StdDraw.filledRectangle(x2, n-4, (x3-x1)/2, (r1-r2)/2);
StdDraw.filledRectangle(x1-r1+r2, n/2, (r1-r2)/2, (y3-y1)/2);
StdDraw.filledRectangle(x2-r4+r2, (y2-y1)/2+y1, (r1-r2)/2, (y2-y1)/2);
StdDraw.filledRectangle(x2+r4-r2, (y2-y1)/2+y1, (r1-r2)/2, (y2-y1)/2);
StdDraw.filledRectangle(n-r2-r3, n/2, (r1-r2)/2, (y3-y1)/2);
PenOption(5); // Draw perimeter of track
StdDraw.square(n/2.0, n/2.0,n/2.0);
Grid(n); // Draw grid
StdDraw.setPenRadius(4/((double)n*20.0));
StdDraw.setPenColor(StdDraw.BLACK);
// Outline of the track
StdDraw.arc( x1, y1, r1, 180, 0); // First u-turn
StdDraw.arc( x1, y1, r2, 180, 0);
StdDraw.arc( x2, y2, r4, 360, 180); // Second u-turn
StdDraw.arc( x2, y2, r3, 360, 180);
StdDraw.arc( x3, y1, r1, 180, 360); // Third u-turn
StdDraw.arc( x3, y1, r2, 180, 360);
StdDraw.arc( x1, y3, r1, 90, 180); // Top corners
StdDraw.arc( x1, y3, r2, 90, 180);
StdDraw.arc( x3, y3, r1, 0, 90);
StdDraw.arc( x3, y3, r2, 0, 90);
StdDraw.line( x1-r1, y1, x1-r1, y3); // Lines
StdDraw.line( x1-r2, y1, x1-r2, y3);
StdDraw.line( x3+r1, y1, x3+r1, y3);
StdDraw.line( x3+r2, y1, x3+r2, y3);
StdDraw.line( x3, n-1, x1, n-1);
StdDraw.line( x3, y3+r2, x1, y3+r2);
StdDraw.line( x1+r2, y1, x2-r4, y2);
StdDraw.line( x1+r1, y1, x2-r3, y2);
StdDraw.line( x2+r3, y2, x3-r1, y1);
StdDraw.line( x2+r4, y2, x3-r2, y1);
PenOption(4); // Draw start line / finish line
StdDraw.line(n/2.0, n-1, n/2.0, y3+r2);
drawDot(pos[X],pos[Y]); // Draw starting position
}
}
0 public static void main(String args) throws InterruptedExceptionf 31 32 middlecrossed = false reverse = false; restart = false pos[X) = (int)(n/2.0); pos[] = n-4; distance[0] = 0; distance[1] 0; // Checkpoint has not been crossed from start 34 35 36 37 38 // Assume clockwise movement // Implements start position / Implements distance of zero Runs menu Waits for mouse click 41 43 45 47 49 whileCIStdDraw.mousePressed)) int int corx = corV = (int)Math.round(StdDraw . mouseK)); (int)Math.round(StdDraw .mouseYO) // Read position of mouse // Implements track 1, if first button is pressed Track10 if (Crash1CposDXI,posCY) // Adds loop for new input if position is within the track Keys); if (middleCrossed && posXn/4 && posCXIn-n/4 && posCMn-n/4) / Checks is checkpoint is crossed 52 53 middleCrossed true; reverse -posX- oldPosM 0; // Checks if moves are counterclockwise Cm1ddleCrossed&L } else (CmiddleCrossed && ! reverse && pos[X) >-n/2 && pos[ n-n/4) 11 reverse && posLX) > // Breaks loop and shows the number of moves used for // completing the track 57 59 61 63 65 67 69 StdDraw. text(n/2, n/2, "You crashed!); // Breaks loop if position hits or passes the outline of the track break 3 while (true); Implements track 1, if second button is pressed else if (StdDraw.mousePressed) && (corX- n-n/4-3 && corX nn/4+3 & corYn/2-1 &&corYn/2+1)) Track2); 72 if (Crash2(posLXI,posM)) t 74 75 76 // Adds loop for new input if position is within the track // // Checks if moves are counterclockwise Keys); if (!middleCrossed && pos[X) > n/4 && pos[X) 0; } ((middleCrossed && ! reverse && pos[X) >=n/2 && posM n-n/4) il n-n/4)) { if Penoption(6); StdDraw. text(n/2, n/2, "You Finished!; Penoption(7); StdDraw.text(n/2, n/3, "You used"movessteps." break 78 else > (middleCrossed && reverse && pos[X) // Breaks loop and shows the number of moves used for 81 // completing the track 83 84 85 86 87 3 else Penoption(6); StdDraw.text(n/2, n/2, "You crashed!" break; // Breaks loop if position hits or passes the outline of the track 89 3 while (true); 91 92 93Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started