The move(Apple) method must call the checkCollision(GameObject) method with the new head as the argument before the new snake segment is added to the snakeParts list.
Isn't it what I'm doing ?
public void move(Apple apple) {
GameObject newHead = createNewHead();
boolean outOfAxisX = newHead.x < 0 || newHead.x >= SnakeGame.WIDTH;
boolean outOfAxisY = newHead.y < 0 || newHead.y >= SnakeGame.HEIGHT;
this.isAlive = !(outOfAxisX || outOfAxisY) && !checkCollision(newHead);
if (this.isAlive) {
snakeParts.set(0, newHead);
if (this.snakeParts.get(0).x == apple.x && this.snakeParts.get(0).y == apple.y) { apple.isAlive = false; }
else { this.removeTail(); }
}
}
package com.codegym.games.snake;
import com.codegym.engine.cell.*;
public class Apple extends GameObject {
private static final String APPLE_SIGN = "\uD83C\uDF4E";
public boolean isAlive = true;
Apple(int x, int y) {
super(x, y);
}
public void draw(Game game) {
game.setCellValueEx(this.x, this.y, Color.NONE, APPLE_SIGN, Color.GREEN, 75);
}
}