I completed all the quests, but it doesn't accept the last one, where i have to setTurnTimer in the method onTurn.
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 Apple apple;
private int turnDelay;
private boolean isGameStopped;
private static final int GOAL = 28;
private int score;
private void win() {
stopTurnTimer();
isGameStopped = true;
Color color = Color.NONE , color1 = Color.NONE;
String message = "You Win";
int size = 60;
showMessageDialog(color, message, color1, size);
}
private void gameOver() {
stopTurnTimer();
isGameStopped = true;
Color color = Color.NONE , color1 = Color.NONE;
String message = "Game Over";
int size = 60;
showMessageDialog(color, message, color1, size);
}
private void createNewApple() {
Apple random;
do {
random = new Apple(getRandomNumber(WIDTH), getRandomNumber(HEIGHT));
}
while (snake.checkCollision(random));
apple = random;
}
public void onKeyPress(Key a) {
switch (a) {
case LEFT : snake.setDirection(Direction.LEFT); break;
case RIGHT : snake.setDirection(Direction.RIGHT); break;
case UP : snake.setDirection(Direction.UP); break;
case DOWN : snake.setDirection(Direction.DOWN); break;
}
if(a == a.SPACE && isGameStopped == true) {
createGame();
}
}
public void onTurn(int a) {
snake.move(apple);
if(apple.isAlive == false) {
createNewApple();
}
if(snake.isAlive == false) {
gameOver();
}
if(snake.getLength() > GOAL) {
win();
}
if(apple.isAlive == false) {
score += 5;
setScore(score);
turnDelay -= 10;
setTurnTimer(turnDelay);
}
drawScene();
}
public void initialize() {
setScreenSize(WIDTH, HEIGHT);
createGame();
}
private void createGame() {
score = 0;
setScore(score);
turnDelay = 300;
setTurnTimer(turnDelay);
Snake a = new Snake(WIDTH/2, HEIGHT/2);
snake = a;
createNewApple();
isGameStopped = false;
drawScene();
// 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.GREEN, "");
}
}
snake.draw(this);
apple.draw(this);
}
public static void main(String[] args) {
}
}