Добрый вечер
Игра не работает : белый экран
Глаз мозолит уже , может быть вы подскажите где ошибку допустил я?
//класс Snake
package com.javarush.games.snake;
import com.javarush.engine.cell.*;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class Snake extends GameObject {
public Snake(int x, int y) {
super(x,y);
GameObject firstPart = new GameObject(x, y);
GameObject secondPart = new GameObject(x + 1, y);
GameObject thirdPart = new GameObject(x + 2, y);
snakeParts.add(firstPart);
snakeParts.add(secondPart);
snakeParts.add(thirdPart);
}
public void move(Apple apple) {
GameObject newHead = createNewHead();
if (newHead.x >= SnakeGame.WIDTH
|| newHead.x < 0
|| newHead.y >= SnakeGame.HEIGHT
|| newHead.y < 0) {
isAlive = false;
return;
}
if (checkCollision(newHead)) {
isAlive = false;
return;
} else snakeParts.add(0, newHead);
if (newHead.x == apple.x && newHead.y == apple.y) {
apple.isAlive = false;
} else {
removeTail();
}
}
public int getLength(){return snakeParts.size();}
public boolean checkCollision(GameObject object) { //проверка на столкновение с телом змеи
boolean answer = false;
for (GameObject obj : snakeParts) {
if (object.x == obj.x && object.y == obj.y) {
answer = true;
break;
}
}
return answer;
}
public void setDirection(Direction direction) {
switch (this.direction) {
case LEFT:
case RIGHT:
if (snakeParts.get(0).x == snakeParts.get(1).x) return;
break;
case UP:
case DOWN:
if (snakeParts.get(0).y == snakeParts.get(1).y) return;
break;
}
this.direction = direction;
}
public void removeTail() {
snakeParts.remove(snakeParts.size() - 1);
}
public GameObject createNewHead() {
GameObject gameObject = null;
if(direction == Direction.UP)
gameObject =new GameObject(snakeParts.get(0).x, snakeParts.get(0).y - 1);
if(direction == Direction.DOWN)
gameObject = new GameObject(snakeParts.get(0).x, snakeParts.get(0).y + 1);
if(direction == Direction.RIGHT)
gameObject = new GameObject(snakeParts.get(0).x+1, snakeParts.get(0).y);
if(direction == Direction.LEFT)
gameObject = new GameObject(snakeParts.get(0).x-1, snakeParts.get(0).y );
return gameObject;
private Direction direction = Direction.LEFT;
private List<GameObject> snakeParts = new ArrayList<>();
private static final String HEAD_SIGN = "\uD83D\uDC7E";
private static final String BODY_SIGN = "\u26AB";
public boolean isAlive = true;
public void draw(Game snake){
for (int i = 0; i < snakeParts.size(); i++) {
if (i == 0)
snake.setCellValueEx(
snakeParts.get(i).x,
snakeParts.get(i).y,
Color.NONE,
HEAD_SIGN,
isAlive? Color.BLACK : Color.RED,
75);
else
snake.setCellValueEx(
snakeParts.get(i).x,
snakeParts.get(i).y,
Color.NONE,
BODY_SIGN,
isAlive? null : Color.RED,
75);
}
}
}
//класс SnakeGame
package com.javarush.games.snake;
import com.javarush.engine.cell.Game;
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;
public void initialize() {
setScreenSize (WIDTH, HEIGHT);
createGame();
}
private int score;
private boolean isGameStopped;
private static final int GOAL = 28;
private void win(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.ALICEBLUE,"YOU ARE WIN!CONGRATULATIONS!",Color.BISQUE,35);
}
@Override
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.UP){
snake.setDirection(Direction.UP);}
else if(key == Key.DOWN){
snake.setDirection(Direction.DOWN);}
if(key == Key.SPACE && isGameStopped ){
createGame();
}
}
private void gameOver(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.DARKTURQUOISE,"YOU ARE DEAD!",Color.AQUAMARINE,29);
};
@Override
public void onTurn(int step) {
if(!snake.isAlive)
gameOver();
snake.move(apple);
if(!apple.isAlive){
score+=5;
setScore(score);
createNewApple();
turnDelay-=10;
setTurnTimer(turnDelay);
}
if(snake.getLength()>GOAL) {
win();
}
drawScene();
//onTurn(int) должен быть вызван метод drawScene() после вызова snake.move().
}
private void createNewApple() {
do {
apple = new Apple(getRandomNumber(WIDTH), getRandomNumber(HEIGHT));
} while (snake.checkCollision(apple));
}
private int turnDelay;
private void createGame(){
score =0;
setScore(score);
Snake 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.BLACK,""); } } }
snake.draw(this);
apple.draw(this);
}
}