help
package com.javarush.games.snake;
import com.javarush.engine.cell.*;
public class SnakeGame extends Game {
public static final int WIDTH = 15;
public static final int HEIGHT = 15;
private Snake snake;
private Apple apple;
private int turnDelay;
private boolean isGameStopped;
private static final int GOAL = 28;
@Override
public void initialize(){
setScreenSize(WIDTH, HEIGHT);
createGame();
}
private void createGame(){
turnDelay = 300;
snake = new Snake(WIDTH/2, HEIGHT/2);
createNewApple();
isGameStopped = false;
drawScene();
setTurnTimer(turnDelay);
}
private void win() {
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.BLACK,"YOU WIN!",Color.RED,50);
}
private void gameOver() {
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.BLACK,"GAME OVER!",Color.RED,50);
}
private void drawScene(){
for(int x=0; x<WIDTH; x++){
for(int y=0; y<HEIGHT; y++){
setCellValueEx(x, y, Color.GREEN, "");
}
}
snake.draw(this);
apple.draw(this);
}
private void createNewApple() {
Apple newApple = new Apple(getRandomNumber(WIDTH), getRandomNumber(HEIGHT));
apple = newApple;
}
@Override
public void onTurn(int step){
snake.move(apple);
if(!apple.isAlive) {
createNewApple();
}
if (!snake.isAlive) {
gameOver();
}
if (snake.getLength()>GOAL) {
win();
}
drawScene();
}
@Override
public void onKeyPress(Key key) {
if(key == Key.LEFT){
snake.setDirection(Direction.LEFT);
}
if(key == Key.RIGHT){
snake.setDirection(Direction.RIGHT);
}
if(key == Key.UP){
snake.setDirection(Direction.UP);
}
if(key == Key.DOWN){
snake.setDirection(Direction.DOWN);
}
else{
}
}
}