delay()はタイマー化 (#1)

Reviewed-on: http://192.168.10.104:3000/suwako/DINORUN/pulls/1
Co-authored-by: 諏訪子 <suwako@076.moe>
Co-committed-by: 諏訪子 <suwako@076.moe>

諏訪子 <suwako@076.moe>、2026-03-03 12:49
コミット d45e6cf22c09b061d47a0dd117777493672abd3c ・親 f8362d8 ・ファイルを見る

3個のファイル、+249 -228

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