Эммм, что-то туплю. Вроде все работает. Игру запускаю, играю, но не показывает, что уровень пройден и. как следствие, не могу опубликовать игру....
package com.javarush.games.snake;
import com.javarush.engine.cell.*;
import static com.javarush.games.snake.Direction.*;
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;
private int score;
public void initialize(){
setScreenSize(WIDTH, HEIGHT);
createGame();
}
private void createGame() {
turnDelay = 300;
Snake s1 = new Snake(WIDTH/2, HEIGHT/2);
snake = s1;
createNewApple();
isGameStopped = false;
drawScene();
setTurnTimer(turnDelay);
score = 0;
setScore(score);
}
private void drawScene() {
int y = 0;
while (y < WIDTH) {
for (int x = 0; x < HEIGHT; x++) {
setCellValueEx(x, y, Color.DARKSEAGREEN, "");
}
y++;
}
snake.draw(this);
apple.draw(this);
}
public void onTurn(int i){
snake.move(apple);
if (!apple.isAlive){
createNewApple();
score = score + 5;
setScore(score);
turnDelay = turnDelay - 10;
setTurnTimer(turnDelay);
}
if (!snake.isAlive) gameOver();
if (snake.getLength() > GOAL) win();
drawScene();
}
public void onKeyPress(Key key) {
if (key == Key.UP) snake.setDirection(UP);
if (key == Key.DOWN) snake.setDirection(DOWN);
if (key == Key.LEFT) snake.setDirection(LEFT);
if (key == Key.RIGHT) snake.setDirection(RIGHT);
if (isGameStopped) {
if (key == Key.SPACE) createGame();
}
}
private void createNewApple() {
do
apple = new Apple (getRandomNumber(WIDTH), getRandomNumber(HEIGHT));
while (snake.checkCollision(apple));
}
private void gameOver() {
isGameStopped = true;
this.stopTurnTimer();
this.showMessageDialog(Color.NONE, "GAME OVER", Color.BLACK, 75);
}
private void win() {
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.NONE, "YOU WIN", Color.BLACK, 75);
}
}