From dfbe5f1043529c6bbe03b200b7c559b78ad34b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Sun, 26 Dec 2021 23:26:31 +0100 Subject: [PATCH] SpaceInvaders: Buggy flashes --- src/display/Display.h | 1 - src/mode/SpaceInvaders/SpaceInvaders.h | 24 +++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/display/Display.h b/src/display/Display.h index 28f6de6..cddbca4 100644 --- a/src/display/Display.h +++ b/src/display/Display.h @@ -43,7 +43,6 @@ public: void setPixelColor(uint8_t x, uint8_t y, uint32_t color) { if (x >= width || y >= height) { - Serial.printf("ERROR: Cannot set pixel (%d/%d) in matrix (%d/%d).\n", x, y, width, height); return; } if ((y % 2) != 0) { diff --git a/src/mode/SpaceInvaders/SpaceInvaders.h b/src/mode/SpaceInvaders/SpaceInvaders.h index 120f27c..bff3d84 100644 --- a/src/mode/SpaceInvaders/SpaceInvaders.h +++ b/src/mode/SpaceInvaders/SpaceInvaders.h @@ -6,7 +6,8 @@ #include "mode/Mode.h" struct Rocket { - volatile bool alive; + bool alive; + uint16_t flash; uint8_t x; uint8_t y; }; @@ -79,7 +80,7 @@ public: Serial.println("WINNER!"); reset(); } - if (swarmY + (invadersCountY - 1) * 2 >= display->height - 1) { + if (swarmY + (invadersCountY - 1) * 2 >= display->height - 1) { // TODO this is only correct if there still are any invaders in the last row (otherwise we "Game Over" too early) Serial.println("GAME OVER"); reset(); } @@ -96,6 +97,12 @@ public: } else { rocket->y -= 1; } + } else if (rocket->flash > 0) { + if (rocket->flash < dt) { + rocket->flash = 0; + } else { + rocket->flash -= dt; + } } } } @@ -167,6 +174,7 @@ public: for (Invader *invader = swarmBegin; invader < swarmEnd; invader++) { if (invader->alive && rocket->alive && collide(rocket, invader)) { rocket->alive = false; + rocket->flash = 1000; invader->alive = false; invadersAlive--; // Serial.printf("Killed invader at (%2u/%2u): %2u/%2u alive.\n", invader->x, invader->y, invadersAlive, invadersCountX * invadersCountY); @@ -197,10 +205,15 @@ public: void drawRockets() { for (Rocket *rocket = rocketsBegin; rocket < rocketsEnd; rocket++) { - if (!rocket->alive) { - continue; + if (rocket->alive) { + display->set(rocket->x, rocket->y, 255, 255, 0); + } else if (rocket->flash > 0) { + display->set(rocket->x - 1, rocket->y - 1, rocket->flash, rocket->flash, rocket->flash); + display->set(rocket->x - 1, rocket->y + 1, rocket->flash, rocket->flash, rocket->flash); + display->set(rocket->x + 0, rocket->y + 0, rocket->flash, rocket->flash, rocket->flash); + display->set(rocket->x + 1, rocket->y + 1, rocket->flash, rocket->flash, rocket->flash); + display->set(rocket->x + 1, rocket->y - 1, rocket->flash, rocket->flash, rocket->flash); } - display->set(rocket->x, rocket->y, 255, 255, 0); } } @@ -219,6 +232,7 @@ public: rocketRuntime = 0; for (Rocket *rocket = rocketsBegin; rocket < rocketsEnd; rocket++) { rocket->alive = false; + rocket->flash = 0; rocket->x = 0; rocket->y = 0; }