In this project, we continue the development of our Simon Says pattern matching game by adding button input.
In our last project, we created a random sequence of LED flashes. We included that code in a loop that will continue to add another flash to the end of the sequence and replay the sequence. This is exactly how our game will work, except that after playing a sequence, the player will have to repeat the sequence using the buttons on the breadboard. If they do it correctly, we add a new LED to the sequence; if they do it incorrectly, the game is over.
In this project, we'll add the buttons as input, and add the code that will verify the button sequence that the player enters.
The hardware setup for this project should be the same setup that was
completed for the project. Here's a reminder:
The first thing we're going to need to do is initialize the buttons — we'll import the function we need and create the button list just like we did in :
Next, we need to add code after we play the sequence to check for whether
the user has entered the correct sequence back. There are several parts to
this, and they will all be captured in a for
loop that runs
through the sequence the player is repeating. Each time through the
for
loop, we'll need to accomplish the following things:
First, we test to see if the button pressed was the next button in the sequence.
If an incorrect button was pressed, we set a variable called
failed
to True. This will be our indication to the
while
loop that the game is over and on our next pass, we
should drop through the loop. This will also require us to change our test
in the while
loop that we already created (we'll do that in
Step #3). And we'll need to stop checking for additional button presses and
drop through the loop.
Lastly, if the player has entered a correct button in the sequence, we need to flash that LED.
Here is what the beginning of the for
loop would look
like:
Next, we check to see if player pressed the WRONG button:
If the button entered by the player was incorrect, we set a variable
called failed
to True, and then we break
— the
break
command will immediately exit the for
loop.
If the button entered by the player was correct, we need to light the LED before moving on to checking the next button entered. Here is what that code should look like:
In the code above, we turn the LED on, wait until the button is released, and turn it back off. But there's an extra line in there as well — we sleep for a very short period. This extends the time the LED is on to give a better appearance when a player presses and releases a button very quickly.
while
LoopFinally, we have to update our while
loop, as we want it to
drop through if we determine that the user made a mistake and the game is
over. Remember, we set a variable called failed
to True when the
user made a mistake, so we can simply test to see whether failed
is True
in our while
loop.
To update the while
loop, we need to first initialize our
failed variable when we start the program, and then we can update our
while
loop as follows:
Here is what the code for this project should look like at this point (with new changes highlighted with arrows) — when you run it, you will see a single LED flash, and you can press the button associated with that LED; then the pattern will repeat, adding a new LED light and you can enter the 2-light sequence; the sequence will continue to add a new LED for as long as you correctly enter the sequence:
The user is given 3 seconds of time to press each button in the sequence. Can you change this to 2 seconds?
Can you make the game even harder, by making the time the user has to
press a button decrease by 5% each time a new LED is added to the
play_order
?
If the user hits the wrong button, give them another chance: flash the LED (the wrong one) with 3 quick flashes. Then give them another 3 seconds to try again to hit the correct button.