In this project, we'll finish the development of our Simon Says pattern matching game by adding a final sequence when the player loses.

The hardware setup for this project should be the same setup that was completed for the project. Here's a reminder:

The last piece of our game is to add in a final sequence for when the user makes a mistake and we drop through the `while` loop.

When this happens, we want to do the following:

1. If the user has lost due to a time-out (he didn't hit a button in time), we light up all the LEDs

2. If the user has lost due to hitting a wrong button in the sequence, we light up the LED that was incorrectly pressed

3. We then play an error tone

4. Finally, we play the correct sequence one last time (quickly)

Here is what that code will look like:

# Game Over — Play error sequence if button_pressed == None: for light in lights: light.on() else: lights[button_pressed].on() you_failed_note.play(1.5).wait() for light in lights: light.off() time.sleep(0.5) # Play correct sequence fast for i in play_order: lights[i].on() notes[i].play(0.1).wait() lights[i].off() time.sleep(0.05)

Here is what the final code for this project looks like (with new changes highlighted with arrows) — go have some fun playing and don't forget to try the challenges!

# ################################## # Import Modules and Initialize Game # ################################## from rstem.button import Button from rstem.gpio import Output from rstem.sound import Note from random import randrange import time buttons = [Button(14), Button(15), Button(23), Button(17)] lights = [Output(4), Output(18), Output(24), Output(27)] notes = [Note('C'), Note('D'), Note('E'), Note('F')] *you_failed_note = Note('E2') for light in lights: light.off() play_order = [] failed = False # ################################## # Main while: loop for game # ################################## while not failed: # Add another random LED to sequence play_order.append(randrange(4)) # Play LED sequence for i in play_order: lights[i].on() notes[i].play(0.4).wait() lights[i].off() time.sleep(0.2) # ############################# # for: loop to get player input # ############################# for i in play_order: # Get the user's button press button_pressed = Button.wait_many(buttons, timeout=3) # If user enters incorrect button, end game if button_pressed != i: failed = True break # If user enters correct button, flash LED and play tone lights[button_pressed].on() notes[button_pressed].play(duration=None) buttons[button_pressed].wait(press=False) time.sleep(0.2) lights[button_pressed].off() notes[button_pressed].stop() if not failed: time.sleep(1.0) *# Game Over — Play error sequence *if button_pressed == None: * for light in lights: * light.on() *else: * lights[button_pressed].on() * *you_failed_note.play(1.5).wait() * *for light in lights: * light.off() * *time.sleep(0.5) * *# Play correct sequence fast *for i in play_order: * lights[i].on() * notes[i].play(0.2).wait() * lights[i].off() * time.sleep(0.2)

You now have a basic Simon Says game you can play, but there are many variations you can try. How complex you go with any variation is up to your own imagination:

1. Reverse: Have the user repeat the sequence of button presses in reverse.

2. Inverted Lights: For each button in the play sequence, light up all 3 of the LEDs NOT next to the button. The gameplay is the same, but it is much more confusing to remember which button to press.

3. Mixed Inverted Lights: Sometimes invert the LEDs (as suggested above), and sometimes don't — but do this randomly.

4. Two at a Time: Do a random sequence that displays two LEDs on at the same time.

5. Onesie Twosie: Sometimes display two LEDs at the same time (as suggested above), and sometimes don't — but do this randomly.

6. Fast Match: Display a sequence of 5 (or so) LEDs really fast — perhaps with just a tenth of a second between each LED flash. Then have the user repeat the whole sequence correctly.

7. Make Your Own: The real fun is in making your own. Mashup and of the suggested projects or challenges together, or create a completely new idea!