Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. Stage window;
  2. Scene mainScene;
  3.  
  4. private final int WIDTH = 500;
  5. private final int HEIGHT = 500;
  6.  
  7. Timeline timeline;
  8.  
  9. private Direction action = Direction.RIGHT;
  10.  
  11. private Rectangle snakeHead;
  12. private final int speed = 3;
  13. private int xSpeed = 3;
  14. private int ySpeed = 3;
  15.  
  16. private int snakeW = 20;
  17. private int snakeH = 20;
  18. private BoundingBox snakeBox;
  19.  
  20. private BoundingBox foodBox;
  21.  
  22. private ImageView food;
  23. private Random rand = new Random();
  24.  
  25. private double foodX;
  26. private double foodY;
  27.  
  28. enum Direction {
  29. LEFT,RIGHT,UP,DOWN;
  30. }
  31.  
  32. private Parent createContent(){
  33. Pane root = new Pane();
  34.  
  35. //food = new Food();
  36. food = new ImageView(new Image("resources/apple.png"));
  37. food.setFitHeight(25);
  38. food.setFitWidth(25);
  39. food.setPreserveRatio(true);
  40. newFood();
  41. foodBox = new BoundingBox(foodX,foodY,20,20);
  42.  
  43.  
  44. snakeHead = new Rectangle(snakeW,snakeH);
  45. snakeHead.setTranslateX(200);
  46. snakeHead.setTranslateY(200);
  47.  
  48. snakeBox = new BoundingBox(snakeHead.getTranslateX(),snakeHead.getTranslateY(),snakeW,snakeH);
  49.  
  50. timeline = new Timeline(new KeyFrame(Duration.millis(16), e-> {
  51. //Snake movement
  52. if(action == Direction.LEFT) {snakeHead.setTranslateX(snakeHead.getTranslateX() - xSpeed);}
  53. if(action == Direction.RIGHT) {snakeHead.setTranslateX(snakeHead.getTranslateX() + xSpeed);}
  54. if(action == Direction.UP) {snakeHead.setTranslateY(snakeHead.getTranslateY() - ySpeed);}
  55. if(action == Direction.DOWN) {snakeHead.setTranslateY(snakeHead.getTranslateY() + ySpeed);}
  56.  
  57. //Stops snake at edges of screen
  58. if(snakeHead.getTranslateX() <= 0){
  59. xSpeed = 0;
  60. if(action == Direction.RIGHT){xSpeed = speed;}
  61. }
  62. if(snakeHead.getTranslateX() >= WIDTH - snakeW){
  63. xSpeed = 0;
  64. if(action == Direction.LEFT){xSpeed = speed;}
  65. }
  66. if(snakeHead.getTranslateY() <= 0){
  67. ySpeed = 0;
  68. if(action == Direction.DOWN){ySpeed = speed;}
  69. }
  70. if(snakeHead.getTranslateY() >= HEIGHT - snakeH){
  71. ySpeed = 0;
  72. if(action == Direction.UP){ySpeed = speed;}
  73. }
  74.  
  75. //TODO: Detect Collisions
  76. if(foodBox.intersects(snakeHead.getTranslateX(),snakeHead.getTranslateY (),snakeW,snakeH)){
  77. newFood();
  78. System.out.println("Collision");
  79. }
  80.  
  81. }));
  82.  
  83.  
  84.  
  85. timeline.setCycleCount(Timeline.INDEFINITE);
  86.  
  87. root.getChildren().addAll(snakeHead,food);
  88.  
  89. return root;
  90. }
  91.  
  92. private void newFood() {
  93. foodX = rand.nextInt(500);
  94. foodY = rand.nextInt(500);
  95.  
  96. food.setTranslateX(foodX);
  97. food.setTranslateY(foodY);
  98.  
  99. System.out.println("X " + foodX);
  100. System.out.println("Y " + foodY);
  101. }
  102.  
  103. private void startGame() {
  104. timeline.play();
  105. }
  106.  
  107. private void stopGame() {
  108. timeline.stop();
  109. }
  110.  
  111.  
  112. @Override
  113. public void start(Stage primaryStage) throws Exception{
  114. window = primaryStage;
  115. mainScene = new Scene(createContent(),WIDTH,HEIGHT);
  116.  
  117. mainScene.setOnKeyPressed(e-> {
  118. switch(e.getCode()) {
  119. case UP: action = Direction.UP; break;
  120. case DOWN: action = Direction.DOWN; break;
  121. case LEFT: action = Direction.LEFT; break;
  122. case RIGHT: action = Direction.RIGHT; break;
  123. }
  124. });
  125.  
  126. window.setTitle("Snake");
  127. window.setResizable(false);
  128. window.setScene(mainScene);
  129. window.show();
  130.  
  131. startGame();
  132. }
  133.  
  134.  
  135. public static void main(String[] args) {
  136. launch(args);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement