Skip to content
Snippets Groups Projects
SnakeGame.java 1.56 KiB
Newer Older
package org.example.game;

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);
    }
}