Add Loosing animation

This commit is contained in:
Andreas committed 2026-04-14 14:32:11 +02:00
1 parent 0d08b06656
commit 31f337f5a7
1 file changed
+35 -4
+35 -4
View File
@@ -61,6 +61,7 @@ const unsigned long INACTIVITY_TIMEOUT = 300000; // 5 minutes in ms — auto-re
int selectedAnswer[NUM_QUESTIONS]; // -1 = no answer, 0/1/2 = button within group
int tempSections; // current number of lit sections (0–8)
bool gameWon;
bool gameLost;
unsigned long lastInputTime; // millis() of the last button press
// ===================== DEBOUNCE STATE =====================
@@ -74,6 +75,8 @@ void checkButtons();
void onButtonPressed(int buttonIdx);
void recalculateTemp();
bool checkWin();
bool checkLoss();
void playLoserAnimation();
void displayTemperature();
void playTempAnimation(int fromSections, int toSections, CRGB color);
void playWinAnimation();
@@ -101,16 +104,19 @@ void setup() {
// ===================== MAIN LOOP =====================
void loop() {
if (!gameWon) {
if (gameWon) {
playWinAnimation();
resetGame();
} else if (gameLost) {
playLoserAnimation();
resetGame();
} else {
if (millis() - lastInputTime >= INACTIVITY_TIMEOUT) {
Serial.println("[TIMEOUT] No input for 5 minutes — resetting.");
resetGame();
} else {
checkButtons();
}
} else {
playWinAnimation();
resetGame();
}
}
@@ -122,6 +128,7 @@ void resetGame() {
}
tempSections = BASELINE_SECTIONS;
gameWon = false;
gameLost = false;
lastInputTime = millis();
displayTemperature();
Serial.println("Game reset. Temperature at baseline (4 sections).");
@@ -195,6 +202,9 @@ void onButtonPressed(int buttonIdx) {
if (checkWin()) {
gameWon = true;
Serial.println("[WIN] ALL CORRECT – You win!");
} else if (checkLoss()) {
gameLost = true;
Serial.println("[LOSS] All answered wrong/half — game over.");
}
}
@@ -221,6 +231,15 @@ bool checkWin() {
return true;
}
// Loss: all 4 questions answered, none correct.
bool checkLoss() {
for (int q = 0; q < NUM_QUESTIONS; q++) {
if (selectedAnswer[q] == -1) return false; // unanswered
if (getAnswerType(q, selectedAnswer[q]) == ANSWER_CORRECT) return false; // at least one correct
}
return true;
}
// ===================== BUTTON READING =====================
void checkButtons() {
@@ -376,6 +395,18 @@ void playWinAnimation() {
}
}
// Red flashing across all LEDs, then fades out.
void playLoserAnimation() {
for (int rep = 0; rep < 5; rep++) {
for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, CRGB(180, 0, 0));
FastLED.show();
delay(200);
for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, CRGB::Black);
FastLED.show();
delay(150);
}
}
void fadeAllLEDs() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(250);