Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication33;
- import java.util.ArrayList;
- import java.util.Random;
- import javafx.animation.AnimationTimer;
- import javafx.application.Application;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.input.KeyCode;
- import javafx.scene.input.KeyEvent;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Polygon;
- import javafx.scene.shape.Rectangle;
- import javafx.scene.shape.Shape;
- import javafx.scene.text.Font;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- public class JavaApplication33 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;
- 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;
- public void Objects() {
- //SnakeBody = new Rectangle(300, 300, 30, 30);
- //SnakeBody.setFill(Color.GOLD);
- //SnakeBody.setStroke(Color.WHITE);
- facing = RIGHT;
- Food = new Rectangle(600, 600, 20, 20);
- Food.setStroke(Color.RED);
- Food.setFill(Color.RED);
- SpeedBoost = new Rectangle(350, 350, 20, 20);
- SpeedBoost.setStroke(Color.WHITE);
- SpeedBoost.setFill(Color.PURPLE);
- ScoreMultiplier = new Rectangle(100, 100, 15, 15);
- ScoreMultiplier.setStroke(Color.WHITE);
- ScoreMultiplier.setFill(Color.CHARTREUSE);
- }
- private class Timer extends AnimationTimer {
- @Override
- public void handle(long l) {
- 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)) {
- canvas.getChildren().add(SpeedBoost);
- }
- }
- if (score >= 4) {
- if (!canvas.getChildren().contains(ScoreMultiplier)) {
- canvas.getChildren().add(ScoreMultiplier);
- }
- }
- if (leftSpeed > 0) {
- Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX() + 30, 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) {
- Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX() - 30, 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) {
- Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY() + 30, 30, 30);
- Snakes.add(r);
- r.setFill(Color.GOLD);
- r.setStroke(Color.WHITE);
- canvas.getChildren().addAll(r);
- }
- if (downSpeed > 0) {
- Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY() - 30, 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();
- 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);
- 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, HighScore, 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)) {
- 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);
- makeSnakeBodies().clear();
- ArrayList<Rectangle> joints = new ArrayList<>();
- //AnimationTimer class1 = new Timer(); // Code resets the Objects() method but not the animation timer class
- }
- if (evt.getCode().equals(KeyCode.RIGHT)) {
- rightSpeed = SnakeSpeed;
- upSpeed = 0;
- leftSpeed = 0;
- downSpeed = 0;
- } else if (evt.getCode().equals(KeyCode.UP)) {
- upSpeed = SnakeSpeed;
- rightSpeed = 0;
- leftSpeed = 0;
- downSpeed = 0;
- } else if (evt.getCode().equals(KeyCode.DOWN)) {
- downSpeed = SnakeSpeed;
- rightSpeed = 0;
- leftSpeed = 0;
- upSpeed = 0;
- } else if (evt.getCode().equals(KeyCode.LEFT)) {
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement