#include #include #include // OLED U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2( U8G2_R0, U8X8_PIN_NONE ); // Boss size #define BOSS_W 22 #define BOSS_H 19 // Boss pixel art const char bossMonster[BOSS_H][BOSS_W + 1] = { "........#####..###....", ".......##.##.######...", "###...#####..#####....", "##############.##.....", "##.##############.....", ".#..###.#.#..#####....", ".#####..####..####..##", "..##...######....#####", ".##...###.###.##.#####", ".#....###.###..#######", ".#....######....#####.", ".#.#.........##.####..", ".#######......#####...", ".##.#.#.#...##.##....", ".##.####..##.####.....", ".#.#.#.....####.##....", ".#.......######.##....", "..##########.######...", ".....###.##...#####..." }; // Boss data struct Boss { int x; int y; int verticalDirection; bool charging; bool returning; unsigned long lastCharge; }; Boss boss; // Start boss void startBoss() { boss.x = 128; boss.y = 25; boss.verticalDirection = 1; boss.charging = false; boss.returning = false; boss.lastCharge = millis(); } // Update boss void updateBoss() { // Enter from right if ( boss.x > 100 && !boss.charging && !boss.returning ) { boss.x -= 1; return; } // Charge every 5 seconds if ( !boss.charging && !boss.returning && millis() - boss.lastCharge > 5000 ) { boss.charging = true; boss.lastCharge = millis(); } // Move quickly to the left if (boss.charging) { boss.x -= 3; if (boss.x <= 55) { boss.x = 55; boss.charging = false; boss.returning = true; } return; } // Return to the right if (boss.returning) { boss.x += 3; if (boss.x >= 100) { boss.x = 100; boss.returning = false; boss.lastCharge = millis(); } return; } // Move up and down boss.y += boss.verticalDirection; if (boss.y <= 12) { boss.y = 12; boss.verticalDirection = 1; } if (boss.y >= 44) { boss.y = 44; boss.verticalDirection = -1; } } // Draw boss void drawBoss() { for (int y = 0; y < BOSS_H; y++) { for (int x = 0; x < BOSS_W; x++) { if (bossMonster[y][x] == '#') { u8g2.drawPixel( boss.x + x, boss.y + y ); } } } } // Setup void setup() { Wire.begin(); u8g2.begin(); startBoss(); } // Loop void loop() { updateBoss(); u8g2.clearBuffer(); drawBoss(); u8g2.sendBuffer(); delay(30); }