-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.java
118 lines (97 loc) · 3.9 KB
/
Snake.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.javarush.games.snake;
import com.javarush.engine.cell.Color;
import com.javarush.engine.cell.Game;
import java.util.ArrayList;
import java.util.List;
public class Snake {
private static final String HEAD_SIGN = "\uD83D\uDC7E";
private static final String BODY_SIGN = "\u26AB";
private final List<GameObject> snakeParts = new ArrayList<>();
public boolean isAlive = true;
private Direction direction = Direction.LEFT;
public Snake(int x, int y) {
GameObject first = new GameObject(x, y);
GameObject second = new GameObject(x + 1, y);
GameObject third = new GameObject(x + 2, y);
snakeParts.add(first);
snakeParts.add(second);
snakeParts.add(third);
}
public void draw(Game game) {
Color color = isAlive ? Color.BLACK : Color.RED;
for (int i = 0; i < snakeParts.size(); i++) {
GameObject part = snakeParts.get(i);
String sign = (i != 0) ? BODY_SIGN : HEAD_SIGN;
game.setCellValueEx(part.x, part.y, Color.NONE, sign, color, 75);
}
}
public void setDirection(Direction direction) {
if ((this.direction == Direction.LEFT || this.direction == Direction.RIGHT) && snakeParts.get(0).x == snakeParts.get(1).x) {
return;
}
if ((this.direction == Direction.UP || this.direction == Direction.DOWN) && snakeParts.get(0).y == snakeParts.get(1).y) {
return;
}
if (direction == Direction.UP && this.direction == Direction.DOWN) {
return;
} else if (direction == Direction.LEFT && this.direction == Direction.RIGHT) {
return;
} else if (direction == Direction.RIGHT && this.direction == Direction.LEFT) {
return;
} else if (direction == Direction.DOWN && this.direction == Direction.UP) {
return;
}
this.direction = direction;
}
public void move(Apple apple) {
GameObject newHead = createNewHead(); //Creating a new head
if (checkCollision(newHead)) {
isAlive = false; //If a coordinate match is found, we set the 'isAlive' flag of the snake to false.
return;
}
//Checking if the coordinates of the new head are within the bounds of the game field.
if (newHead.x >= SnakeGame.WIDTH || newHead.x < 0 || newHead.y >= SnakeGame.HEIGHT || newHead.y < 0) {
isAlive = false; //Setting the 'isAlive' flag to false if the coordinates go beyond the boundaries of the game field.
return;
}
// Checking for a match between the coordinates of the new head and the coordinates of the apple.
if (newHead.x == apple.x && newHead.y == apple.y) {
apple.isAlive = false;
}//Setting the 'isAlive' flag of the apple to false if the coordinates match.
else {
removeTail(); // Удаление хвоста
}
snakeParts.add(0, newHead); // Adding a new head to the beginning of the list.
}
public GameObject createNewHead() {
GameObject head = snakeParts.get(0);
int headX = head.x;
int headY = head.y;
int newHeadX = headX;
int newHeadY = headY;
if (direction == Direction.LEFT) {
newHeadX--;
} else if (direction == Direction.RIGHT) {
newHeadX++;
} else if (direction == Direction.UP) {
newHeadY--;
} else {
newHeadY++;
}
return new GameObject(newHeadX, newHeadY);
}
public void removeTail() {
snakeParts.remove(snakeParts.size() - 1);
}
public boolean checkCollision(GameObject gameObject) {
for (GameObject part : snakeParts) {
if (part.x == gameObject.x && part.y == gameObject.y) {
return true;
}
}
return false;
}
public int getLength() {
return snakeParts.size();
}
}