I am making snake in javafx, I have completed my game, and I am trying to add a reset button in my game. When I click the reset key (ENTER), the game should reset, but the arraylist that stores the rectangles that make up the snake does not reset. For the life of me, I cannot figure out how to make it work. Any help is appreciated, thanks!
Code: https://pastebin.com/yiQi8DSU
Code:
package finalproject;
public class FinalProject extends Application {
Random rand = new Random();
Pane canvas;
Scene scene;
double points1[] = {100, 100, 1200, 100, 1200, 900, 100, 900,};
Polygon poly1;
Text Score, HighScore, GameOver, Reset, Time;
Font font1, font2;
Rectangle SnakeBody, Food, EndScreen, SpeedBoost, ScoreMultiplier;
ArrayList<Rectangle> Snakes;
int RIGHT = 0, UP = 1, LEFT = 2, DOWN = 3;
int facing = 0;
int rightSpeed = 0, upSpeed = 0, leftSpeed = 0, downSpeed = 0;
int SnakeSpeed = 4;
int score = 0;
int Multiplier = 0;
int Booster = 0;
int Score2 = 0;
//int timeCount = 2000;
// long second = 1000000000;
public void Objects() {
facing = RIGHT;
Food = new Rectangle(600, 600, 20, 20);
Food.setStroke(Color.RED);
Food.setFill(Color.RED);
SpeedBoost = new Rectangle(4000, 4000, 20, 20);
SpeedBoost.setStroke(Color.WHITE);
SpeedBoost.setFill(Color.PURPLE);
ScoreMultiplier = new Rectangle(4000, 4000, 15, 15);
ScoreMultiplier.setStroke(Color.WHITE);
ScoreMultiplier.setFill(Color.CHARTREUSE);
}
public void reset() {
SnakeBody.setX(150);
SnakeBody.setY(150);
Food.setX(600);
Food.setY(600);
rightSpeed = 0;
upSpeed = 0;
leftSpeed = 0;
downSpeed = 0;
SnakeSpeed = 4;
if (canvas.getChildren().contains(SpeedBoost)) {
canvas.getChildren().remove(SpeedBoost);
}
if (canvas.getChildren().contains(ScoreMultiplier)) {
canvas.getChildren().remove(ScoreMultiplier);
}
score = 0;
Score.setText("Score: " + score);
Snakes.clear();
Objects();
;
}
private class Timer extends AnimationTimer {
@Override
public void handle(long l) {
// Timer timer2 = new Timer();
Shape overlap2 = Shape.intersect(SnakeBody, SpeedBoost);
if (overlap2.getBoundsInLocal().getWidth() > 0 || overlap2.getBoundsInLocal().getHeight() > 0) {
SpeedBoost.setX(4000); //Moves the object outside of the canvas.
SpeedBoost.setY(4000);
Booster = 10;
}
Shape overlap3 = Shape.intersect(SnakeBody, ScoreMultiplier);
if (overlap3.getBoundsInLocal().getWidth() > 0 || overlap3.getBoundsInLocal().getHeight() > 0) {
ScoreMultiplier.setX(4000); //Moves the object outside of the canvas.
ScoreMultiplier.setY(4000);
Multiplier = 10;
}
//Snake moving lines
if (rightSpeed > 0 && facing != RIGHT) {
facing = RIGHT;
} else {
for (int i = Snakes.size() - 1; i > 0; i--) {
Snakes.get(i).setX(Snakes.get(i - 1).getX());
Snakes.get(i).setY(Snakes.get(i - 1).getY());
}
}
SnakeBody.setX(SnakeBody.getX() + rightSpeed - leftSpeed);
SnakeBody.setY(SnakeBody.getY() + downSpeed - upSpeed);
//Boundaries for the snake
if (SnakeBody.getX() <= 100 || SnakeBody.getY() <= 100) {
if (!canvas.getChildren().contains(EndScreen)) {
canvas.getChildren().addAll(EndScreen, GameOver);
}
}
if (SnakeBody.getX() >= 1170 || SnakeBody.getY() >= 870) {
if (!canvas.getChildren().contains(EndScreen)) {
canvas.getChildren().addAll(EndScreen, GameOver);
}
}
Shape overlap = Shape.intersect(SnakeBody, Food);
if (overlap.getBoundsInLocal().getWidth() > 0 || overlap.getBoundsInLocal().getHeight() > 0) {
Food.setX(rand.nextInt(1200));
Food.setY(rand.nextInt(900));
if (Multiplier == 5) {
Score2 = Score2 + 1;
Score.setText("Score: " + Score2);
} else {
score = score + 1;
Score.setText("Score: " + score);
}
if (score > 5) {
if (!canvas.getChildren().contains(SpeedBoost)) {
SpeedBoost.setX(350);
SpeedBoost.setY(350);
canvas.getChildren().add(SpeedBoost);
}
}
if (score >= 4) {
if (!canvas.getChildren().contains(ScoreMultiplier)) {
ScoreMultiplier.setX(100);
ScoreMultiplier.setY(100);
canvas.getChildren().add(ScoreMultiplier);
}
}
if (leftSpeed > 0) {
for (int i = 0; i < 5; i++) { //Adds 5x the rectangles, preventing the slow growing.
Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY(), 30, 30);
Snakes.add(r);
r.setFill(Color.GOLD);
r.setStroke(Color.WHITE);
canvas.getChildren().addAll(r);
}
}
if (rightSpeed > 0) {
for (int i = 0; i < 5; i++) {
Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY(), 30, 30);
Snakes.add(r);
r.setFill(Color.GOLD);
r.setStroke(Color.WHITE);
canvas.getChildren().addAll(r);
}
}
if (upSpeed > 0) {
for (int i = 0; i < 5; i++) {
Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY(), 30, 30);
Snakes.add(r);
r.setFill(Color.GOLD);
r.setStroke(Color.WHITE);
canvas.getChildren().addAll(r);
}
}
if (downSpeed > 0) {
for (int i = 0; i < 5; i++) {
Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY(), 30, 30);
Snakes.add(r);
r.setFill(Color.GOLD);
r.setStroke(Color.WHITE);
canvas.getChildren().addAll(r);
}
}
}
if (Food.getX() <= 100) {
Food.setX(rand.nextInt(1200));
}
if (Food.getY() <= 100) {
Food.setY(rand.nextInt(900));
}
if (Food.getX() >= 1170) {
Food.setX(rand.nextInt(1200));
}
if (Food.getY() >= 870) {
Food.setY(rand.nextInt(900));
}
}
}
public void start(Stage primaryStage) {
canvas = new Pane();
scene = new Scene(canvas, 1300, 1300, Color.BLACK);
Objects();
/*Shape overlap5 = Shape.intersect(SnakeBody, Food);
if (overlap5.getBoundsInLocal().getWidth() > 0 || overlap5.getBoundsInLocal().getHeight() > 0) {
timeCount = timeCount - 1;
Time = new Text(700, 60, "Time Survived: " + timeCount);
Time.setFont(font1);
Time.setFill(Color.YELLOW);
}*/
EndScreen = new Rectangle(1300, 1300);
EndScreen.setFill(Color.BLACK);
poly1 = new Polygon(points1);
poly1.setFill(Color.DARKBLUE);
poly1.setStroke(Color.GREY);
poly1.setStrokeWidth(3);
font1 = Font.font("Arial", 24);
font2 = Font.font("Arial", 48);
GameOver = new Text(500, 500, "Game Over");
GameOver.setFont(font2);
GameOver.setFill(Color.RED);
//Reset = new Text(650, 650, "You survived for: " + timeCount);
Score = new Text(250, 60, "Score: " + score);
Score.setFont(font1);
Score.setFill(Color.YELLOW);
HighScore = new Text(700, 60, "High Score: ");
HighScore.setFont(font1);
HighScore.setFill(Color.YELLOW);
canvas.getChildren().addAll(poly1, Score, Food);
Snakes = makeSnakeBodies();
canvas.getChildren().addAll(Snakes);
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent evt) {
if (Multiplier == 10) {
Score2 = score * 2; //Multiplies score
Multiplier = 5; //Any value other than 10
}
if (Booster == 10) {
if (evt.getCode().equals(KeyCode.Q)) {
SnakeSpeed = 15;
}
if (evt.getCode().equals(KeyCode.A)) {
SnakeSpeed = 4;
}
}
if (evt.getCode().equals(KeyCode.ENTER)) {
reset();
//AnimationTimer class1 = new Timer(); // Code resets the Objects() method but not the animation timer class
}
if (evt.getCode().equals(KeyCode.RIGHT) && leftSpeed == 0) {
rightSpeed = SnakeSpeed;
upSpeed = 0;
leftSpeed = 0;
downSpeed = 0;
} else if (evt.getCode().equals(KeyCode.UP) && downSpeed == 0) {
upSpeed = SnakeSpeed;
rightSpeed = 0;
leftSpeed = 0;
downSpeed = 0;
} else if (evt.getCode().equals(KeyCode.DOWN) && upSpeed == 0) {
downSpeed = SnakeSpeed;
rightSpeed = 0;
leftSpeed = 0;
upSpeed = 0;
} else if (evt.getCode().equals(KeyCode.LEFT) && rightSpeed == 0) {
leftSpeed = SnakeSpeed;
rightSpeed = 0;
upSpeed = 0;
downSpeed = 0;
}
}
});
primaryStage.setTitle("Snake");
primaryStage.setScene(scene);
primaryStage.show();
Timer timer = new Timer();
timer.start();
}
public ArrayList<Rectangle> makeSnakeBodies() {
ArrayList<Rectangle> joints = new ArrayList<>();
int x = 150;
int y = 150;
SnakeBody = new Rectangle(x, y, 30, 30);
SnakeBody.setFill(Color.GOLD);
SnakeBody.setStroke(Color.WHITE);
joints.add(SnakeBody);
return joints;
}
}
If you need specific sections of code, I can post them separately. Thanks again!