Add Loosing animation
This commit is contained in:
1 parent
0d08b06656
commit
31f337f5a7
1 file changed
+35
-4
@@ -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 selectedAnswer[NUM_QUESTIONS]; // -1 = no answer, 0/1/2 = button within group
|
||||||
int tempSections; // current number of lit sections (0–8)
|
int tempSections; // current number of lit sections (0–8)
|
||||||
bool gameWon;
|
bool gameWon;
|
||||||
|
bool gameLost;
|
||||||
unsigned long lastInputTime; // millis() of the last button press
|
unsigned long lastInputTime; // millis() of the last button press
|
||||||
|
|
||||||
// ===================== DEBOUNCE STATE =====================
|
// ===================== DEBOUNCE STATE =====================
|
||||||
@@ -74,6 +75,8 @@ void checkButtons();
|
|||||||
void onButtonPressed(int buttonIdx);
|
void onButtonPressed(int buttonIdx);
|
||||||
void recalculateTemp();
|
void recalculateTemp();
|
||||||
bool checkWin();
|
bool checkWin();
|
||||||
|
bool checkLoss();
|
||||||
|
void playLoserAnimation();
|
||||||
void displayTemperature();
|
void displayTemperature();
|
||||||
void playTempAnimation(int fromSections, int toSections, CRGB color);
|
void playTempAnimation(int fromSections, int toSections, CRGB color);
|
||||||
void playWinAnimation();
|
void playWinAnimation();
|
||||||
@@ -101,16 +104,19 @@ void setup() {
|
|||||||
|
|
||||||
// ===================== MAIN LOOP =====================
|
// ===================== MAIN LOOP =====================
|
||||||
void loop() {
|
void loop() {
|
||||||
if (!gameWon) {
|
if (gameWon) {
|
||||||
|
playWinAnimation();
|
||||||
|
resetGame();
|
||||||
|
} else if (gameLost) {
|
||||||
|
playLoserAnimation();
|
||||||
|
resetGame();
|
||||||
|
} else {
|
||||||
if (millis() - lastInputTime >= INACTIVITY_TIMEOUT) {
|
if (millis() - lastInputTime >= INACTIVITY_TIMEOUT) {
|
||||||
Serial.println("[TIMEOUT] No input for 5 minutes — resetting.");
|
Serial.println("[TIMEOUT] No input for 5 minutes — resetting.");
|
||||||
resetGame();
|
resetGame();
|
||||||
} else {
|
} else {
|
||||||
checkButtons();
|
checkButtons();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
playWinAnimation();
|
|
||||||
resetGame();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,6 +128,7 @@ void resetGame() {
|
|||||||
}
|
}
|
||||||
tempSections = BASELINE_SECTIONS;
|
tempSections = BASELINE_SECTIONS;
|
||||||
gameWon = false;
|
gameWon = false;
|
||||||
|
gameLost = false;
|
||||||
lastInputTime = millis();
|
lastInputTime = millis();
|
||||||
displayTemperature();
|
displayTemperature();
|
||||||
Serial.println("Game reset. Temperature at baseline (4 sections).");
|
Serial.println("Game reset. Temperature at baseline (4 sections).");
|
||||||
@@ -195,6 +202,9 @@ void onButtonPressed(int buttonIdx) {
|
|||||||
if (checkWin()) {
|
if (checkWin()) {
|
||||||
gameWon = true;
|
gameWon = true;
|
||||||
Serial.println("[WIN] ALL CORRECT – You win!");
|
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;
|
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 =====================
|
// ===================== BUTTON READING =====================
|
||||||
|
|
||||||
void checkButtons() {
|
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() {
|
void fadeAllLEDs() {
|
||||||
for (int i = 0; i < NUM_LEDS; i++) {
|
for (int i = 0; i < NUM_LEDS; i++) {
|
||||||
leds[i].nscale8(250);
|
leds[i].nscale8(250);
|
||||||
|
|||||||
Reference in new issue
Block a user