Advertisement
Guest User

Untitled

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