если везде поставить x, то первые два условия выполняются, если в up и down поставить y, то вообще никакое условие не выполняется, почему?
package com.javarush.games.snake;
import com.javarush.engine.cell.*;
import com.javarush.games.snake.*;
public class SnakeGame extends Game{
public static final int WIDTH = 15;
public static final int HEIGHT = 15;
private Snake snake;
private int turnDelay;
private Apple apple;
private boolean isGameStopped;
private static final int GOAL = 28;
public Apple getApple(){
return apple;
}
public void initialize(){
setScreenSize(HEIGHT, WIDTH);
createGame();
}
private void gameOver(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.RED, "Game Over, loser", Color.BLACK, 60);
}
private void win(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.GREEN, "YOU ARE THE CHAMPION!", Color.BLUE, 60);
}
@Override
public void onTurn(int a){
snake.move(apple);
if(apple.isAlive == false) createNewApple();
if(snake.isAlive == false) gameOver();
if(snake.getLength() > GOAL) win();
drawScene();
}
private void createGame(){
snake = new Snake(WIDTH / 2, HEIGHT / 2);
createNewApple();
isGameStopped = false;
drawScene();
turnDelay = 300;
setTurnTimer(turnDelay);
}
private void createNewApple(){
while(true){
apple = new Apple(getRandomNumber(WIDTH),getRandomNumber(HEIGHT));
if(snake.checkCollision(apple) == false) break;
}
}
private void drawScene(){
for(int x = 0; x < WIDTH; x++){
for(int y = 0; y < HEIGHT; y++){
setCellValueEx(x, y, Color.DARKSEAGREEN, "");
}
}
snake.draw(this);
apple.draw(this);
}
@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(isGameStopped == true && key == key.SPACE) createGame();
}
}