В методе createGame() поле apple необходимо инициализировать новым объектом типа Apple с параметрами 5, 5 перед вызовом метода drawScene().
package com.javarush.games.snake;
import com.javarush.engine.cell.*;
public class SnakeGame extends Game{
private Apple apple;
private int turnDelay;
@Override
public void onTurn(int step) {
snake.move(apple);
drawScene();
}
private Snake snake;
private void createGame(){
apple = new Apple(5,5);
snake = new Snake(WIDTH/ 2, HEIGHT/ 2);
drawScene();
Apple apple = new Apple(7,7);
apple.draw(this);
turnDelay = 300;
setTurnTimer(turnDelay);
}
private void drawScene(){
for(int i = 0; i < HEIGHT; i++){
for(int a = 0; a < WIDTH; a++){
setCellValueEx(i,a,Color.ALICEBLUE, "");
}
}
snake.draw(this);
apple.draw(this);
}
public static final int WIDTH = 15;
public static final int HEIGHT = 15;
@Override
public void initialize() {
setScreenSize(WIDTH, HEIGHT);
createGame();
}
@Override
public void onKeyPress(Key key) {
if(key == Key.LEFT && key != Key.RIGHT) {
snake.setDirection(Direction.LEFT);
}
if(key == Key.RIGHT && key != Key.LEFT) {
snake.setDirection(Direction.RIGHT);
}
if(key == Key.DOWN && key != Key.UP) {
snake.setDirection(Direction.DOWN);
}
if(key == Key.UP && key != Key.DOWN){
snake.setDirection(Direction.UP);
}
}
}