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.
Step #1: Add the initialization code
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 Many Buttons:
Step #2: Check the button sequence entered by the player
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:
The wait_many() function takes a list of buttons as a parameter, will wait for any of them to be pressed and will then return the index into the buttons list of the button that was pressed.
See the API reference for more details.
Next, we check to see if the button_pressed was correct. Though, to make the code a bit easier to follow, we're going to check to see if the button_pressed was actually incorrect:
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:
Step #3: Update the while: loop
Finally, 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 == 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: