add changes

This commit is contained in:
Isaac Wellish 2019-08-12 17:47:34 -04:00
parent be82d91832
commit 375cc8019f

View file

@ -14,9 +14,8 @@ import touchio
import adafruit_dotstar
# Initialize dot star led
num_pixels = 1
pixels = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI,
num_pixels, brightness=0.1, auto_write=False)
1, brightness=0.1)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
@ -38,12 +37,12 @@ cap_touches = [False, False, False, False]
def intro_game():
pixels.fill(blue)
pixels.show()
for q in range(len(leds)):
leds[q].value = True
time.sleep(1)
for led in leds:
led.value = True
time.sleep(0.25)
for l in range(len(leds)):
leds[l].value = False
for led in leds:
led.value = False
def wheel(pos):
# Input a value 0 to 255 to get a color value.
@ -60,10 +59,9 @@ def wheel(pos):
def rainbow_cycle(wait):
for j in range(255):
for i in range(num_pixels):
rc_index = (i * 256 // num_pixels) + j
for i in range(len(pixels)):
rc_index = (i * 256 // 1) + j
pixels[i] = wheel(rc_index & 255)
pixels.show()
time.sleep(wait)
def read_caps():
@ -83,17 +81,13 @@ def read_caps():
cap_touches[3] = touches[3].raw_value > 3000
return cap_touches
def record_caps(timeout=3):
def timeout_touch(timeout=3):
start_time = time.monotonic() # start 3 second timer waiting for user input
val = None
while time.monotonic() - start_time < timeout:
caps = read_caps()
for i,c in enumerate(caps):
if c:
val = i
time.sleep(0.1)
if val is not None: # if there's input from a pad exit the timer loop
return val
return i
def light_cap(cap, duration=0.5):
# turn the LED for the selected cap on
@ -102,47 +96,36 @@ def light_cap(cap, duration=0.5):
leds[cap].value = False
time.sleep(duration)
def play_sequence(sequence):
def play_sequence(seq):
duration = max(0.1, 1 - len(sequence) * 0.05)
for cap in sequence:
for cap in seq:
light_cap(cap, duration)
def read_sequence(sequence):
def read_sequence(seq):
pixels.fill(green)
pixels.show()
for cap in sequence:
if record_caps() != cap:
for cap in seq:
if timeout_touch() != cap:
# the player made a mistake!
return False
light_cap(cap, 0.5)
return True
def play_error():
# make dot star red
pixels.fill(red)
pixels.show()
time.sleep(3)
def play_game():
while True:
intro_game() # led light sequence at beginning of each game
sequence = []
while True:
pixels.fill(blue) # blue for showing user sequence
pixels.show()
time.sleep(1)
sequence.append(random.randint(0, 3)) # add new light to sequence each time
play_sequence(sequence) # show the sequence
if not read_sequence(sequence): # if user inputs wrong sequence, gameover
# game over
play_error()
# game over, make dot star red
pixels.fill(red)
time.sleep(3)
print("gameover")
break
else:
print("Next sequence unlocked!")
rainbow_cycle(0) # Dot star animation after each correct sequence
pixels.fill(0)
pixels.show()
time.sleep(1)
while True:
play_game()