Add Game Won Screen, contributes to #4

pull/14/head
Dennis Bücker 1 year ago
parent 151f62a147
commit 552ec98b34
  1. 29
      src/GameWonState.cpp
  2. 1
      src/MCUInfo.h
  3. 31
      src/SimonSays.cpp
  4. 18
      src/SimonSays.h

@ -0,0 +1,29 @@
#include "SimonSays.h"
#include <FastLED.h>
GameWonState::GameWonState():
hue{0}
{}
void GameWonState::enter(SimonSays &simonSays)
{
Serial.println("Entered Game won state!");
simonSays.simonDisplay.setDisplayMode(0); // set custom mode
fill_rainbow(rainbowColors,25,hue++);
for (int pixelIdx = 0; pixelIdx <25;pixelIdx++)
{
simonSays.simonDisplay.drawPixel(pixelIdx,rainbowColors[pixelIdx]);
}
}
void GameWonState::update(SimonSays &simonSays)
{
fill_rainbow(rainbowColors, 25, hue++);
for (int pixelIdx = 0; pixelIdx < 25; pixelIdx++)
{
simonSays.simonDisplay.drawPixel(pixelIdx, rainbowColors[pixelIdx]);
}
}

@ -8,5 +8,6 @@ struct MCUInfo
static constexpr float longButtonPress = 700; // millisecond
static constexpr int maxColorArrayLength = 10; //maximal array length
static constexpr int maxLevel = 10; // Maximum Level number after game is going to 'win' state.
};

@ -5,20 +5,23 @@
namespace {
ShowColorSequence showColorSequence;
EnterColorSequence enterColorSequence;
GameWonState gameWonState;
}
SimonSays::SimonSays(SimonDisplay & _simonDisplay):
stateList {
&showColorSequence,
&enterColorSequence
&enterColorSequence,
&gameWonState
},
currentState{0},
currentSequence{999,999,999,999,999,999,999,999,999,999}, // dummy sequence of 10 entries
currentSequenceLength{1},
currentLevel{0},
currentSpeed{100},
maxLevel{MCUInfo::maxLevel},
winStatus{false},
simonDisplay(_simonDisplay)
{
@ -41,12 +44,23 @@ void SimonSays::begin() {
void SimonSays::goToNextPhase() {
// This advances to the next phase (which is a state)..
// Serial.print("Leave state ");
// Serial.print(currentState);
stateList[currentState] -> leave(*this);
stateList[currentState]->leave(*this);
int numberOfStates = sizeof(stateList) / sizeof(stateList[0]);
currentState = (currentState + 1) % numberOfStates; // Advance to next phase and go to first one if at end
// Check if Game is won
if (winStatus)
{
currentState = numberOfStates-1; // Set win state idx to last saved state
}
else
{
currentState = (currentState + 1) % (numberOfStates - 1); // Advance to next phase and go to first one if at end, not counting the win state (therefore -1)
}
// Enter selected state
stateList[currentState] -> enter(*this);
// Serial.print(" and enter state ");
// Serial.println(currentState);
@ -145,6 +159,13 @@ void SimonSays::resetGameAfterLoose() {
void SimonSays::advanceLevel() {
currentLevel++;
if (currentLevel >= maxLevel)
{
// Goal is reached, go to win state
winStatus = true;
return;
}
Serial.print("Current Level is ");
Serial.println(currentLevel);

@ -48,6 +48,18 @@ class EnterColorSequence final : public GameState {
int arrayOfUniqueColors[MCUInfo::maxColorArrayLength]; // will contain an array of all unique colors
};
class GameWonState final : public GameState
{
public:
GameWonState();
void enter(SimonSays &simonSays) override;
void update(SimonSays &simonSays) override;
void leave(SimonSays &simonSays) override{};
private:
uint8_t hue;
CRGB rainbowColors[25];
};
// This is the main game class. Different states of the game are called from here.
@ -55,10 +67,10 @@ class SimonSays
{
public:
SimonSays(SimonDisplay &simonDisplay);
~SimonSays() {};
~SimonSays(){};
// State logic
GameState *stateList[2];
GameState *stateList[3];
int currentState;
// possible functions to call
@ -73,6 +85,8 @@ public:
int currentSequenceLength;
int currentLevel;
int currentSpeed;
int maxLevel;
bool winStatus; // Is set true, if overall Game is won.
// Display device logic for LEDs etc
SimonDisplay &simonDisplay;

Loading…
Cancel
Save