|
@@ -0,0 +1,203 @@
|
|
1
|
+#define UP 1
|
|
2
|
+#define DOWN 2
|
|
3
|
+#define LEFT 3
|
|
4
|
+#define RIGHT 4
|
|
5
|
+#define IN 5
|
|
6
|
+#define OUT 6
|
|
7
|
+
|
|
8
|
+void snake(void);
|
|
9
|
+void setPixel(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
|
|
10
|
+void clearPixel(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
|
|
11
|
+uint8_t pixelSet(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
|
|
12
|
+void newCoin(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t *buf);
|
|
13
|
+void move(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t dir);
|
|
14
|
+
|
|
15
|
+void setPixel(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
|
|
16
|
+ buf[(8 * z) + y] |= (1 << x);
|
|
17
|
+}
|
|
18
|
+
|
|
19
|
+void clearPixel(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
|
|
20
|
+ buf[(8 * z) + y] &= ~(1 << x);
|
|
21
|
+}
|
|
22
|
+
|
|
23
|
+uint8_t pixelSet(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
|
|
24
|
+ return buf[(8 * z) + y] &= (1 << x);
|
|
25
|
+}
|
|
26
|
+
|
|
27
|
+void displayBuffs(uint8_t *a, uint8_t *b) {
|
|
28
|
+ uint8_t *buf = (uint8_t *)malloc(64);
|
|
29
|
+ uint8_t i;
|
|
30
|
+ for (i = 0; i < 64; i++) {
|
|
31
|
+ buf[i] = a[i] | b[i];
|
|
32
|
+ }
|
|
33
|
+ setImage(buf);
|
|
34
|
+ free(buf);
|
|
35
|
+}
|
|
36
|
+
|
|
37
|
+void newCoin(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t *buf) {
|
|
38
|
+ uint8_t tempX, tempY, tempZ;
|
|
39
|
+ while (1) {
|
|
40
|
+ tempX = (uint8_t)(((rand() / 256) / 32) - 1); // Num from 0 to 7
|
|
41
|
+ tempY = (uint8_t)(((rand() / 256) / 32) - 1);
|
|
42
|
+ tempZ = (uint8_t)(((rand() / 256) / 32) - 1);
|
|
43
|
+ if (!pixelSet(tempX, tempY, tempZ, buf)) {
|
|
44
|
+ break;
|
|
45
|
+ }
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ *x = tempX;
|
|
49
|
+ *y = tempY;
|
|
50
|
+ *z = tempZ;
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+void move(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t dir) {
|
|
54
|
+ switch (dir) {
|
|
55
|
+ case UP:
|
|
56
|
+ *y = (*y + 1);
|
|
57
|
+ break;
|
|
58
|
+
|
|
59
|
+ case DOWN:
|
|
60
|
+ *y = (*y - 1);
|
|
61
|
+ break;
|
|
62
|
+
|
|
63
|
+ case LEFT:
|
|
64
|
+ *x = (*x - 1);
|
|
65
|
+ break;
|
|
66
|
+
|
|
67
|
+ case RIGHT:
|
|
68
|
+ *x = (*x + 1);
|
|
69
|
+ break;
|
|
70
|
+
|
|
71
|
+ case IN:
|
|
72
|
+ *z = (*z - 1);
|
|
73
|
+ break;
|
|
74
|
+
|
|
75
|
+ case OUT:
|
|
76
|
+ *z = (*z + 1);
|
|
77
|
+ break;
|
|
78
|
+
|
|
79
|
+ default:
|
|
80
|
+ break;
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ while (*x > 7) {
|
|
84
|
+ *x -= 8;
|
|
85
|
+ }
|
|
86
|
+ while (*y > 7) {
|
|
87
|
+ *y -= 8;
|
|
88
|
+ }
|
|
89
|
+ while (*z > 8) {
|
|
90
|
+ *z -= 8;
|
|
91
|
+ }
|
|
92
|
+}
|
|
93
|
+
|
|
94
|
+uint8_t isSet(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf, uint8_t dir) {
|
|
95
|
+ move(&x, &y, &z, dir);
|
|
96
|
+ return pixelSet(x, y, z, buf);
|
|
97
|
+}
|
|
98
|
+
|
|
99
|
+uint8_t inverse(uint8_t dir) {
|
|
100
|
+ if ((dir > 0) && (dir < 7)) {
|
|
101
|
+ if ((dir % 2) == 0) {
|
|
102
|
+ // even dir
|
|
103
|
+ return (dir - 1);
|
|
104
|
+ } else {
|
|
105
|
+ // odd dir
|
|
106
|
+ return (dir + 1);
|
|
107
|
+ }
|
|
108
|
+ } else {
|
|
109
|
+ return 0;
|
|
110
|
+ }
|
|
111
|
+}
|
|
112
|
+
|
|
113
|
+void clearTail(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf, uint8_t wrongDir) {
|
|
114
|
+ // dirs from 1 to 6
|
|
115
|
+ uint8_t i;
|
|
116
|
+ for (i = 1; i <= 6; i++) {
|
|
117
|
+ if (i != wrongDir) {
|
|
118
|
+ if (isSet(x, y, z, buf, i)) {
|
|
119
|
+ move(&x, &y, &z, i);
|
|
120
|
+ clearTail(x, y, z, buf, inverse(i));
|
|
121
|
+ return;
|
|
122
|
+ }
|
|
123
|
+ }
|
|
124
|
+ }
|
|
125
|
+ // If we reached this point, we are in the last call and clear the pixel
|
|
126
|
+ clearPixel(x, y, z, buf);
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+void snake() {
|
|
130
|
+ uint8_t *snake = (uint8_t *)malloc(64); // Snake
|
|
131
|
+ uint8_t i, xPos = 3, yPos = 3, zPos = 3, dir = UP, length = 1;
|
|
132
|
+ uint8_t xCoin = 1, yCoin = 1, zCoin = 1;
|
|
133
|
+ char c;
|
|
134
|
+ for (i = 0; i < 64; i++) {
|
|
135
|
+ snake[i] = 0;
|
|
136
|
+ }
|
|
137
|
+ setPixel(3, 3, 3, snake);
|
|
138
|
+
|
|
139
|
+ serialWriteString("Controls: W A S D Q E, x to quit\n");
|
|
140
|
+
|
|
141
|
+ while(1) {
|
|
142
|
+ if (serialHasChar()) {
|
|
143
|
+ c = serialGet();
|
|
144
|
+ switch (c) {
|
|
145
|
+ case 'x':
|
|
146
|
+ free(snake);
|
|
147
|
+ return;
|
|
148
|
+
|
|
149
|
+ case 'w':
|
|
150
|
+ dir = UP;
|
|
151
|
+ break;
|
|
152
|
+
|
|
153
|
+ case 'a':
|
|
154
|
+ dir = LEFT;
|
|
155
|
+ break;
|
|
156
|
+
|
|
157
|
+ case 's':
|
|
158
|
+ dir = DOWN;
|
|
159
|
+ break;
|
|
160
|
+
|
|
161
|
+ case 'd':
|
|
162
|
+ dir = RIGHT;
|
|
163
|
+ break;
|
|
164
|
+
|
|
165
|
+ case 'q':
|
|
166
|
+ dir = IN;
|
|
167
|
+ break;
|
|
168
|
+
|
|
169
|
+ case 'e':
|
|
170
|
+ dir = OUT;
|
|
171
|
+ break;
|
|
172
|
+
|
|
173
|
+ default:
|
|
174
|
+ break;
|
|
175
|
+ }
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ if (isFinished() > 6) {
|
|
179
|
+ // Perform next game step and display it
|
|
180
|
+ move(&xPos, &yPos, &zPos, dir); // move snake head coords
|
|
181
|
+ setPixel(xPos, yPos, zPos, snake); // Add snake head pixel
|
|
182
|
+ if ((xPos == xCoin) && (yPos == yCoin) && (zCoin == zPos)) {
|
|
183
|
+ newCoin(&xCoin, &yCoin, &zCoin, snake); // Set new coin
|
|
184
|
+ if (length < 255) { // change length
|
|
185
|
+ length++;
|
|
186
|
+ } else {
|
|
187
|
+ // You won :)
|
|
188
|
+ free(snake);
|
|
189
|
+ return;
|
|
190
|
+ }
|
|
191
|
+ } else {
|
|
192
|
+ // Delete last snake pixel to match length
|
|
193
|
+ clearTail(xPos, yPos, zPos, snake, 0);
|
|
194
|
+ }
|
|
195
|
+
|
|
196
|
+ setPixel(xCoin, yCoin, zCoin, snake);
|
|
197
|
+ setImage(snake); // Display snake and coin
|
|
198
|
+ clearPixel(xCoin, yCoin, zCoin, snake);
|
|
199
|
+ }
|
|
200
|
+ }
|
|
201
|
+
|
|
202
|
+ free(snake);
|
|
203
|
+}
|