-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_class.pde
138 lines (119 loc) · 3.34 KB
/
snake_class.pde
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
135
136
137
138
class Snake{
BodyPart head;
BodyPart tail;
int died = 0;
int direction = floor(random(4));;
int snake_length = 1;
Snake(int headX, int headY) {
head = new BodyPart(headX, headY);
tail = head;
}
void append(int x, int y) {
BodyPart current = head;
while (current.next != null) {
current = current.next;
}
current.next = new BodyPart(x,y);
current.next.previous = current;
tail = current.next;
snake_length++;
}
void draw_snake(){
BodyPart current = head;
draw_rectangle(current.xCell, current.yCell, "head");
//println("head at x = ", current.xCell, " and y = ", current.yCell);
current = current.next;
while (current.next != null) {
//println("x = ", current.xCell, " and y = ", current.yCell);
draw_rectangle(current.xCell, current.yCell, "body");
current = current.next;
}
//println("x = ", current.xCell, " and y = ", current.yCell);
draw_rectangle(current.xCell, current.yCell, "body");
}
void test_iterate_back(){
BodyPart current = tail;
println("tail at x = ", current.xCell, " and y = ", current.yCell);
current = current.previous;
while (current.previous != null) {
println("x = ", current.xCell, " and y = ", current.yCell);
current = current.previous;
}
println("head x = ", current.xCell, " and y = ", current.yCell);
}
int get_length() {
return snake_length;
}
int get_dir() {
return direction;
}
int check_collision(int cellX, int cellY) {
BodyPart current = head;
if (head.xCell == cellX && head.yCell == cellY) {
return 2;
}
while (current.next != null && current.next.next != null) {
current = current.next;
if (current.xCell == cellX && current.yCell == cellY) {
return 1;
}
}
return 0;
}
void propagate() {
int goal_x = (direction%2)*(2-direction) + head.xCell;
int goal_y = (1-direction%2)*(-1+direction) + head.yCell;
int collision_value = checkCollision(goal_x, goal_y);
if (collision_value == 3) { //fruit
BodyPart p = new BodyPart(goal_x, goal_y);
head.previous = p;
p.next = head;
head = p;
createFruit();
snake_length++;
} else if (collision_value > 0) { //fruit
if (VERBOSE) println("collision with walls");
died = 1;
} else if (collision_value < 0) { //fruit
if (VERBOSE) println("collision with body");
died = 1;
} else {
if (VERBOSE) println("started propagation");
BodyPart current = tail;
while (current.previous != null) {
current.xCell = current.previous.xCell;
current.yCell = current.previous.yCell;
current = current.previous;
}
current.xCell = goal_x;
current.yCell = goal_y;
if (VERBOSE) println("finished propagation");
}
}
int is_dead() {
return died;
}
}
class BodyPart{
BodyPart next;
BodyPart previous;
int xCell, yCell;
BodyPart(int xcellindex, int ycellindex) {
xCell = xcellindex;
yCell = ycellindex;
next = null;
previous = null;
}
}
class Fruit{
int xCell, yCell;
int reward;
Fruit(int xcellindex, int ycellindex, int framerate) {
xCell = xcellindex;
yCell = ycellindex;
reward = framerate;
}
void draw_fruit() {
draw_rectangle(xCell, yCell, "fruit");
}
}