SIMON 5
In this project, we continue the development of our Simon-like pattern matching game by adding button input.
Speaker & Audio Cable
I/O (Input/Output) RaspberrySTEM Cell
Having issues? Check out the Troubleshooting Guide.

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.

Programming Implementation

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:

  1. First, we test to see if the button pressed was the next button in the sequence.

  2. 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.

  3. 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:




wait_many() Function

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 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.


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 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 -- 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:



home | prev | next