У меня закончились варианты, для чего это надо понятно ,но как реализовать не могу понять ...
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 int turnDelay;
private Snake snake;
private Apple apple;
private boolean isGameStopped;
private static final int GOAL =28;
public void initialize(){
setScreenSize(WIDTH, HEIGHT);
createGame();
}
private void createGame(){
snake = new Snake(WIDTH/2, HEIGHT/2);
createNewApple();
isGameStopped = false;
drawScene();
turnDelay= 300;
setTurnTimer(turnDelay);
}
private void drawScene(){
for(int x =0;x<WIDTH;x++){
for(int y = 0;y<HEIGHT;y++){
setCellValueEx(x, y, Color.BLUE, "");
}
}
snake.draw(this);
apple.draw(this);
}
public void onTurn(int j){
snake.move(apple);
if(apple.isAlive == false){
createNewApple();
}
if(snake.isAlive == false){
gameOver();
}
if(snake.getLength()>GOAL){
win();
}
drawScene();
}
private void createNewApple(){
do{
apple = new Apple(getRandomNumber(WIDTH),getRandomNumber(HEIGHT));
}
while(snake.checkCollision(apple));
}
public void onKeyPress(Key key){
if (key == Key.LEFT) {
snake.setDirection(Direction.LEFT);
}
if (key == Key.RIGHT) {
snake.setDirection(Direction.RIGHT);
}
if (key == Key.DOWN) {
snake.setDirection(Direction.DOWN);
}
if (key == Key.UP) {
snake.setDirection(Direction.UP);
}
if(key == Key.SPACE && isGameStopped == true){
createGame();
}
}
private void gameOver(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.BLACK, "GAME OVER", Color.RED, 65);
}
private void win(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.BLACK, "YOU WIN!!!", Color.GREEN, 65);
}
}