0

Im trying to make snake game and im using my implementation of ScheduledService to move rectangles to imitate movement.

Problem is that ScheduledService seems to have trouble with adding new nodes. To check if im correct i tried adding huge rectangle on a runtime and whole window doesnt refresh (when i remove line about this rectangle snake still moves but food cannot be seen). How do i make sure that task adds node to my scene?

I tried my methods without concurrency and everything works fine.

public class GameLoop extends ScheduledService {
    GameScene scene;
    Square first;
    Direction direction;


    GameLoop(GameScene scene){
        this.scene=scene;
        setPeriod(new Duration(150));
    }


    @Override
    protected Task createTask() {
        return new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                if(!gameOver()){
                    if (scene.getSnake().move(direction, scene.getFood())) {
                        scene.placeFood();
                        return null;
                    }
                }else{
                    System.out.println("game over");
                    cancel(false);
                }
                return null;
            }
        };
    }


    public void setDirection(Direction direction){
        this.direction=direction;
    }
}
public class GameScene extends Scene {
    private Snake snake;
    private Square food;
    private Pane pane;
    int i=0;
    //======================================================================


    GameScene(double x, double y, Pane pane){
        super(pane, x, y);
        this.pane=pane;
        pane.setBackground(new Background(new BackgroundFill(Color.BLACK, null, null)));
        snake=new Snake(x, y);
        placeFood();
        pane.getChildren().add(snake);

    }

    //=====================================================================

    public void placeFood(){
        //If i add this the whole window freezes
        /*
        if(i>0) {
            pane.getChildren().add(new Rectangle(100, 100, Color.BLUE));
        }
        */
        food=new Square(10, 10, Color.RED);
        System.out.println(food.hashCode());
        System.out.println("Setting new Food");
        do {
            food.setX(DataViability.roundTen(Math.random() *this.getWidth() - 10));
            food.setY(DataViability.roundTen(Math.random()*this.getHeight() - 10));
        }while(snake.onSnake(food, true));
        System.out.println("Broke out");
        pane.getChildren().add(food);
        // This sout doesnt appear on the console when adding food in a GameLoop
        System.out.println("Set food at : " + food.getX() + " " + food.getY());
        i++;
    }
Subish
  • 1
  • 1
    Use something from the `Animation` class to move the snake. I suggest `Timeline` or `AnimationTimer`. [Good resource 1](https://gamedevelopment.tutsplus.com/tutorials/introduction-to-javafx-for-game-development--cms-23835). [Good resource 2](https://carlfx.wordpress.com/2012/03/29/javafx-2-gametutorial-part-1/). – SedJ601 Jun 07 '19 at 19:57
  • 1
    https://stackoverflow.com/questions/9966136/javafx-periodic-background-task ?! (Don't try to modify the scene from a background thread; The tasks are executed on a background thread.) – fabian Jun 07 '19 at 20:44

0 Answers0