Quellcode durchsuchen

Added 3d snake game to play on cube.

Yes, I'm bored. But thats only for you to do the real work *wink wink*…
Thomas Buck vor 12 Jahren
Ursprung
Commit
8aa5269ea7
2 geänderte Dateien mit 219 neuen und 12 gelöschten Zeilen
  1. 16
    12
      CubeFirmware/main.c
  2. 203
    0
      CubeFirmware/snake.c

+ 16
- 12
CubeFirmware/main.c Datei anzeigen

@@ -59,18 +59,19 @@
59 59
 // x = errorcode, e = error definition, not NOERROR
60 60
 #define ISERROR(x, e) ((x) & (e))
61 61
 
62
-uint8_t selfTest(void);
63 62
 void serialHandler(char c);
64 63
 void sendAudioData(void);
65 64
 void recieveAnimations(void);
66 65
 void transmitAnimations(void);
67 66
 uint8_t audioModeSelected(void);
68
-inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
69
-inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
67
+void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
70 68
 void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf);
71 69
 void visualizeAudioData(uint8_t *audioData, uint8_t *imageData);
72 70
 #ifdef DEBUG
73 71
 void printErrors(uint8_t e);
72
+uint8_t selfTest(void);
73
+
74
+#include "snake.c"
74 75
 #endif
75 76
 
76 77
 uint8_t refreshAnimationCount = 1;
@@ -139,8 +140,10 @@ int main(void) {
139 140
 			if (isFinished()) {
140 141
 				audioData = getAudioData();
141 142
 				if (audioData != NULL) {
143
+					imageData = (uint8_t *)malloc(64);
142 144
 					visualizeAudioData(audioData, imageData);
143 145
 					setImage(imageData);
146
+					free(imageData);
144 147
 					free(audioData);
145 148
 				}
146 149
 			}
@@ -183,6 +186,7 @@ int main(void) {
183 186
 	return 0;
184 187
 }
185 188
 
189
+#ifdef DEBUG
186 190
 uint8_t selfTest(void) {
187 191
 	uint8_t result = NOERROR;
188 192
 	
@@ -208,7 +212,6 @@ uint8_t selfTest(void) {
208 212
 	return result;
209 213
 }
210 214
 
211
-#ifdef DEBUG
212 215
 void printErrors(uint8_t e) {
213 216
 	if (ISERROR(e, AUDIOERROR)) {
214 217
 		serialWriteString(" => No answer from Audio!\n");
@@ -241,6 +244,7 @@ void serialHandler(char c) {
241 244
 		serialWriteString("(t)ime, (a)udio, (c)ount, (x)Custom count\n");
242 245
 		serialWriteString("(y)Set fixed animation count\n");
243 246
 		serialWriteString("S(e)lf Test\n");
247
+		serialWriteString("Play S(n)ake\n");
244 248
 #endif
245 249
 		break;
246 250
 
@@ -315,6 +319,10 @@ void serialHandler(char c) {
315 319
 		serialWrite('\n');
316 320
 		printErrors(c);
317 321
 		break;
322
+
323
+	case 'n': case 'N':
324
+		snake();
325
+		break;
318 326
 #endif
319 327
 
320 328
 	default:
@@ -503,14 +511,6 @@ uint8_t audioModeSelected(void) {
503 511
 	return lastButtonState;
504 512
 }
505 513
 
506
-inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
507
-	buf[(8 * z) + y] |= (1 << x);
508
-}
509
-
510
-inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
511
-	buf[(8 * z) + y] &= ~(1 << x);
512
-}
513
-
514 514
 void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
515 515
 	uint8_t i = 0;
516 516
 	for (; i < height; i++) {
@@ -518,6 +518,10 @@ void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
518 518
 	}
519 519
 }
520 520
 
521
+void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
522
+	buf[(8 * z) + y] |= (1 << x);
523
+}
524
+
521 525
 void visualizeAudioData(uint8_t *audioData, uint8_t *imageData) {
522 526
 	uint8_t i;
523 527
 	for (i = 0; i < 64; i++) {

+ 203
- 0
CubeFirmware/snake.c Datei anzeigen

@@ -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
+}

Laden…
Abbrechen
Speichern