Newer
Older
package org.example.SnakeGame;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Random;
public class SnakeGame extends Application {
private final static Logger logger = LogManager.getLogger(SnakeGame.class);
//window
private static final int width = 600;
private static final int height = 500;
private static final int radius = 5;
//food
private Circle food;
private Random random;
private Pane root;
private Circle snake;
/** methode will create the food
* and display it randomly **/
private void newFood(){
food = new Circle(random.nextInt(width-2),random.nextInt(height-2), radius);
food.setFill(Color.RED);
root.getChildren().add(food);
}
private void newSnake(){
snake = new Circle(300,250, radius+2);
root.getChildren().add(snake);
}
@Override
public void start (Stage primaryStage) {
root = new Pane();
root.setPrefSize(width, height);
random = new Random();
newFood();
newSnake();
Scene scene = new Scene (root);
primaryStage.setTitle("Snake Game");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
logger.info("Game is activated");
launch(args);
}
}