M5Stackってなに?
EspressifのESP32、SDカード、ボタン3つ、USBやタイプCのコネクターなどいろいろ詰め込まれているモジュールです。
Wi-Fiがついているので、WiFi環境で動かしたい人にとっては強い味方。
特に画面付きなのがうれしいところ!
Arduino IDEで動かすことができるのも特徴ですね。
M5Stackをとりあえず動かしてみよう
やり方にか関しては、公式ページにて詳しく解説してあるので、こちらを参考にしてください。
M5Stack Docs - The reference docs for M5Stack products.
The reference docs for M5Stack products. Quick start, get the detailed information or instructions such as IDE,UIFLOW,Arduino. The tutorials for M5Burner, Firmw...
プログラム
フラッピーバードのゲームがプレイできるプログラム
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
// By Ponticelli Domenico. // https://github.com/pcelli85/M5Stack_FlappyBird_game #include <M5Stack.h> #include <EEPROM.h> #define TFTW 320 // screen width #define TFTH 240 // screen height #define TFTW2 160 // half screen width #define TFTH2 120 // half screen height // game constant #define SPEED 1 #define GRAVITY 9.8 #define JUMP_FORCE 2.15 #define SKIP_TICKS 20.0 // 1000 / 50fps #define MAX_FRAMESKIP 5 // bird size #define BIRDW 16 // bird width #define BIRDH 16 // bird height #define BIRDW2 8 // half width #define BIRDH2 8 // half height // pipe size #define PIPEW 24 // pipe width #define GAPHEIGHT 42 // pipe gap height // floor size #define FLOORH 30 // floor height (from bottom of the screen) // grass size #define GRASSH 4 // grass height (inside floor, starts at floor y) int maxScore = 0; const int buttonPin = 2; // background const unsigned int BCKGRDCOL = M5.Lcd.color565(138,235,244); // bird const unsigned int BIRDCOL = M5.Lcd.color565(255,254,174); // pipe const unsigned int PIPECOL = M5.Lcd.color565(99,255,78); // pipe highlight const unsigned int PIPEHIGHCOL = M5.Lcd.color565(250,255,250); // pipe seam const unsigned int PIPESEAMCOL = M5.Lcd.color565(0,0,0); // floor const unsigned int FLOORCOL = M5.Lcd.color565(246,240,163); // grass (col2 is the stripe color) const unsigned int GRASSCOL = M5.Lcd.color565(141,225,87); const unsigned int GRASSCOL2 = M5.Lcd.color565(156,239,88); // bird sprite // bird sprite colors (Cx name for values to keep the array readable) #define C0 BCKGRDCOL #define C1 M5.Lcd.color565(195,165,75) #define C2 BIRDCOL #define C3 TFT_WHITE #define C4 TFT_RED #define C5 M5.Lcd.color565(251,216,114) static unsigned int birdcol[] = { C0, C0, C1, C1, C1, C1, C1, C0, C0, C0, C1, C1, C1, C1, C1, C0, C0, C1, C2, C2, C2, C1, C3, C1, C0, C1, C2, C2, C2, C1, C3, C1, C0, C2, C2, C2, C2, C1, C3, C1, C0, C2, C2, C2, C2, C1, C3, C1, C1, C1, C1, C2, C2, C3, C1, C1, C1, C1, C1, C2, C2, C3, C1, C1, C1, C2, C2, C2, C2, C2, C4, C4, C1, C2, C2, C2, C2, C2, C4, C4, C1, C2, C2, C2, C1, C5, C4, C0, C1, C2, C2, C2, C1, C5, C4, C0, C0, C1, C2, C1, C5, C5, C5, C0, C0, C1, C2, C1, C5, C5, C5, C0, C0, C0, C1, C5, C5, C5, C0, C0, C0, C0, C1, C5, C5, C5, C0, C0}; // bird structure static struct BIRD { long x, y, old_y; long col; float vel_y; } bird; // pipe structure static struct PIPES { long x, gap_y; long col; } pipes; // score int score; // temporary x and y var static short tmpx, tmpy; // --------------- // draw pixel // --------------- // faster drawPixel method by inlining calls and using setAddrWindow and pushColor // using macro to force inlining #define drawPixel(a, b, c) M5.Lcd.setAddrWindow(a, b, a, b); M5.Lcd.pushColor(c) void setup() { // put your setup code here, to run once: M5.begin(); resetMaxScore(); } void loop() { // put your main code here, to run repeatedly: game_start(); game_loop(); game_over(); } // --------------- // game loop // --------------- void game_loop() { // =============== // prepare game variables // draw floor // =============== // instead of calculating the distance of the floor from the screen height each time store it in a variable unsigned char GAMEH = TFTH - FLOORH; // draw the floor once, we will not overwrite on this area in-game // black line M5.Lcd.drawFastHLine(0, GAMEH, TFTW, TFT_BLACK); // grass and stripe M5.Lcd.fillRect(0, GAMEH+1, TFTW2, GRASSH, GRASSCOL); M5.Lcd.fillRect(TFTW2, GAMEH+1, TFTW2, GRASSH, GRASSCOL2); // black line M5.Lcd.drawFastHLine(0, GAMEH+GRASSH, TFTW, TFT_BLACK); // mud M5.Lcd.fillRect(0, GAMEH+GRASSH+1, TFTW, FLOORH-GRASSH, FLOORCOL); // grass x position (for stripe animation) long grassx = TFTW; // game loop time variables double delta, old_time, next_game_tick, current_time; next_game_tick = current_time = millis(); int loops; // passed pipe flag to count score bool passed_pipe = false; // temp var for setAddrWindow unsigned char px; while (1) { loops = 0; while( millis() > next_game_tick && loops < MAX_FRAMESKIP) { // =============== // input // =============== if (M5.BtnB.wasPressed()) { // if the bird is not too close to the top of the screen apply jump force if (bird.y > BIRDH2*0.5) bird.vel_y = -JUMP_FORCE; // else zero velocity else bird.vel_y = 0; } M5.update(); // =============== // update // =============== // calculate delta time // --------------- old_time = current_time; current_time = millis(); delta = (current_time-old_time)/1000; // bird // --------------- bird.vel_y += GRAVITY * delta; bird.y += bird.vel_y; // pipe // --------------- pipes.x -= SPEED; // if pipe reached edge of the screen reset its position and gap if (pipes.x < -PIPEW) { pipes.x = TFTW; pipes.gap_y = random(10, GAMEH-(10+GAPHEIGHT)); } // --------------- next_game_tick += SKIP_TICKS; loops++; } // =============== // draw // =============== // pipe // --------------- // we save cycles if we avoid drawing the pipe when outside the screen if (pipes.x >= 0 && pipes.x < TFTW) { // pipe color M5.Lcd.drawFastVLine(pipes.x+3, 0, pipes.gap_y, PIPECOL); M5.Lcd.drawFastVLine(pipes.x+3, pipes.gap_y+GAPHEIGHT+1, GAMEH-(pipes.gap_y+GAPHEIGHT+1), PIPECOL); // highlight M5.Lcd.drawFastVLine(pipes.x, 0, pipes.gap_y, PIPEHIGHCOL); M5.Lcd.drawFastVLine(pipes.x, pipes.gap_y+GAPHEIGHT+1, GAMEH-(pipes.gap_y+GAPHEIGHT+1), PIPEHIGHCOL); // bottom and top border of pipe drawPixel(pipes.x, pipes.gap_y, PIPESEAMCOL); drawPixel(pipes.x, pipes.gap_y+GAPHEIGHT, PIPESEAMCOL); // pipe seam drawPixel(pipes.x, pipes.gap_y-6, PIPESEAMCOL); drawPixel(pipes.x, pipes.gap_y+GAPHEIGHT+6, PIPESEAMCOL); drawPixel(pipes.x+3, pipes.gap_y-6, PIPESEAMCOL); drawPixel(pipes.x+3, pipes.gap_y+GAPHEIGHT+6, PIPESEAMCOL); } // erase behind pipe if (pipes.x <= TFTW) M5.Lcd.drawFastVLine(pipes.x+PIPEW, 0, GAMEH, BCKGRDCOL); // bird // --------------- tmpx = BIRDW-1; do { px = bird.x+tmpx+BIRDW; // clear bird at previous position stored in old_y // we can't just erase the pixels before and after current position // because of the non-linear bird movement (it would leave 'dirty' pixels) tmpy = BIRDH - 1; do { drawPixel(px, bird.old_y + tmpy, BCKGRDCOL); } while (tmpy--); // draw bird sprite at new position tmpy = BIRDH - 1; do { drawPixel(px, bird.y + tmpy, birdcol[tmpx + (tmpy * BIRDW)]); } while (tmpy--); } while (tmpx--); // save position to erase bird on next draw bird.old_y = bird.y; // grass stripes // --------------- grassx -= SPEED; if (grassx < 0) grassx = TFTW; M5.Lcd.drawFastVLine( grassx %TFTW, GAMEH+1, GRASSH-1, GRASSCOL); M5.Lcd.drawFastVLine((grassx+64)%TFTW, GAMEH+1, GRASSH-1, GRASSCOL2); // =============== // collision // =============== // if the bird hit the ground game over if (bird.y > GAMEH-BIRDH) break; // checking for bird collision with pipe if (bird.x+BIRDW >= pipes.x-BIRDW2 && bird.x <= pipes.x+PIPEW-BIRDW) { // bird entered a pipe, check for collision if (bird.y < pipes.gap_y || bird.y+BIRDH > pipes.gap_y+GAPHEIGHT) break; else passed_pipe = true; } // if bird has passed the pipe increase score else if (bird.x > pipes.x+PIPEW-BIRDW && passed_pipe) { passed_pipe = false; // erase score with background color M5.Lcd.setTextColor(BCKGRDCOL); M5.Lcd.setCursor( TFTW2, 4); M5.Lcd.print(score); // set text color back to white for new score M5.Lcd.setTextColor(TFT_WHITE); // increase score since we successfully passed a pipe score++; } // update score // --------------- M5.Lcd.setCursor( TFTW2, 4); M5.Lcd.print(score); } // add a small delay to show how the player lost delay(1200); } // --------------- // game start // --------------- void game_start() { M5.Lcd.fillScreen(TFT_BLACK); M5.Lcd.fillRect(10, TFTH2 - 20, TFTW-20, 1, TFT_WHITE); M5.Lcd.fillRect(10, TFTH2 + 32, TFTW-20, 1, TFT_WHITE); M5.Lcd.setTextColor(TFT_WHITE); M5.Lcd.setTextSize(3); // half width - num char * char width in pixels M5.Lcd.setCursor( TFTW2-(6*9), TFTH2 - 16); M5.Lcd.println("FLAPPY"); M5.Lcd.setTextSize(3); M5.Lcd.setCursor( TFTW2-(6*9), TFTH2 + 8); M5.Lcd.println("-BIRD-"); M5.Lcd.setTextSize(2); M5.Lcd.setCursor( 10, TFTH2 - 36); M5.Lcd.println("M5Stack"); M5.Lcd.setCursor( TFTW2 - (17*9), TFTH2 + 36); M5.Lcd.println("Premi il bottone centrale"); while (1) { // wait for push button if(M5.BtnB.wasPressed()) { break; } M5.update(); } // init game settings game_init(); } void game_init() { // clear screen M5.Lcd.fillScreen(BCKGRDCOL); // reset score score = 0; // init bird bird.x = 144; bird.y = bird.old_y = TFTH2 - BIRDH; bird.vel_y = -JUMP_FORCE; tmpx = tmpy = 0; // generate new random seed for the pipe gape randomSeed(analogRead(0)); // init pipe pipes.x = 0; pipes.gap_y = random(20, TFTH-60); } // --------------- // game over // --------------- void game_over() { M5.Lcd.fillScreen(TFT_BLACK); EEPROM_Read(&maxScore,0); if(score>maxScore) { EEPROM_Write(&score,0); maxScore = score; M5.Lcd.setTextColor(TFT_RED); M5.Lcd.setTextSize(2); M5.Lcd.setCursor( TFTW2 - (13*6), TFTH2 - 26); M5.Lcd.println("NEW HIGHSCORE"); } M5.Lcd.setTextColor(TFT_WHITE); M5.Lcd.setTextSize(3); // half width - num char * char width in pixels M5.Lcd.setCursor( TFTW2 - (9*9), TFTH2 - 6); M5.Lcd.println("GAME OVER"); M5.Lcd.setTextSize(2); M5.Lcd.setCursor( 10, 10); M5.Lcd.print("score: "); M5.Lcd.print(score); M5.Lcd.setCursor( TFTW2 - (12*6), TFTH2 + 18); M5.Lcd.println("press button"); M5.Lcd.setCursor( 10, 28); M5.Lcd.print("Max Score:"); M5.Lcd.print(maxScore); while (1) { // wait for push button if(M5.BtnB.wasPressed()) { break; } M5.update(); } } void resetMaxScore() { EEPROM_Write(&maxScore,0); } void EEPROM_Write(int *num, int MemPos) { byte ByteArray[2]; memcpy(ByteArray, num, 2); for(int x = 0; x < 2; x++) { EEPROM.write((MemPos * 2) + x, ByteArray[x]); } } void EEPROM_Read(int *num, int MemPos) { byte ByteArray[2]; for(int x = 0; x < 2; x++) { ByteArray[x] = EEPROM.read((MemPos * 2) + x); } memcpy(num, ByteArray, 2); } |
ディスプレイの動作確認をするプログラム
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 |
#include <M5Stack.h> // the setup routine runs once when M5Stack starts up void setup() { // initialize the M5Stack object M5.begin(); // Lcd display M5.Lcd.fillScreen(WHITE); delay(500); M5.Lcd.fillScreen(RED); delay(500); M5.Lcd.fillScreen(GREEN); delay(500); M5.Lcd.fillScreen(BLUE); delay(500); M5.Lcd.fillScreen(BLACK); delay(500); // text print M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(10, 10); M5.Lcd.setTextColor(WHITE); M5.Lcd.setTextSize(1); M5.Lcd.printf("Display Test!"); // draw graphic delay(1000); M5.Lcd.drawRect(100, 100, 50, 50, BLUE); delay(1000); M5.Lcd.fillRect(100, 100, 50, 50, BLUE); delay(1000); M5.Lcd.drawCircle(100, 100, 50, RED); delay(1000); M5.Lcd.fillCircle(100, 100, 50, RED); delay(1000); M5.Lcd.drawTriangle(30, 30, 180, 100, 80, 150, YELLOW); delay(1000); M5.Lcd.fillTriangle(30, 30, 180, 100, 80, 150, YELLOW); } // the loop routine runs over and over again forever void loop(){ //rand draw M5.Lcd.fillTriangle(random(M5.Lcd.width()-1), random(M5.Lcd.height()-1), random(M5.Lcd.width()-1), random(M5.Lcd.height()-1), random(M5.Lcd.width()-1), random(M5.Lcd.height()-1), random(0xfffe)); M5.update(); } |
どちらもスケッチ例のM5Stackに入っているプログラムです。
M5Stackで何ができるの?
ここまで出来たら次は何ができるのって方がいると思います。
私は、WifiとFirebaseを使って、BME280で取得した気温、気圧、湿度をいつでもスマホで確認できるようなシステムを作ったりといろいろと今までのマイコン開発のレベルアップができるようになりましたよ。
その記事に関しても時期に上げていくので皆さんもぜひ試してみてください。
コメント