-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlappyPixel.ino
344 lines (298 loc) · 8.68 KB
/
FlappyPixel.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// FlappyPixel
// Use the A button to start the game and to move the pixel up.
// Avoid the green bars.
// If you reach 99 points rewrite drawScore to display three digit values ;)
//
// made for the digipixel http://www.bradsprojects.com/the-digipixel/
// with code from Brad and Crazyau
//
// 2014 Jan Krummrey
//
// todo:
// Display 3-digit scores
// or change color every 100 points for pipes and score
#include <DigiPixel.h>
#include <avr/pgmspace.h>
// leave the following line uncommented for use with a Digispark
DigiPixel digiPixel(3,0,5,2,1); // LED Latch/Button Shift !load pin, LED/Button clock pin, LED Data Pin, LED Output Enable pin, Buttons data pin)
// leave the following line uncommented for use with an Arduino
// DigiPixel digiPixel(5,2,6,4,3); // LED Latch/Button Shift !load pin, LED/Button clock pin, LED Data Pin, LED Output Enable pin, Buttons data pin)
// Program memory arrays
byte titleRed[56] PROGMEM={
0b00000, 0b00000, 0b00000, 0b00010, 0b00000, 0b01000, 0b00000, 0b00000, 0b11111, 0b10100, 0b00000, 0b11111, 0b00001, 0b00000, 0b01111, 0b10100, 0b01111, 0b00000, 0b11111, 0b10100, 0b01000, 0b00000, 0b11111, 0b10100, 0b01000, 0b00010, 0b11000, 0b00111, 0b11000, 0b00000, 0b01000, 0b00000, 0b11111, 0b10100, 0b01000, 0b00000, 0b11111, 0b00000, 0b11011, 0b00100, 0b11011, 0b00000, 0b11111, 0b10101, 0b00000, 0b11111, 0b00001, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00010, 0b00000, 0b01000, 0b00000};
byte titleGreen[56]PROGMEM={
0b00001, 0b01011, 0b00001, 0b00001, 0b00001, 0b00011, 0b00001, 0b00001, 0b11111, 0b10101, 0b00001, 0b11111, 0b00001, 0b00001, 0b01111, 0b10101, 0b01111, 0b00001, 0b11111, 0b10101, 0b01001, 0b00001, 0b11111, 0b10101, 0b01001, 0b00001, 0b11001, 0b00111, 0b11001, 0b00001, 0b00011, 0b00001, 0b11111, 0b10101, 0b01001, 0b00001, 0b11111, 0b00001, 0b11011, 0b00101, 0b11011, 0b00001, 0b11111, 0b10101, 0b00001, 0b11111, 0b00001, 0b00001, 0b00001, 0b00001, 0b01011, 0b00001, 0b00001, 0b00001, 0b00011, 0b00001};
byte titleBlue[56] PROGMEM={
0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b10100, 0b01000, 0b00000, 0b11111, 0b00000, 0b11011, 0b00100, 0b11011, 0b00000, 0b11111, 0b10101, 0b00000, 0b11111, 0b00001, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000};
byte numberTable[30]PROGMEM={
0b01111110, 0b01000010, 0b01111110, 0b00100010, 0b01111110, 0b00000010, 0b01001110, 0b01001010, 0b01111010, 0b01000010, 0b01001010, 0b01111110, 0b01111000, 0b00001000, 0b01111110, 0b01111010, 0b01001010, 0b01001110, 0b01111110, 0b01001010, 0b01001110, 0b01000000, 0b01000000, 0b01111110, 0b01111110, 0b01001010, 0b01111110, 0b01111010, 0b01001010, 0b01111110};
#define ScrollSpeedSave 7
#define DrawOffset 2
// Title Variables
byte ScrollSpeed = ScrollSpeedSave;
byte TextOffset = 0;
boolean drawCloud = true;
// bird values
int birdX;
int birdY;
const byte birdColor = 3;
const int birdFallSpeed = 200; // bigger = slower
long birdFallUpdate; // millis() when bird falling should be updated
const int birdMoveSpeed = 400; // bigger = slower
long birdMoveUpdate; // millis() when bird moving (pipes and background) should be updated
int score;
boolean birdIsHovering = false;
const int birdHoverDelayDefault = 30;
int birdHoverDelay = birdHoverDelayDefault;
// pipe values (2 pipes)
const byte numberOfPipes = 2;
int pipeX[numberOfPipes];
int pipeY[numberOfPipes];
const byte pipeColor = 2;
// cloud values
int clouds[] = {
0, 1, 1, 0, 1, 0, 1, 1};
const byte cloudColor = 7;
// states (buttons, gameOver)
boolean buttonPressed = false;
byte buttonCounter = 0;
boolean gameOver = false;
// score digits
byte mSD = 0;
byte lSD = 0;
void setup(){
titleLoop();
restart();
}
void restart() {
// start in middle
birdX = 3;
birdY = 3;
score = 0;
// set first move updates
birdFallUpdate = millis() + birdFallSpeed;
birdMoveUpdate = millis() + birdMoveSpeed;
// set the first pipes
for(int i = 0; i<numberOfPipes; i++)
{
pipeX[i] = 10 + (i*5);
pipeY[i] = random(6);
}
// reset gameover
gameOver = false;
buttonCounter = 0;
}
void loop(){
updateGame();
updateGraphics();
digiPixel.saveButtonStates();
digiPixel.drawScreen();
}
void updateGame(){
if (!gameOver)
{
// move bird
if (millis() > birdFallUpdate)
{
if (birdHoverDelay != 0)
{
birdHoverDelay--;
}
else{
birdFallUpdate = millis() + birdFallSpeed;
birdY = birdY - 1;
if ((birdY < 0) || (birdY > 200)) // game over if under screen
{
birdY = 0;
gameOver = true;
}
}
}
// flap at keypress (keep state to avoid key hold)
if (digiPixel.buttonAPressed == true){
if (!buttonPressed)
{
birdY = birdY + 1;
buttonPressed = true;
birdHoverDelay = birdHoverDelayDefault;
}
}
else
{
buttonPressed = false;
}
// move background and pipes
if (millis() > birdMoveUpdate)
{
birdMoveUpdate = millis() + birdMoveSpeed;
// move clouds
if (drawCloud == true)
{
for (int i = 0; i < 7; i ++)
{
clouds[i] = clouds[i+1];
}
clouds[7] = random(2); // 0 sky, 1 cloud
drawCloud = false;
}
else
{
drawCloud = true;
}
// move pipes
for(int i = 0; i<numberOfPipes; i++)
{
pipeX[i] = pipeX[i] - 1;
if (pipeX[i] == birdX-1) // bird passes pipe
{
score++;
}
if (pipeX[i] < 0)
{
pipeY[i] = random(6);
pipeX[i] = 9;
}
}
}
// check for collision with pipes
for(int i = 0; i<numberOfPipes; i++)
{
if (birdX == pipeX[i])
{
if ((birdY < pipeY[i]) || (birdY > pipeY[i] + 2))
{
gameOver = true;
}
}
}
}
else
{
waitForRestart();
}
}
void updateGraphics(){
digiPixel.clearScreen();
// draw clouds
for (int i = 0; i < 8; i++)
{
if (clouds[i] == 1)
{
digiPixel.setPixel(i, 7, cloudColor);
}
}
// draw pipes
for (int i = 0; i < numberOfPipes; i++)
{
for (int j = 0; j < 8; j++)
{
if (pipeX[i] < 8)
{
if ((j < pipeY[i]) or (j > pipeY[i] + 2))
{
digiPixel.setPixel(pipeX[i], j, pipeColor);
}
}
}
}
// draw bird
digiPixel.setPixel(birdX, birdY, birdColor);
// draw game over
//
if (gameOver)
{
drawScore();
}
}
void waitForRestart()
{
if (digiPixel.buttonAPressed == true){
if (!buttonPressed)
{
buttonPressed = true;
}
}
else
{
if (buttonPressed)
{
// only count button ups
buttonCounter++;
}
buttonPressed = false;
}
if (buttonCounter > 1)
{
// reset after two button ups (2 to avoid direct restart)
restart();
}
}
void updateTitle(){
if (ScrollSpeed != 0){
ScrollSpeed--;
}
else{
ScrollSpeed = ScrollSpeedSave;
TextOffset++;
if (TextOffset == sizeof(titleRed) - 8){
TextOffset = 0;
}
// move clouds
if (drawCloud == true)
{
for (int i = 0; i < 7; i ++)
{
clouds[i] = clouds[i+1];
}
clouds[7] = random(2); // 0 sky, 1 cloud
drawCloud = false;
}
else
{
drawCloud = true;
}
}
}
void saveTitle(){
digiPixel.clearScreen();
// draw characters
for (byte index = 0; index <= 7; index++){
digiPixel.bufferRed[index] = pgm_read_dword(&titleRed[(index) + TextOffset]);
digiPixel.bufferGreen[index] = pgm_read_dword(&titleGreen[(index) + TextOffset]);
digiPixel.bufferBlue[index] = pgm_read_dword(&titleBlue[(index) + TextOffset]);
}
// draw clouds
for (int i = 0; i < 8; i++)
{
if (clouds[i] == 1)
{
digiPixel.setPixel(i, 7, cloudColor);
}
}
}
void titleLoop(){
while (1){
updateTitle();
saveTitle();
digiPixel.saveButtonStates();
digiPixel.drawScreen();
if (digiPixel.buttonAPressed == true){
return;
}
}
}
void drawScore(){
mSD = score / 10;
lSD = score % 10;
digiPixel.clearScreen();
for (byte index = 0; index <= 2; index++)
{
digiPixel.bufferRed[index] = pgm_read_dword(&numberTable[(index) + (mSD * 3)]);
}
for (byte index = 4; index <= 6; index++)
{
digiPixel.bufferRed[index] = pgm_read_dword(&numberTable[(index - 4) + (lSD * 3)]);
}
digiPixel.drawScreen();
waitForRestart();
}