SpaceInvaders: Buggy flashes

This commit is contained in:
Patrick Haßel 2021-12-26 23:26:31 +01:00
parent 10e836c5f8
commit dfbe5f1043
2 changed files with 19 additions and 6 deletions

View File

@ -43,7 +43,6 @@ public:
void setPixelColor(uint8_t x, uint8_t y, uint32_t color) { void setPixelColor(uint8_t x, uint8_t y, uint32_t color) {
if (x >= width || y >= height) { if (x >= width || y >= height) {
Serial.printf("ERROR: Cannot set pixel (%d/%d) in matrix (%d/%d).\n", x, y, width, height);
return; return;
} }
if ((y % 2) != 0) { if ((y % 2) != 0) {

View File

@ -6,7 +6,8 @@
#include "mode/Mode.h" #include "mode/Mode.h"
struct Rocket { struct Rocket {
volatile bool alive; bool alive;
uint16_t flash;
uint8_t x; uint8_t x;
uint8_t y; uint8_t y;
}; };
@ -79,7 +80,7 @@ public:
Serial.println("WINNER!"); Serial.println("WINNER!");
reset(); 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"); Serial.println("GAME OVER");
reset(); reset();
} }
@ -96,6 +97,12 @@ public:
} else { } else {
rocket->y -= 1; 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++) { for (Invader *invader = swarmBegin; invader < swarmEnd; invader++) {
if (invader->alive && rocket->alive && collide(rocket, invader)) { if (invader->alive && rocket->alive && collide(rocket, invader)) {
rocket->alive = false; rocket->alive = false;
rocket->flash = 1000;
invader->alive = false; invader->alive = false;
invadersAlive--; invadersAlive--;
// Serial.printf("Killed invader at (%2u/%2u): %2u/%2u alive.\n", invader->x, invader->y, invadersAlive, invadersCountX * invadersCountY); // 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() { void drawRockets() {
for (Rocket *rocket = rocketsBegin; rocket < rocketsEnd; rocket++) { for (Rocket *rocket = rocketsBegin; rocket < rocketsEnd; rocket++) {
if (!rocket->alive) { if (rocket->alive) {
continue;
}
display->set(rocket->x, rocket->y, 255, 255, 0); 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);
}
} }
} }
@ -219,6 +232,7 @@ public:
rocketRuntime = 0; rocketRuntime = 0;
for (Rocket *rocket = rocketsBegin; rocket < rocketsEnd; rocket++) { for (Rocket *rocket = rocketsBegin; rocket < rocketsEnd; rocket++) {
rocket->alive = false; rocket->alive = false;
rocket->flash = 0;
rocket->x = 0; rocket->x = 0;
rocket->y = 0; rocket->y = 0;
} }