My snake moves correctly when I push the arrow keys however the last tasks are not passing??
package com.codegym.games.snake;
import com.codegym.engine.cell.*;
public class SnakeGame extends Game
{
public static final int WIDTH = 15;
public static final int HEIGHT = 15;
private int turnDelay;
private Snake snake;
public void initialize()
{
setScreenSize(WIDTH, HEIGHT);
createGame();
}
public void onTurn(int turn) {
snake.move();
drawScene();
}
private void createGame()
{
Snake snake = new Snake(WIDTH / 2, HEIGHT / 2);
this.snake = snake;
this.turnDelay = 300;
drawScene();
setTurnTimer(turnDelay);
// Apple apple = new Apple(7, 7);
// apple.draw(this);
}
private void drawScene()
{
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
setCellValueEx(x, y, Color.SKYBLUE, "");
}
}
snake.draw(this);
}
@Override
public void onKeyPress(Key key) {
Direction d = snake.getDirection();
if (d == Direction.LEFT || d == Direction.RIGHT)
{
if (key == Key.UP)
{
snake.setDirection(Direction.UP);
}
else if (key == Key.DOWN)
{
snake.setDirection(Direction.DOWN);
}
}
else if (d == Direction.UP || d == Direction.DOWN)
{
if (key == Key.RIGHT)
{
snake.setDirection(Direction.RIGHT);
}
else if (key == Key.LEFT)
{
snake.setDirection(Direction.LEFT);
}
}
}
}