-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovement.ino
156 lines (135 loc) · 3.17 KB
/
Movement.ino
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
void reset() {
led.line(0, 0, 7, 7); // (x0, y0, x1, y1)
led.line(7, 0, 0, 7);
led.update();
delay(1000);
led.clear();
}
void reset_food_coor() {
led.dot(food[0], food[1], 0);
food[0] = random(0,7);
food[1] = random(0,7);
led.dot(food[0], food[1], 1);
led.update();
}
void food_status() {
if (snake[0].x_coor == food[0] && snake[0].y_coor == food[1]) {
grow();
reset_food_coor();
}
}
void grow() {
snake[snake_length].x_coor = food[0];
snake[snake_length].y_coor = food[1];
snake_length += 1;
}
void blink(unsigned int game_speed) {
for (int i = 0; i < snake_length; ++i) {
led.dot(snake[i].x_coor, snake[i].y_coor, 1);
}
led.update();
for (int i = 0; i < snake_length; ++i) {
led.dot(snake[i].x_coor, snake[i].y_coor, 0);
}
delay(100 + game_speed);
led.update();
}
/* update snake's coordinate system */
void update_snake_pos() {
for (int i = snake_length; i > 0; --i) {
snake[i].x_coor = snake[i - 1].x_coor;
snake[i].y_coor = snake[i - 1].y_coor;
}
}
int collision() {
int length = snake_length - 4;
if (length < 1)
length = 0;
for (unsigned int i = 0; i < length; ++i) {
Serial.print("snake length = ");
Serial.println(snake_length);
Serial.print(" ---- value of i: ");
Serial.println(i);
Serial.print("length: ");
Serial.println(length);
delay(1000);
Serial.print("\n ____snake[i].x_coor: ");
Serial.println(snake[i].x_coor);
Serial.print(" ____snake[i + 4].x_coor: ");
Serial.println(snake[i + 4].x_coor);
Serial.print(" ____snake[i].y_coor: ");
Serial.println(snake[i].y_coor);
Serial.print(" ____snake[i + 4].y_coor: ");
Serial.println(snake[i + 4].y_coor);
delay(1000);
if (snake[0].x_coor == snake[i + 4].x_coor
&& snake[0].y_coor == snake[i + 4].y_coor) {
Serial.println("=== collison check ===\n");
return 1;
}
}
Serial.println("END of collison check");
return 0;
}
int movement(unsigned int game_speed) {
blink(game_speed);
if (collision() == 1) {
reset();
return 1;
}
update_snake_pos();
return 0;
}
/* functions to change directions */
void move_up(unsigned int game_speed) {
if (previous_direction == DOWN) {
move_down(game_speed); // ignore command of going into opposite direction
return;
}
if (movement(game_speed) == 1)
return ;
if (snake[0].x_coor == 7)
snake[0].x_coor = -1;
snake[0].x_coor += 1;
food_status();
previous_direction = UP;
}
void move_down(unsigned int game_speed) {
if (previous_direction == UP) {
move_up(game_speed);
return;
}
if (movement(game_speed) == 1)
return ;
if (snake[0].x_coor == 0)
snake[0].x_coor = 8;
snake[0].x_coor -= 1;
food_status();
previous_direction = DOWN;
}
void move_right(unsigned int game_speed) {
if (previous_direction == LEFT) {
move_left(game_speed);
return;
}
if (movement(game_speed) == 1)
return ;
if (snake[0].y_coor == 7)
snake[0].y_coor = -1;
snake[0].y_coor += 1;
food_status();
previous_direction = RIGHT;
}
void move_left(unsigned int game_speed) {
if (previous_direction == RIGHT) {
move_right(game_speed);
return;
}
if (movement(game_speed) == 1)
return ;
if (snake[0].y_coor == 0)
snake[0].y_coor = 8;
snake[0].y_coor -= 1;
food_status();
previous_direction = LEFT;
}