Класс SnakeGame;
package com.javarush.games.snake;
import com.javarush.engine.cell.*;
public class SnakeGame extends Game{
public static void main(String[] args) {
}
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;
private int score;
public void initialize(){
setScreenSize(WIDTH, HEIGHT);
createGame();
}
private void createGame(){
score = 0;
setScore(score);
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.DARKSEAGREEN,"");
}
}
snake.draw(this);
apple.draw(this);
}
@Override
public void onTurn(int step) {
snake.move(apple);
if (!apple.isAlive){
score += 5;
setScore(score);
turnDelay -= 10;
setTurnTimer(turnDelay);
createNewApple();
}
if (!snake.isAlive){
gameOver();
}
if (snake.getLength()> GOAL){
win();
}
drawScene();
}
@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.DOWN){
snake.setDirection(Direction.DOWN);
}
else if (key == Key.UP){
snake.setDirection(Direction.UP);
}
else if (key == Key.SPACE && isGameStopped){
createGame();
}
}
private void createNewApple(){
apple = new Apple(getRandomNumber(WIDTH), getRandomNumber(HEIGHT));
while (snake.checkCollision(apple))
apple = new Apple(getRandomNumber(WIDTH), getRandomNumber(HEIGHT));
}
private void gameOver(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.BLUE,"GAME OVER", Color.BLACK, 50);
}
private void win(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.RED, "WIN", Color.BLACK, 75);
}
}
Класс Snake;
package com.javarush.games.snake;
import com.javarush.engine.cell.Game;
import com.javarush.engine.cell.*;
import java.util.ArrayList;
import java.util.List;
public class Snake {
public boolean isAlive = true;
private static final String HEAD_SIGN = "\uD83D\uDC7E";
private static final String BODY_SIGN = "\u26AB";
private Direction direction = Direction.LEFT;
private List<GameObject> snakeParts = new ArrayList<GameObject>();
public void draw (Game game){
if (isAlive) {
for (int i = 0; i < snakeParts.size(); i++) {
if (i == 0) {
game.setCellValueEx(snakeParts.get(i).x, snakeParts.get(i).y, Color.NONE, HEAD_SIGN,Color.BLACK, 75);
}
else game.setCellValueEx(snakeParts.get(i).x, snakeParts.get(i).y, Color.NONE, BODY_SIGN,Color.BLACK, 75);
}
}
else {
for (int i = 0; i < snakeParts.size(); i++) {
if (i == 0) {
game.setCellValueEx(snakeParts.get(i).x, snakeParts.get(i).y, Color.NONE, HEAD_SIGN,Color.RED, 75);
}
else game.setCellValueEx(snakeParts.get(i).x, snakeParts.get(i).y, Color.NONE, BODY_SIGN,Color.RED, 75);
}
}
}
public Snake(int x, int y) {
for (int i = 0; i < 3; i++) snakeParts.add(new GameObject(x + i, y));
}
public void move(Apple apple){
GameObject cords = createNewHead();
if (apple.x == cords.x && apple.y == cords.y){
apple.isAlive = false;
}
else {
if (checkCollision(cords)){
isAlive = false;
}
else {
snakeParts.add(0, cords);
removeTail();
}
}
}
public GameObject createNewHead(){
if (direction == Direction.LEFT){
return new GameObject(snakeParts.get(0).x-1, snakeParts.get(0).y);
}
else if(direction == Direction.RIGHT){
return new GameObject(snakeParts.get(0).x+1, snakeParts.get(0).y);
}
else if (direction == Direction.DOWN){
return new GameObject(snakeParts.get(0).x, snakeParts.get(0).y +1);
}
else return new GameObject(snakeParts.get(0).x, snakeParts.get(0).y-1);
}
public void removeTail(){
snakeParts.remove(snakeParts.size()-1);
}
public void setDirection(Direction direction) {
if (direction == Direction.LEFT && this.direction != Direction.RIGHT && snakeParts.get(0).y != snakeParts.get(1).y){
this.direction = direction;
}
else if (direction == Direction.DOWN && this.direction != Direction.UP && snakeParts.get(0).x != snakeParts.get(1).x){
this.direction = direction;
}
else if (direction == Direction.UP && this.direction != Direction.DOWN && snakeParts.get(0).x != snakeParts.get(1).x){
this.direction = direction;
}
else if (direction == Direction.RIGHT && this.direction != Direction.LEFT && snakeParts.get(0).y != snakeParts.get(1).y){
this.direction = direction;
}
}
public boolean checkCollision(GameObject gameObject){
boolean result = false;
for (int i = 0; i < snakeParts.size(); i++ ){
if (snakeParts.get(i).x == gameObject.x && snakeParts.get(i).y == gameObject.y){
isAlive = false;
result = true;
}
}
return result;
}
public int getLength(){
return snakeParts.size();
}
}
Класс Apple;
package com.javarush.games.snake;
import com.javarush.engine.cell.Color;
import com.javarush.engine.cell.Game;
import com.javarush.engine.cell.*;
public class Apple extends GameObject {
public boolean isAlive = true;
private static final String APPLE_SIGN = "\uD83C\uDF4E";
public Apple(int x, int y) {
super(x, y);
}
public void draw(Game game){
game.setCellValueEx(x,y, Color.NONE,APPLE_SIGN,Color.GREEN, 75 );
}
}
Класс Direction;
package com.javarush.games.snake;
public enum Direction {
UP,
RIGHT,
DOWN,
LEFT;
}
Класс GameObject;
package com.javarush.games.snake;
public class GameObject {
public int x;
public int y;
public GameObject(int x, int y) {
this.x = x;
this.y = y;
}
}