-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.java
More file actions
134 lines (108 loc) · 3.9 KB
/
Snake.java
File metadata and controls
134 lines (108 loc) · 3.9 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import com.javarush.engine.cell.*;
import java.util.ArrayList;
import java.util.List;
public class Snake{
private List<GameObject> snakeParts = new ArrayList<>();
private static final String HEAD_SIGN = "🐲";
private static final String BODY_SIGN = "🍀";
public boolean isAlive = true;
private Direction direction = Direction.LEFT;
private Color cellColor;
public Snake(int x, int y){
GameObject one = new GameObject(x, y);
GameObject two = new GameObject(x+1, y);
GameObject three = new GameObject(x+2,y);
snakeParts.add(one);
snakeParts.add(two);
snakeParts.add(three);
}
public void draw(Game game){
if(isAlive){
cellColor=Color.BLACK;
}
else{
cellColor=Color.RED;
}
game.setCellValueEx(snakeParts.get(0).x, snakeParts.get(0).y, Color.NONE, HEAD_SIGN, cellColor, 75);
for (int i = 1; i != snakeParts.size(); i++) {
game.setCellValueEx(snakeParts.get(i).x, snakeParts.get(i).y, Color.NONE, BODY_SIGN, cellColor, 75);
}
}
public void setDirection(Direction direction){
if(snakeParts.get(0).x == snakeParts.get(1).x && (this.direction == Direction.LEFT || this.direction == Direction.RIGHT)) {
return; //if the snake is going left/right and we press "LEFT/RIGHT" - do nothing(return)
}
if(snakeParts.get(0).y == snakeParts.get(1).y && (this.direction == Direction.UP || this.direction == Direction.DOWN)){
return; //if the snake is going up and we press "UP/DOWN" - do nothing(return)
}
if(direction == Direction.UP && this.direction == Direction.DOWN) {
return;
}
if(direction == Direction.RIGHT && this.direction == Direction.LEFT) {
return;
}
if(direction == Direction.DOWN && this.direction == Direction.UP) {
return;
}
if(direction == Direction.LEFT && this.direction == Direction.RIGHT) {
return;
}
this.direction = direction;
}
public void move(Apple apple){
GameObject newHead = createNewHead();
if(newHead.x>=SnakeGame.WIDTH || //if the snake encounters the wall;
newHead.x<0 ||
newHead.y>=SnakeGame.HEIGHT ||
newHead.y<0
){
isAlive=false;
return;
}
if(newHead.x == apple.x && newHead.y == apple.y){ //if the snake encounters the apple;
apple.isAlive=false;
snakeParts.add(0, newHead);
}
else{
if(checkCollision(newHead)) {
isAlive = false;
return;
}
removeTail();
snakeParts.add(0, newHead);
}
}
public GameObject createNewHead(){
GameObject object = snakeParts.get(0);
switch (direction){
case UP:
object= new GameObject(object.x, object.y-1);
break;
case DOWN:
object= new GameObject(object.x, object.y+1);
break;
case RIGHT:
object= new GameObject(object.x+1, object.y);
break;
case LEFT: object= new GameObject(object.x-1, object.y);
break;
}
return object;
}
public void removeTail(){
int removeLastIndex = snakeParts.size() - 1;
snakeParts.remove(removeLastIndex);
}
public boolean checkCollision(GameObject object){
boolean collision=false;
for(GameObject each: snakeParts){
if(each.x == object.x && each.y == object.y) {
collision=true;
}
}
return collision;
}
public int getLength(){
return snakeParts.size();
}
}