ΠΊΠ°ΠΊ ΡΠ΄Π΅Π»Π°ΡΡ ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅Π΅ ΡΡΠ΅Π±ΠΎΠ²Π°Π½ΠΈΠ΅((
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 int turnDelay;
@Override
public void initialize(){
setScreenSize(WIDTH, HEIGHT);
createGame();
}
private void createGame(){
snake = new Snake(WIDTH/2, HEIGHT/2);
turnDelay = 300;
setTurnTimer(turnDelay);
drawScene();
}
private void drawScene(){
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
setCellValueEx(x, y, Color.LIGHTPINK, "");
}
}
snake.draw(this);
}
@Override
public void onTurn(int a){
snake.move();
drawScene();
}
@Override
public void onKeyPress(Key key) {
switch(key){
case UP: {
snake.setDirection(Direction.UP);
break;
}case DOWN: {
snake.setDirection(Direction.DOWN);
break;
}case RIGHT: {
snake.setDirection(Direction.RIGHT);
break;
}case LEFT: {
snake.setDirection(Direction.LEFT);
break;
}
}
}
}