Skip to content
Snippets Groups Projects
Commit aaaece42 authored by mzxs1g21's avatar mzxs1g21
Browse files

Colour sequence game

parent 7c21df08
No related branches found
No related tags found
No related merge requests found
import machine
import neopixel
import random
import time
# Assign buttons and LED
button_pins = [20, 21, 22] # Pins for buttons 1, 2, and 3 respectively
# Set up buttons as inputs with pull-down resistors
for pin in button_pins:
machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_DOWN)
np = neopixel.NeoPixel(machine.Pin(28), 1)
np[0] = (0,0,0) # Initially set the led to nothing
np.write()
# Blinks the LED green once
def blink_green():
np[0] = (0,0,0)
np.write()
time.sleep_ms(500)
np[0] = (0,255,0)
np.write()
time.sleep_ms(500)
# Blinks the LED red once
def blink_red():
np[0] = (0,0,0)
np.write()
time.sleep_ms(500)
np[0] = (255,0,0)
np.write()
time.sleep_ms(500)
# Blinks the LED blue once
def blink_blue():
np[0] = (0,0,0)
np.write()
time.sleep_ms(500)
np[0] = (0,0,255)
np.write()
time.sleep_ms(500)
button_press_duration = 0
# Generate random a sequence of random length of nummbers from 0,1,2
rep = random.randint(3,5)
sequence = [random.randint(0, 2) for i in range(rep)]
for button in sequence:
if button == 0:
blink_red()
elif button == 1:
blink_green()
elif button == 2:
blink_blue()
np[0] = (0,0,0)
np.write()
button_presses = []
while len(button_presses) < rep:
# Check each button to see if it's been pressed
for i, pin in enumerate(button_pins):
if machine.Pin(pin).value() == 0:
button_presses.append(i)
time.sleep(0.3) # Wait a short time to avoid registering multiple presses
if sequence == button_presses:
print("Correct!")
blink_green()
blink_green()
blink_green()
np[0] = (0,0,0)
np.write()
else:
print("Incorrect!")
blink_red()
blink_red()
blink_red()
np[0] = (0,0,0)
np.write()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment