Advertisement
Guest User

Untitled

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