Light Reaction Game

A light reaction game where you click a button when the corresponding light goes on and the player will press the button that lights up to complete the round. the game gets progressively harder as the player progresses through the game until the player doesn't press the button in time or they press the wrong button. once finished the buttons all flash the amount of rounds completed and a pattern to signify the high score.
Supplies
Materials:
Raspberry Pi Pico
Wires
4 x LED
4 x resistor 220Ω
4 x Arcade Button
Push Button
Equipment:
soldering iron
solder
Code
from machine import Pin
import utime
import urandom
# Pin definitions
button_white = Pin(18, Pin.IN, Pin.PULL_UP)
button_pink = Pin(26, Pin.IN, Pin.PULL_UP)
button_green = Pin(2, Pin.IN, Pin.PULL_UP)
button_yellow = Pin(14, Pin.IN, Pin.PULL_UP)
push_button = Pin(11, Pin.IN, Pin.PULL_UP)
led_green = Pin(4, Pin.OUT)
led_yellow = Pin(8, Pin.OUT)
led_white = Pin(20, Pin.OUT)
led_red = Pin(27, Pin.OUT)
# LED and button lists
leds = [led_green, led_yellow, led_white, led_red]
buttons = [button_green, button_yellow, button_white, button_pink]
# Game state
game_active = False
last_button_state = 1
reaction_delay = 0.5 # Initial delay between rounds (seconds)
min_delay = 0.15
delay_decrement = 0.02
score = 0
max_wait_time = 2000 # ms before timeout (loss)
def turn_off_all_leds():
for led in leds:
led.value(0)
def flash_leds(times):
for _ in range(times):
for led in leds:
led.value(1)
utime.sleep(0.3)
for led in leds:
led.value(0)
utime.sleep(0.3)
def wait_for_release(pin):
while pin.value() == 0:
utime.sleep_ms(10)
def play_reaction_game():
global game_active, reaction_delay, score
index = urandom.getrandbits(2) # Random index 0–3
selected_led = leds[index]
selected_button = buttons[index]
selected_led.value(1) # Light up LED
start_time = utime.ticks_ms()
timed_out = False
# Wait for correct button or timeout
while selected_button.value() == 1:
if push_button.value() == 0:
game_active = False
turn_off_all_leds()
return
if utime.ticks_diff(utime.ticks_ms(), start_time) > max_wait_time:
timed_out = True
break
selected_led.value(0)
if timed_out:
game_active = False
return
score += 1
print(f"Round {score} complete")
if reaction_delay > min_delay:
reaction_delay -= delay_decrement
utime.sleep(reaction_delay)
# Main loop
while True:
current_button_state = push_button.value()
# Toggle game state
if last_button_state == 1 and current_button_state == 0:
utime.sleep_ms(200)
game_active = not game_active
wait_for_release(push_button)
if game_active:
print("Game started!")
score = 0
reaction_delay = 0.5
else:
print("Game stopped!")
turn_off_all_leds()
last_button_state = current_button_state
if game_active:
play_reaction_game()
if not game_active:
print(f"Game over! You completed {score} rounds.")
flash_leds(score)
turn_off_all_leds()
else:
turn_off_all_leds()
Wiring Diagram
Downloads
Ground Wires

connect the ground pins to each other
Anode Wires

connect anode wires with there own wires