最初コミット

諏訪子 <suwako@076.moe>、2026-03-01 17:41
コミット 01705ab77883a7e810be085cfc3eaacf90575a40 ・ファイルを見る

2個のファイル、+228 -0

追加 game.ino
@@ -0,0 +1,216 @@
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+}
追加 play.ino
@@ -0,0 +1,12 @@
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+}