-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_matrix_test.ino
137 lines (117 loc) · 3.25 KB
/
led_matrix_test.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
#include <string.h>
#include "wall.h"
#include "hero.h"
#include "videoBuffer.h"
#include "charDrawer.h"
#define RESET 3
#define CLOCK 4
#define BUTTON 2
#define HEIGHT 8
#define WIDTH 10
#define GAME_TITLE 0
#define GAME_ACTION 1
#define GAME_CRASH 2
#define GAME_OVER 3
#define GAME_SCORE 4
VideoBuffer vb = VideoBuffer((int)WIDTH, (int)HEIGHT, 6, (int)RESET, (int)CLOCK);
unsigned long last_millis = 0;
Hero hero;
Wall wall;
bool button_down = false;
bool button_pressed = false;
int game_state = GAME_TITLE;
unsigned long game_state_changed = 0;
char title_text[5] = "BIRD";
char game_over_text[10] = "GAME OVER";
char score_text[6] = "SCORE";
char score[12];
void setup() {
Serial.begin(9600);
pinMode(BUTTON, INPUT);
hero.setup(1, HEIGHT/2);
wall.setup(WIDTH, HEIGHT);
last_millis = millis();
}
void loop(){
unsigned long mls = millis();
float delta = (mls-last_millis)/60.0;
last_millis = mls;
unsigned int length = 0;
vb.clear();
switch(game_state){
case GAME_ACTION:
wall.update(delta);
hero.update(delta, digitalRead(BUTTON) == HIGH);
if(hero.testCollision(&wall)){
strcpy(score, score_text);
sprintf(score, "%d", wall.getLevel()-1);
gameAdvance();
}
wall.draw(&vb);
hero.draw(&vb);
break;
case GAME_CRASH:
if(mls-game_state_changed > 500 || mls % 100 < 50){
for(int y=0; y<HEIGHT; y++){
for(int x=0; x<WIDTH; x++){
vb.setPixel(x, y, true);
}
}
wall.draw(&vb, true);
hero.draw(&vb, true);
}
else {
wall.draw(&vb);
hero.draw(&vb);
}
if(mls-game_state_changed > 1500){
gameAdvance();
}
break;
case GAME_OVER:
length = stringLengthInPixels(game_over_text) + WIDTH;
drawString(game_over_text, WIDTH-int(((mls-game_state_changed)/200) % length), 2, &vb);
if(button_pressed){
gameAdvance();
}
break;
case GAME_SCORE:
length = stringLengthInPixels(score) + WIDTH;
drawString(score, WIDTH-int(((mls-game_state_changed)/200) % length), 2, &vb);
if(button_pressed){
gameAdvance();
}
break;
case GAME_TITLE:
default:
length = stringLengthInPixels(title_text) + WIDTH;
drawString(title_text, WIDTH-int(((mls-game_state_changed)/200) % length), 2, &vb);
if(button_pressed){
gameAdvance();
}
break;
}
vb.flush();
button_pressed = false;
if(!button_down){
if(digitalRead(BUTTON) == HIGH){
button_down = true;
}
}
else {
if(digitalRead(BUTTON) == LOW){
button_down = false;
button_pressed = true;
}
}
}
void gameAdvance(){
if(++game_state > GAME_SCORE){
game_state = GAME_TITLE;
}
if(game_state == GAME_ACTION){
hero.reset();
wall.reset();
}
game_state_changed = millis();
}