/
main.c
executable file
·261 lines (210 loc) · 5.34 KB
/
main.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <math.h> //include libm
#include "mpu6050/mpu6050.h"
#include "Nokia5110/Nokia5110.h"
#define LIMIT 0.2
#define LEFT 42
#define UP 43
#define RIGHT 44
#define DOWN 45
#define WIDTH 14
#define HEIGHT 6
#define MAX_LEN 100
#define SNAKE 'O'
#define HEAD '0'
#define SPACE ' '
#define FOOD 'X'
typedef struct toast {
int x;
int y;
} toast;
char screen[HEIGHT][WIDTH];
toast snake[MAX_LEN];
toast food;
int snake_len, snake_tail;
int game = 1, score = 0;
int direction = RIGHT;
void put_food()
{
srand(42);
int x = (rand() + snake_len) % HEIGHT;
int y = (rand() + snake_tail) % WIDTH;
// Find available space if random fails
if (screen[x][y] == SNAKE) {
for (x=0; x<HEIGHT; x++)
for (y=0; y<WIDTH; y++) {
for (int z=0; z<MAX_LEN; z++) {
if (snake[z].x == -1 || x == snake[z].x || y == snake[z].y) {
screen[x][y] = FOOD;
food.x = x; food.y = y;
return;
}
}
}
} else {
screen[x][y] = FOOD;
food.x = x; food.y = y;
}
}
void update_snake()
{
int done;
for (int x=0; x<HEIGHT; x++)
for (int y=0; y<WIDTH; y++) {
done = 0;
for (int z=0; z<MAX_LEN; z++) {
if (snake[z].x != -1 && x == snake[z].x && y == snake[z].y) {
screen[x][y] = SNAKE;
done = 1;
break;
}
}
if (done == 0) {
if (screen[x][y] == FOOD) {
screen[x][y] = FOOD;
} else {
screen[x][y] = SPACE;
}
}
}
}
void change_dir()
{
// Can eat?
if (food.x == snake[snake_len-1].x && food.y == snake[snake_len-1].y) {
snake[snake_len].x = food.x;
snake[snake_len].y = food.y;
if (++snake_len == MAX_LEN) snake_len = 0;
screen[food.x][food.y] = SNAKE;
score += 10; put_food();
}
switch(direction) {
case UP: {
// Check for screen edge
if (snake[snake_len-1].x == 0) {
snake[snake_len].x = 5;
snake[snake_len].y = snake[snake_len-1].y;
} else {
snake[snake_len].x = snake[snake_len-1].x-1;
snake[snake_len].y = snake[snake_len-1].y;
}
break;
}
case RIGHT: {
// Check for screen edge
if (snake[snake_len-1].y == WIDTH-1) {
snake[snake_len].x = snake[snake_len-1].x;
snake[snake_len].y = 0;
} else {
snake[snake_len].x = snake[snake_len-1].x;
snake[snake_len].y = snake[snake_len-1].y+1;
}
break;
}
case DOWN: {
// Check for screen edge
if (snake[snake_len-1].x == HEIGHT-1) {
snake[snake_len].x = 0;
snake[snake_len].y = snake[snake_len-1].y;
} else {
snake[snake_len].x = snake[snake_len-1].x+1;
snake[snake_len].y = snake[snake_len-1].y;
}
break;
}
case LEFT: {
// Check for screen edge
if (snake[snake_len-1].y == 0) {
snake[snake_len].x = snake[snake_len-1].x;
snake[snake_len].y = WIDTH-1;
} else {
snake[snake_len].x = snake[snake_len-1].x;
snake[snake_len].y = snake[snake_len-1].y-1;
}
break;
}
}
// Wrong bite
if (screen[snake[snake_len].x][snake[snake_len].y] == SNAKE) {
// Score to string and screen
char ss[] = "Points: ";
char s[10];
dtostrf(score, 3, 0, s);
strcat(ss,s);
LCD_clear();
LCD_write_string(0,0," GAME OVER ");
LCD_write_string(0,2,ss);
// End game
game = 0; return;
}
// Mark slice as inactive
snake[snake_tail].x = -1;
// Keep inside array limit
if (++snake_tail == MAX_LEN) snake_tail = 0;
if (++snake_len == MAX_LEN) snake_len = 0;
update_snake();
}
int main(void)
{
double axg = 0, ayg = 0, azg = 0;
double gxds = 0, gyds = 0, gzds = 0;
//init mpu6050
mpu6050_init();
_delay_ms(50);
LCD_init();
LCD_clear();
put_food();
// INtro
while (1) {
// Read current parameters
mpu6050_getConvData(&axg, &ayg, &azg, &gxds, &gyds, &gzds);
LCD_write_string(0,0," Little Snake ");
LCD_write_string(0,3," >> Move << ");
if (axg > LIMIT || axg < -LIMIT ||ayg < -LIMIT || ayg > LIMIT)
break;
}
// Init snake, both on screen and array
screen[3][5] = SNAKE;
screen[3][6] = SNAKE;
screen[3][7] = SNAKE;
snake[0].x = 3; snake[0].y = 5;
snake[1].x = 3; snake[1].y = 6;
snake[2].x = 3; snake[2].y = 7;
snake_tail = 0; snake_len = 3;
for (int z=snake_len; z<MAX_LEN; z++) snake[z].x = -1;
while(game) {
// Read current parameters
mpu6050_getConvData(&axg, &ayg, &azg, &gxds, &gyds, &gzds);
// Take a decision
if(axg > LIMIT) {if (direction != UP) direction = DOWN;}
else if(axg < -LIMIT) {if (direction != DOWN) direction = UP;}
if(ayg > LIMIT) {if (direction != LEFT) direction = RIGHT;}
else if(ayg < -LIMIT) {if (direction != RIGHT) direction = LEFT;}
// Make it happen
change_dir();
// Check end game
if (!game) break;
LCD_init();
LCD_clear();
// Print entire screen
for (int x=0; x<HEIGHT; x++) {
for (int y=0; y<WIDTH; y++) {
if (x == snake[snake_len-1].x && y == snake[snake_len-1].y) {
LCD_write_char(HEAD);
} else if (screen[x][y] == SNAKE) {
LCD_write_char(SNAKE);
} else if (screen[x][y] == FOOD) {
LCD_write_char(FOOD);
} else {
LCD_write_char(SPACE);
}
}
}
_delay_ms(250);
}
}