-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake_Game.ino
192 lines (156 loc) · 4.04 KB
/
Snake_Game.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
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
// Librarys
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include <ArduinoQueue.h>
#ifndef PSTR
#define PSTR
#endif
// Hardware Config
#define PIN 11
#define MATRIX_WIDTH 32
#define MATRIX_HEIGHT 8
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(MATRIX_WIDTH, MATRIX_HEIGHT, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
#define BUTTON_TOP 5
#define BUTTON_RIGHT 9
#define BUTTON_BOTTOM 6
#define BUTTON_LEFT 3
// Variablen und Konstanten, Konstrukte
#define TOP 0
#define RIGHT 1
#define BOTTOM 2
#define LEFT 3
struct coord {
int x;
int y;
};
ArduinoQueue<coord> snake(256);
coord snakeHead;
coord collectablePoint;
int direction;
bool lost;
//Helper-Funktionen
coord generateRandomCoord(int width, int height) {
return {
collectablePoint.x = random(0, width),
collectablePoint.y = random(0, height)
};
}
void emptySnake() {
while (!snake.isEmpty()) {
snake.dequeue();
}
}
//** Snake-Game **//
void initializeSnakeGame() {
lost = false;
emptySnake();
snake.enqueue({ 0, 0 });
snake.enqueue({ 1, 0 });
snake.enqueue({ 2, 0 });
snakeHead.x = 4;
snakeHead.y = 0;
direction = RIGHT;
collectablePoint = generateRandomCoord(MATRIX_WIDTH, MATRIX_HEIGHT);
}
void SnakeGame() {
while (!lost) {
// Richtung der Schlange festlegen
if (!digitalRead(BUTTON_TOP)) {
direction = TOP;
}
if (!digitalRead(BUTTON_RIGHT)) {
direction = RIGHT;
}
if (!digitalRead(BUTTON_BOTTOM)) {
direction = BOTTOM;
}
if (!digitalRead(BUTTON_LEFT)) {
direction = LEFT;
}
// Je nach Richtung Koordinaten verändern
switch (direction) {
case TOP:
snakeHead.y--;
break;
case RIGHT:
snakeHead.x++;
break;
case BOTTOM:
snakeHead.y++;
break;
case LEFT:
snakeHead.x--;
break;
}
// Bei Randwerten an anderer Seite wieder anfangen
if (snakeHead.x > MATRIX_WIDTH - 1) {
snakeHead.x = 0;
}
if (snakeHead.x < 0) {
snakeHead.x = MATRIX_WIDTH;
}
if (snakeHead.y > MATRIX_HEIGHT - 1) {
snakeHead.y = 0;
}
if (snakeHead.y < 0) {
snakeHead.y = MATRIX_HEIGHT;
}
// Checken ob crash -> Spiel verloren
for (int i = 0; i < snake.itemCount(); i++) {
coord coordToCheck = snake.dequeue();
snake.enqueue(coordToCheck);
if (snakeHead.x == coordToCheck.x && snakeHead.y == coordToCheck.y) {
lost = true;
}
}
// Berechnete neue Kopfkoordinate an Schlange anhängen
snake.enqueue({
snakeHead.x,
snakeHead.y,
});
// Je nachdem ob Punkt getroffen wurde Snake hinten kürzen (voran laufen) oder länger lassen und neuen Punkt generieren
if (snakeHead.x == collectablePoint.x && snakeHead.y == collectablePoint.y) {
collectablePoint = generateRandomCoord(MATRIX_WIDTH, MATRIX_HEIGHT);
} else {
snake.dequeue();
}
matrix.fillScreen(0);
matrix.drawPixel(collectablePoint.x, collectablePoint.y, matrix.Color(255, 0, 0));
for (int coordCount = snake.itemCount(); coordCount > 0; coordCount--) {
coord coordToDraw = snake.dequeue();
snake.enqueue(coordToDraw);
matrix.drawPixel(coordToDraw.x, coordToDraw.y, matrix.Color(0, 255, 0));
}
matrix.show();
//Delay zum prellen der Buttons
delay(50);
}
}
void printScore(){
matrix.fillScreen(0);
matrix.setCursor(0, 0);
matrix.setTextColor(matrix.Color(255, 0, 0));
matrix.print(snake.itemCount());
matrix.show();
}
//** ENDE Snake-Game **//
// Hauptsetup des Programms
void setup() {
pinMode(BUTTON_TOP, INPUT_PULLUP);
pinMode(BUTTON_RIGHT, INPUT_PULLUP);
pinMode(BUTTON_BOTTOM, INPUT_PULLUP);
pinMode(BUTTON_LEFT, INPUT_PULLUP);
matrix.begin();
matrix.setTextWrap(false);
matrix.setBrightness(40);
}
// Main-Loop
void loop() {
initializeSnakeGame();
SnakeGame();
printScore();
delay(5000);
}