SIMON 5

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:

INSERT CODE BLOCK

# Initialize the LEDs and sequence list

from rstem.button import Button

buttons = [Button(27), Button(23), Button(24), Button(22)]

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.  We can do this by creating a for loop that runs through the entire sequence of buttons -- each time through the loop we check to see if the button pressed matches the next button in the sequence.  If the button pressed is incorrect, we end the game and drop through the loop.  If the button pressed is correct, we light that LED and move on to check the next button press.

Here is what the beginning of the for loop would look like:

INSERT CODE BLOCK

#Wait for user to repeat

for i in play order:

    button_pressed = Button.wait_many(buttons, timeout=3)

Notice we use the function wait_many() for this.  

Talk about wait_many()...

Next, we check to see if the button_pressed was incorrect:

INSERT CODE BLOCK

if button_pressed != i:
    failed = True
    break

We do three things in this loop:

1.  First, we use an if statement test the button_pressed to see if it was different than the next button in the sequence. 

2.  If it is (the if statement resolves to True), 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 require us to change our test in the while: loop that we already created -- we'll get to that soon.

3.  Lastly, now that the player has entered an incorrect button, we don't need to keep checking buttons, so we want to abruptly stop the for loop we're in.  We do this using the break command.

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:

# Light and play while button is pressed.
lights[button_pressed].on() 
buttons[button_pressed].wait(press=False)
time.sleep(0.2) 
lights[button_pressed].off()


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:

NSERT CODE BLOCK

# Initialize the LEDs and sequence list
failed = False

...

while not Failed:


Here is what the code for this project should look like at this point:



prev| next