Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.67 KB | None | 0 0
  1. package finalproject;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import javafx.animation.AnimationTimer;
  6. import javafx.application.Application;
  7. import javafx.event.EventHandler;
  8. import javafx.scene.Scene;
  9. import javafx.scene.input.KeyCode;
  10. import javafx.scene.input.KeyEvent;
  11. import javafx.scene.layout.Pane;
  12. import javafx.scene.paint.Color;
  13. import javafx.scene.shape.Polygon;
  14. import javafx.scene.shape.Rectangle;
  15. import javafx.scene.shape.Shape;
  16. import javafx.scene.text.Font;
  17. import javafx.scene.text.Text;
  18. import javafx.stage.Stage;
  19.  
  20. public class FinalProject extends Application {
  21.  
  22.     Random rand = new Random();
  23.     Pane canvas;
  24.     Scene scene;
  25.     double points1[] = {100, 100, 1200, 100, 1200, 900, 100, 900,};
  26.     Polygon poly1;
  27.     Text Score, HighScore, GameOver, Reset, Time;
  28.     Font font1, font2;
  29.     Rectangle SnakeBody, Food, EndScreen, SpeedBoost, ScoreMultiplier;
  30.     ArrayList<Rectangle> Snakes;
  31.     int RIGHT = 0, UP = 1, LEFT = 2, DOWN = 3;
  32.     int facing = 0;
  33.     int rightSpeed = 0, upSpeed = 0, leftSpeed = 0, downSpeed = 0;
  34.     int SnakeSpeed = 4;
  35.     int score = 0;
  36.     int Multiplier = 0;
  37.     int Booster = 0;
  38.     int Score2 = 0;
  39.     int timeCount = 0;
  40.  
  41.     public void Objects() {
  42.  
  43.         facing = RIGHT;
  44.  
  45.         Food = new Rectangle(600, 600, 20, 20);
  46.         Food.setStroke(Color.RED);
  47.         Food.setFill(Color.RED);
  48.  
  49.         SpeedBoost = new Rectangle(4000, 4000, 20, 20);
  50.         SpeedBoost.setStroke(Color.WHITE);
  51.         SpeedBoost.setFill(Color.PURPLE);
  52.  
  53.         ScoreMultiplier = new Rectangle(4000, 4000, 15, 15);
  54.         ScoreMultiplier.setStroke(Color.WHITE);
  55.         ScoreMultiplier.setFill(Color.CHARTREUSE);
  56.  
  57.     }
  58.  
  59.     public void reset() {
  60.         SnakeBody.setX(150);
  61.         SnakeBody.setY(150);
  62.  
  63.         Food.setX(600);
  64.         Food.setY(600);
  65.  
  66.         rightSpeed = 0;
  67.         upSpeed = 0;
  68.         leftSpeed = 0;
  69.         downSpeed = 0;
  70.         SnakeSpeed = 4;
  71.  
  72.         if (canvas.getChildren().contains(SpeedBoost)) {
  73.             canvas.getChildren().remove(SpeedBoost);
  74.         }
  75.  
  76.         if (canvas.getChildren().contains(ScoreMultiplier)) {
  77.             canvas.getChildren().remove(ScoreMultiplier);
  78.         }
  79.  
  80.         score = 0;
  81.         Score.setText("Score: " + score);
  82.  
  83.         Snakes.clear();
  84.  
  85.         Snakes = new ArrayList<Rectangle>();
  86.         for (int i = Snakes.size() - 1; i > 0; i--) {
  87.  
  88.             Snakes.get(i).setX(Snakes.get(i - 1).getX());
  89.             Snakes.get(i).setY(Snakes.get(i - 1).getY());
  90.  
  91.         }
  92.  
  93.         Objects();
  94.  
  95.     }
  96.  
  97.     private class Timer extends AnimationTimer {
  98.  
  99.         @Override
  100.  
  101.         public void handle(long l) {
  102.  
  103.            
  104.            // timeCount = timeCount + 1; //Ignore, WIP
  105.            
  106.            
  107.            
  108.             Shape overlap2 = Shape.intersect(SnakeBody, SpeedBoost);
  109.  
  110.             if (overlap2.getBoundsInLocal().getWidth() > 0 || overlap2.getBoundsInLocal().getHeight() > 0) {
  111.  
  112.                 SpeedBoost.setX(4000); //Moves the object outside of the canvas.
  113.                 SpeedBoost.setY(4000);
  114.  
  115.                 Booster = 10;
  116.             }
  117.  
  118.             Shape overlap3 = Shape.intersect(SnakeBody, ScoreMultiplier);
  119.  
  120.             if (overlap3.getBoundsInLocal().getWidth() > 0 || overlap3.getBoundsInLocal().getHeight() > 0) {
  121.  
  122.                 ScoreMultiplier.setX(4000); //Moves the object outside of the canvas.
  123.                 ScoreMultiplier.setY(4000);
  124.  
  125.                 Multiplier = 10;
  126.  
  127.             }
  128.  
  129.             //Snake moving lines
  130.             if (rightSpeed > 0 && facing != RIGHT) {
  131.  
  132.                 facing = RIGHT;
  133.  
  134.             } else {
  135.  
  136.                 for (int i = Snakes.size() - 1; i > 0; i--) {
  137.  
  138.                     Snakes.get(i).setX(Snakes.get(i - 1).getX());
  139.                     Snakes.get(i).setY(Snakes.get(i - 1).getY());
  140.  
  141.                 }
  142.  
  143.             }
  144.  
  145.             SnakeBody.setX(SnakeBody.getX() + rightSpeed - leftSpeed);
  146.             SnakeBody.setY(SnakeBody.getY() + downSpeed - upSpeed);
  147.  
  148.             //Boundaries for the snake
  149.             if (SnakeBody.getX() <= 100 || SnakeBody.getY() <= 100) {
  150.  
  151.                 if (!canvas.getChildren().contains(EndScreen)) {
  152.                     canvas.getChildren().addAll(EndScreen, GameOver);
  153.  
  154.                 }
  155.  
  156.             }
  157.  
  158.             if (SnakeBody.getX() >= 1170 || SnakeBody.getY() >= 870) {
  159.  
  160.                 if (!canvas.getChildren().contains(EndScreen)) {
  161.                     canvas.getChildren().addAll(EndScreen, GameOver);
  162.  
  163.                 }
  164.             }
  165.  
  166.             Shape overlap = Shape.intersect(SnakeBody, Food);
  167.  
  168.             if (overlap.getBoundsInLocal().getWidth() > 0 || overlap.getBoundsInLocal().getHeight() > 0) {
  169.  
  170.                 Food.setX(rand.nextInt(1200));
  171.                 Food.setY(rand.nextInt(900));
  172.  
  173.                 if (Multiplier == 5) {
  174.  
  175.                     Score2 = Score2 + 1;
  176.                     Score.setText("Score: " + Score2);
  177.  
  178.                 } else {
  179.  
  180.                     score = score + 1;
  181.                     Score.setText("Score: " + score);
  182.  
  183.                 }
  184.  
  185.                 if (score > 5) {
  186.  
  187.                     if (!canvas.getChildren().contains(SpeedBoost)) {
  188.  
  189.                         SpeedBoost.setX(350);
  190.                         SpeedBoost.setY(350);
  191.  
  192.                         canvas.getChildren().add(SpeedBoost);
  193.  
  194.                     }
  195.                 }
  196.  
  197.                 if (score >= 4) {
  198.  
  199.                     if (!canvas.getChildren().contains(ScoreMultiplier)) {
  200.  
  201.                         ScoreMultiplier.setX(100);
  202.                         ScoreMultiplier.setY(100);
  203.  
  204.                         canvas.getChildren().add(ScoreMultiplier);
  205.                     }
  206.                 }
  207.  
  208.                 if (leftSpeed > 0) {
  209.  
  210.                     for (int i = 0; i < 5; i++) { //Adds 5x the rectangles, preventing the slow growing.
  211.  
  212.                         Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY(), 30, 30);
  213.                         Snakes.add(r);
  214.                         r.setFill(Color.GOLD);
  215.                         r.setStroke(Color.WHITE);
  216.                         canvas.getChildren().addAll(r);
  217.  
  218.                     }
  219.                 }
  220.  
  221.                 if (rightSpeed > 0) {
  222.  
  223.                     for (int i = 0; i < 5; i++) {
  224.  
  225.                         Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY(), 30, 30);
  226.                         Snakes.add(r);
  227.                         r.setFill(Color.GOLD);
  228.                         r.setStroke(Color.WHITE);
  229.  
  230.                         canvas.getChildren().addAll(r);
  231.                     }
  232.  
  233.                 }
  234.  
  235.                 if (upSpeed > 0) {
  236.  
  237.                     for (int i = 0; i < 5; i++) {
  238.                         Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY(), 30, 30);
  239.  
  240.                         Snakes.add(r);
  241.                         r.setFill(Color.GOLD);
  242.                         r.setStroke(Color.WHITE);
  243.                         canvas.getChildren().addAll(r);
  244.                     }
  245.  
  246.                 }
  247.  
  248.                 if (downSpeed > 0) {
  249.  
  250.                     for (int i = 0; i < 5; i++) {
  251.                         Rectangle r = new Rectangle(Snakes.get(Snakes.size() - 1).getX(), Snakes.get(Snakes.size() - 1).getY(), 30, 30);
  252.  
  253.                         Snakes.add(r);
  254.  
  255.                         r.setFill(Color.GOLD);
  256.                         r.setStroke(Color.WHITE);
  257.                         canvas.getChildren().addAll(r);
  258.                     }
  259.                 }
  260.             }
  261.  
  262.             if (Food.getX() <= 100) {
  263.                 Food.setX(rand.nextInt(1200));
  264.  
  265.             }
  266.  
  267.             if (Food.getY() <= 100) {
  268.                 Food.setY(rand.nextInt(900));
  269.  
  270.             }
  271.  
  272.             if (Food.getX() >= 1170) {
  273.                 Food.setX(rand.nextInt(1200));
  274.  
  275.             }
  276.  
  277.             if (Food.getY() >= 870) {
  278.                 Food.setY(rand.nextInt(900));
  279.  
  280.             }
  281.         }
  282.     }
  283.  
  284.     public void start(Stage primaryStage) {
  285.  
  286.         canvas = new Pane();
  287.         scene = new Scene(canvas, 1300, 1300, Color.BLACK);
  288.         Objects();
  289.  
  290.         EndScreen = new Rectangle(1300, 1300);
  291.         EndScreen.setFill(Color.BLACK);
  292.  
  293.         poly1 = new Polygon(points1);
  294.         poly1.setFill(Color.DARKBLUE);
  295.         poly1.setStroke(Color.GREY);
  296.         poly1.setStrokeWidth(3);
  297.  
  298.         font1 = Font.font("Arial", 24);
  299.         font2 = Font.font("Arial", 48);
  300.  
  301.         GameOver = new Text(500, 500, "Game Over");
  302.         GameOver.setFont(font2);
  303.         GameOver.setFill(Color.RED);
  304.  
  305.         Reset = new Text(650, 650, "You survived for: " + timeCount);
  306.  
  307.         Score = new Text(250, 60, "Score: " + score);
  308.         Score.setFont(font1);
  309.         Score.setFill(Color.YELLOW);
  310.  
  311.         Time = new Text(700, 60, "Time Survived: " + timeCount);
  312.         Time.setFont(font1);
  313.         Time.setFill(Color.YELLOW);
  314.  
  315.         HighScore = new Text(700, 60, "High Score: ");
  316.         HighScore.setFont(font1);
  317.         HighScore.setFill(Color.YELLOW);
  318.  
  319.         canvas.getChildren().addAll(poly1, Score, HighScore, Food);
  320.         Snakes = makeSnakeBodies();
  321.         canvas.getChildren().addAll(Snakes);
  322.  
  323.         scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
  324.  
  325.             public void handle(KeyEvent evt) {
  326.  
  327.                 if (Multiplier == 10) {
  328.  
  329.                     Score2 = score * 2; //Multiplies score
  330.  
  331.                     Multiplier = 5; //Any value other than 10
  332.  
  333.                 }
  334.  
  335.                 if (Booster == 10) {
  336.  
  337.                     if (evt.getCode().equals(KeyCode.Q)) {
  338.  
  339.                         SnakeSpeed = 15;
  340.  
  341.                     }
  342.  
  343.                     if (evt.getCode().equals(KeyCode.A)) {
  344.  
  345.                         SnakeSpeed = 4;
  346.  
  347.                     }
  348.                 }
  349.  
  350.                 if (evt.getCode().equals(KeyCode.ENTER)) {
  351.  
  352.                     reset();
  353.  
  354.                     //AnimationTimer class1 = new Timer(); // Code resets the Objects() method but not the animation timer class
  355.                 }
  356.  
  357.                 if (evt.getCode().equals(KeyCode.RIGHT) && leftSpeed == 0) {
  358.  
  359.                     rightSpeed = SnakeSpeed;
  360.                     upSpeed = 0;
  361.                     leftSpeed = 0;
  362.                     downSpeed = 0;
  363.  
  364.                 } else if (evt.getCode().equals(KeyCode.UP) && downSpeed == 0) {
  365.  
  366.                     upSpeed = SnakeSpeed;
  367.                     rightSpeed = 0;
  368.                     leftSpeed = 0;
  369.                     downSpeed = 0;
  370.  
  371.                 } else if (evt.getCode().equals(KeyCode.DOWN) && upSpeed == 0) {
  372.  
  373.                     downSpeed = SnakeSpeed;
  374.                     rightSpeed = 0;
  375.                     leftSpeed = 0;
  376.                     upSpeed = 0;
  377.  
  378.                 } else if (evt.getCode().equals(KeyCode.LEFT) && rightSpeed == 0) {
  379.  
  380.                     leftSpeed = SnakeSpeed;
  381.                     rightSpeed = 0;
  382.                     upSpeed = 0;
  383.                     downSpeed = 0;
  384.  
  385.                 }
  386.  
  387.             }
  388.  
  389.         });
  390.  
  391.         primaryStage.setTitle("Snake");
  392.         primaryStage.setScene(scene);
  393.         primaryStage.show();
  394.         Timer timer = new Timer();
  395.         timer.start();
  396.  
  397.        
  398.        
  399.        
  400.        
  401.     }
  402.  
  403.     public ArrayList<Rectangle> makeSnakeBodies() {
  404.  
  405.         ArrayList<Rectangle> joints = new ArrayList<>();
  406.  
  407.         int x = 150;
  408.         int y = 150;
  409.  
  410.         SnakeBody = new Rectangle(x, y, 30, 30);
  411.         SnakeBody.setFill(Color.GOLD);
  412.         SnakeBody.setStroke(Color.WHITE);
  413.         joints.add(SnakeBody);
  414.  
  415.         return joints;
  416.  
  417.     }
  418.  
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement