-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.java
96 lines (78 loc) · 1.48 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
import java.util.LinkedList;
public class Snake {
private int x;
private int y;
private int xv;
private int yv;
public Snake() {
x = (Game.width / (2 * Game.size)) * Game.size;
y = (Game.height / (2 * Game.size)) * Game.size;
xv = 0;
yv = 0;
}
public Snake(int x, int y) {
this.x = x;
this.y = y;
xv = 0;
yv = 0;
}
public LinkedList<Snake> updateTail(LinkedList<Snake> tail) {
tail.add(new Snake(x, y));
while (tail.size() > Game.length)
tail.pop();
return tail;
}
public void eat(Apple apple, Game game) {
if (x + xv * Game.size == apple.getX() && y + yv * Game.size == apple.getY()) {
Game.length++;
game.setTitle("Score: " + ++Game.score);
apple.spawn();
}
}
public void move() {
x += xv * Game.size;
y += yv * Game.size;
}
public void teleport() {
if (x < 0)
x = Game.width;
if (x > Game.width)
x = 0;
if (y <= 0)
y = Game.height;
if (y > Game.height)
y = Game.size;
}
public void death(LinkedList<Snake> tail) {
for (int i = 0; i < tail.size(); i++) {
Snake s = tail.get(i);
if (Game.score != 0 && x == s.getX() && y == s.getY()) {
System.exit(1);
}
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getXv() {
return xv;
}
public void setXv(int xv) {
this.xv = xv;
}
public int getYv() {
return yv;
}
public void setYv(int yv) {
this.yv = yv;
}
}