Skip to content

Instantly share code, notes, and snippets.

@benevpi
Last active July 11, 2025 09:33
Show Gist options
  • Save benevpi/86cc662c405b8dfbe9af3122b3529682 to your computer and use it in GitHub Desktop.
Save benevpi/86cc662c405b8dfbe9af3122b3529682 to your computer and use it in GitHub Desktop.
A reaction game in Circuit Python
import board
import busio
import time
import random
import digitalio
NUM_PIXELS = 10
#For Pico
#import neopixel
#PIXEL_PIN = board.GP0
#BUTTON_PIN = board.GP1
#pixels = neopixel.NeoPixel(PIXEL_PIN, NUM_PIXELS, brightness=BRIGHTNESS, auto_write=False)
#For Raspberry PI
import neopixel_spi
PIXEL_PIN = board.D11
CLOCK_PIN = board.D10
BUTTON_PIN = board.D17
pixels = neopixel_spi.NeoPixel_SPI(busio.SPI(board.D11, MOSI=board.D10), 10)
BRIGHTNESS = 0.5
DELAY = 0.3
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WHITE = (100, 100, 100)
FLASH_DURATION = 0.6
# Button setup
button = digitalio.DigitalInOut(BUTTON_PIN)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
def flash_color(color, duration=FLASH_DURATION):
pixels.fill(color)
pixels.show()
time.sleep(duration)
while True:
red_pos = 0
# Move the red pixel forward
while red_pos < NUM_PIXELS:
# Draw strip
for i in range(NUM_PIXELS):
if i == red_pos:
pixels[i] = RED
else:
pixels[i] = GREEN
pixels.show()
# Wait for DELAY, check for button press
t0 = time.monotonic()
pressed = False
while time.monotonic() - t0 < DELAY:
if not button.value:
pressed = True
break
if pressed:
if red_pos == NUM_PIXELS - 1:
# SUCCESS!
flash_color(WHITE)
else:
# FAIL!
flash_color(RED)
break
red_pos += 1
else:
# If it reaches the end and you didn't press, flash red for "miss"
flash_color(RED)
# Small pause before restarting
time.sleep(0.7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment