Problems with move function. How to access the head?
Now we have an apple and a snake that can move. Now we'll implement the logic to make them interact. To start, add an apple variable to the SnakeGame class, and initialize its coordinates to (5, 5) when the game is created. And, draw an apple.
Rewrite the Snake class's move() method. Now it should take an apple as an argument. And if the snake eats an apple, the apple's state should be set to dead, and the snake's size should increase by 1 element.
- The SnakeGame class must have a private Apple apple field.
- In the createGame() method, the apple field must be initialized with a new Apple object with arguments (5, 5) before the drawScene() is called.
- In the drawScene() method, you must call the draw(Game) method on the apple after drawing the snake. Pass this to the method.
- The Snake class must not have a public void move() method.
- The Snake class must have a public void move(Apple apple) method.
- In the move(Apple) method, if the new snake head's coordinates match the apple's coordinates, you need to set isAlive=false on the apple and not remove the snake's tail.
- Call the move(Apple) method in the SnakeGame class's onTurn(int) method.
Bud1% @ @ @ @E%DSDB` @ @ @