master
ブランチ master
DINORUN / game / game.ino
4.7 KB、251行 生 Blame 履歴
1#include <rgb_lcd.h>
2rgb_lcd lcd;
3
4volatile int curCol, curRow;
5
6const int pinButton = 3;
7const int pinBuzzer = 7;
8
9typedef enum {
10 Start, Play, Dead
11} GameState;
12
13volatile GameState state = Start;
14
15byte dino[8] = {
16 0b00100,
17 0b01110,
18 0b00110,
19 0b01100,
20 0b01110,
21 0b11100,
22 0b01100,
23 0b10100
24};
25
26byte rock[8] = {
27 0b00010,
28 0b00110,
29 0b01110,
30 0b00111,
31 0b01110,
32 0b01110,
33 0b11110,
34 0b11111
35};
36
37byte coin[8] = {
38 0b00100,
39 0b01110,
40 0b01110,
41 0b11011,
42 0b11011,
43 0b01110,
44 0b01110,
45 0b00100
46};
47
48char dinoChar = byte(7);
49char rockChar = byte(6);
50char coinChar = byte(5);
51
52bool jump = false;
53bool jumping = false;
54bool died = false;
55unsigned long jumpTime = 0;
56const unsigned long jumpDuration = 350;
57
58int score = 0;
59int dinoY = 1;
60
61const int initRocks[] = { 5, 12, 16, 19, 23, 27, 30, 34, 36, 39, 41, 44, 47, 49, 52 };
62const int countRocks = sizeof(initRocks) / sizeof(initRocks[0]);
63int rockX[countRocks];
64const int initCoins[] = { 8, 17, 25, 29, 32, 40, 43, 48, 55};
65const int countCoins = sizeof(initCoins) / sizeof(initCoins[0]);
66bool coinCollected[countCoins] = {};
67int coinX[countCoins];
68
69int gameSpeed = 200;
70unsigned long lastMove = 0;
71
72unsigned long deathShowStart = 0;
73const unsigned long deathScreenMinTime = 2000;
74
75volatile bool btnPressed = false;
76
77void setup() {
78 pinMode(pinBuzzer, OUTPUT);
79 attachInterrupt(digitalPinToInterrupt(pinButton), buttonHandler, RISING);
80 lcd.begin(16, 2);
81 lcd.createChar(7, dino);
82 lcd.createChar(6, rock);
83 lcd.createChar(5, coin);
84 resetGame();
85}
86
87void buttonHandler() {
88 btnPressed = true;
89}
90
91void resetGame() {
92 score = 0;
93 jump = false;
94 jumping = false;
95 jumpTime = 0;
96 dinoY = 1;
97 lastMove = millis();
98 deathShowStart = 0;
99 lcd.setRGB(0, 0, 255);
100
101 for (int i = 0; i < countRocks; ++i) {
102 rockX[i] = initRocks[i];
103 }
104
105 for (int i = 0; i < countCoins; ++i) {
106 coinX[i] = initCoins[i];
107 coinCollected[i] = false;
108 }
109}
110
111void titlescreen() {
112 char buf[16];
113 lcd.clear();
114 lcd.setCursor(4, 0);
115 lcd.print("DINO RUN");
116 lcd.setCursor(0, 1);
117 sprintf(buf, "%c %c %c %c", dinoChar, rockChar, coinChar, rockChar);
118 lcd.print(buf);
119
120 if (btnPressed) {
121 btnPressed = false;
122 state = Play;
123 resetGame();
124 }
125}
126
127void gameplay() {
128 unsigned long now = millis();
129
130 if (btnPressed) {
131 btnPressed = false;
132 if (!jumping) {
133 tone(pinBuzzer, 523, 100);
134 jump = true;
135 jumpTime = now;
136 }
137 }
138
139 if (jump) {
140 jumping = true;
141 dinoY = 0;
142 if (now - jumpTime >= jumpDuration) {
143 jump = false;
144 jumping = false;
145 dinoY = 1;
146 }
147 }
148
149 if (now - lastMove >= (unsigned long)gameSpeed) {
150 lastMove = now;
151
152 for (int i = 0; i < countRocks; ++i) {
153 rockX[i]--;
154 if (rockX[i] < -1) rockX[i] = 28 + random(12, 22);
155 }
156
157 for (int i = 0; i < countCoins; ++i) {
158 coinX[i]--;
159 if (coinX[i] < -1) coinX[i] = 32 + random(15, 30);
160 }
161 }
162
163 lcd.clear();
164
165 lcd.setCursor(3, 0);
166 char scoreBuf[16];
167 sprintf(scoreBuf, "SCORE: %3d", score);
168 lcd.print(scoreBuf);
169
170 char player[2];
171 char obstacle[2];
172 char collect[2];
173
174 sprintf(player, "%c", dinoChar);
175 sprintf(obstacle, "%c", rockChar);
176 sprintf(collect, "%c", coinChar);
177
178 lcd.setCursor(1, dinoY);
179 lcd.print(player);
180
181 bool hit = false;
182 for (int i = 0; i < countRocks; ++i) {
183 int x = rockX[i];
184 if (x >= 0 && x < 16) {
185 lcd.setCursor(x, 1);
186 lcd.print(obstacle);
187 if (dinoY == 1 && x == 1) {
188 hit = true;
189 }
190 }
191 }
192
193 for (int i = 0; i < countCoins; ++i) {
194 int x = coinX[i];
195 if (x >= 0 && x < 16) {
196 lcd.setCursor(x, 1);
197 if (dinoY == 1 && x == 1 && !coinCollected[i]) {
198 tone(pinBuzzer, 880, 80);
199 score++;
200 coinCollected[i] = true;
201 } else if (!coinCollected[i]) lcd.print(collect);
202 }
203 }
204
205 if (hit) {
206 unsigned long n = millis();
207 lcd.setRGB(255, 0, 0);
208 tone(pinBuzzer, 698, 1000);
209
210 if (n >= 2000) {
211 state = Dead;
212 deathShowStart = now;
213 }
214 }
215}
216
217void dead() {
218 unsigned long now = millis();
219 lcd.setRGB(0, 0, 255);
220
221 lcd.clear();
222 char buf[16];
223 lcd.setCursor(3, 0);
224 sprintf(buf, "SCORE: %3d", score);
225 lcd.print(buf);
226 lcd.setCursor(4, 1);
227 lcd.print("GAMEOVER");
228 if (now - deathShowStart < deathScreenMinTime) return;
229
230 if (btnPressed) {
231 btnPressed = false;
232 state = Start;
233 resetGame();
234 }
235}
236
237void loop() {
238 delay(200);
239 switch (state) {
240 case Start:
241 titlescreen();
242 break;
243 case Play:
244 gameplay();
245 break;
246 case Dead:
247 dead();
248 break;
249 }
250 delay(200);
251}