from machine import Pin, PWM
import time
import random
RESET_BUTTON = 1
GREEN_BUTTON = 5
RED_BUTTON = 9
BLUE_BUTTON = 13
YELLOW_BUTTON = 15
SPEAKER = 16
YELLOW_LIGHT = 17
BLUE_LIGHT = 18
RED_LIGHT = 26
GREEN_LIGHT = 28
# Set up our buttons and LEDs
game_controls = {
'red': {
'button': Pin(RED_BUTTON, Pin.IN, Pin.PULL_UP),
'led': Pin(RED_LIGHT, Pin.OUT),
'tone': 440 # Musical note frequency
},
'blue': {
'button': Pin(BLUE_BUTTON, Pin.IN, Pin.PULL_UP),
'led': Pin(BLUE_LIGHT, Pin.OUT),
'tone': 330
},
'green': {
'button': Pin(GREEN_BUTTON, Pin.IN, Pin.PULL_UP),
'led': Pin(GREEN_LIGHT, Pin.OUT),
'tone': 277
},
'yellow': {
'button': Pin(YELLOW_BUTTON, Pin.IN, Pin.PULL_UP),
'led': Pin(YELLOW_LIGHT, Pin.OUT),
'tone': 165
}
}
# Set up speaker
speaker = PWM(Pin(SPEAKER)) # Change pin number as needed
class SimonGame:
def __init__(self):
self.sequence = []
self.game_speed = 500 # Time in milliseconds for each light/sound
def play_tone(self, frequency, duration):
"""Play a tone on the speaker"""
speaker.freq(frequency)
speaker.duty_u16(30000)
time.sleep_ms(duration)
speaker.duty_u16(0)
time.sleep_ms(50) # Small gap between sounds
def flash_color(self, color):
"""Light up one color and play its tone"""
controls = game_controls[color]
controls['led'].value(1) # Turn on LED
self.play_tone(controls['tone'], self.game_speed)
time.sleep_ms(100)
controls['led'].value(0) # Turn off LED
time.sleep_ms(100) # Gap between colors
def show_sequence(self):
"""Show the complete sequence to the player"""
time.sleep_ms(500) # Pause before starting
for color in self.sequence:
self.flash_color(color)
def get_button_press(self):
"""Wait for and return one button press"""
while True:
for color, controls in game_controls.items():
if controls['button'].value() == 0: # Button pressed
controls['led'].value(1) # Turn on LED
self.play_tone(controls['tone'], 200)
controls['led'].value(0) # Turn off LED
time.sleep_ms(100) # Debounce
return color
time.sleep_ms(10) # Don't overwhelm CPU
def play_victory_sound(self):
"""Play a happy sound"""
for freq in [330, 392, 494, 660]:
self.play_tone(freq, 150)
def play_game_over_sound(self):
"""Play a sad sound"""
for freq in [294, 277, 262]:
self.play_tone(freq, 300)
def play_game(self):
"""Main game loop"""
print("Simon Game Started!")
self.sequence = []
while True:
# Add new color to sequence
self.sequence.append(random.choice(list(game_controls.keys())))
# Show sequence to player
self.show_sequence()
# Get player's response
print(f"Repeat the sequence! ({len(self.sequence)} colors)")
# Check each button press against sequence
for expected_color in self.sequence:
pressed_color = self.get_button_press()
if pressed_color != expected_color:
print(f"Game Over! Score: {len(self.sequence)-1}")
self.play_game_over_sound()
return len(self.sequence) - 1 # Return final score
# Player got sequence right!
print("Correct! Next sequence...")
self.play_victory_sound()
# Speed up game every 3 correct sequences
# if len(self.sequence) % 3 == 0:
# self.game_speed = max(200, self.game_speed - 50)
time.sleep(1) # Pause before next round
# Start the game
if __name__ == "__main__":
game = SimonGame()
while True:
score = game.play_game()
print(f"Final Score: {score}")
print("Press any button to play again!")
game.get_button_press() # Wait for button press to restart