I have no idea why UP and DOWN Direction not passing....Someone please help .I am stuck at part18 of snake game
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 Snake snake;
private int turnDelay;
private Apple apple;
private boolean isGameStopped;
private static final int GOAL=28;
public void initialize()
{
setScreenSize(15,15);
createGame();
}
private void createGame()
{
snake = new Snake(WIDTH/2,WIDTH/2);
// apple= new Apple(5,5);
createNewApple();
isGameStopped=false;
drawScene();
// Apple apple = new Apple(7,7);
// apple.draw(this);
turnDelay=300;
setTurnTimer(turnDelay);
}
private void createNewApple()
{
while(true)
{
int x= getRandomNumber(8);
int y=getRandomNumber(8);
apple = new Apple(x,y);
boolean checkcol= snake.checkCollision( apple);
if(checkcol==false)
break;
}
}
private void drawScene()//Drawing the grid
{
for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
// setCellColor(x, y, Color.DARKSEAGREEN) ;
setCellValueEx(x, y, Color.DARKSEAGREEN, "");
}
}
snake.draw(this);
apple.draw(this);
}
public void onTurn(int i)
{
snake.move(apple);
if(apple.isAlive==false)
{
createNewApple();
}
if(snake.isAlive==false)
{
gameOver();
}
if(snake.getLength()>28)
{
win();
}
drawScene();
}
public void onKeyPress(Key key)
{
if(key == Key.LEFT)
{
snake.setDirection(Direction.LEFT);
}
else
if(key == Key.RIGHT)
{
snake.setDirection(Direction.RIGHT);
}
else
if(key == Key.DOWN)
{
snake.setDirection(Direction.DOWN);
}
else
if(key == Key.UP)
{
snake.setDirection(Direction.UP);
}
else
if(key==Key.SPACE && isGameStopped == true)
{
createGame();
}
}
private void gameOver()
{
stopTurnTimer();
isGameStopped=true;
showMessageDialog(Color.GREEN, "GAME OVER", Color.RED, 10) ;
}
private void win()
{
stopTurnTimer() ;
isGameStopped=true;
showMessageDialog(Color.GREEN, "YOU WON", Color.RED, 10) ;
}
}