Skip to content

Commit 3788d28

Browse files
committed
reset branch, initial commit
1 parent 2c74c4a commit 3788d28

1 file changed

Lines changed: 155 additions & 0 deletions

File tree

PyRuler_Simon_Game/code.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
"""
2+
This example runs the 'Simon' game on the PyRuler.
3+
Memorize each led sequence and tap the corresponding
4+
touch pads on the pyruler to advance to each new sequence.
5+
Code adapted from Miguel Grinberg's Simon game for Circuit Playground Express
6+
7+
"""
8+
9+
import time
10+
import random
11+
import board
12+
from digitalio import DigitalInOut, Direction
13+
import touchio
14+
import adafruit_dotstar
15+
16+
# Initialize dot star led
17+
num_pixels = 1
18+
pixels = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI,
19+
num_pixels, brightness=0.1, auto_write=False)
20+
red = (255,0,0)
21+
green = (0,255,0)
22+
blue = (0,0,255)
23+
24+
led = DigitalInOut(board.D13)
25+
led.direction = Direction.OUTPUT
26+
27+
touches = [DigitalInOut(board.CAP0)]
28+
for p in (board.CAP1, board.CAP2, board.CAP3):
29+
touches.append(touchio.TouchIn(p))
30+
31+
leds = []
32+
for p in (board.LED4, board.LED5, board.LED6, board.LED7):
33+
led = DigitalInOut(p)
34+
led.direction = Direction.OUTPUT
35+
leds.append(led)
36+
37+
cap_touches = [False, False, False, False]
38+
39+
def intro_game():
40+
pixels.fill(blue)
41+
pixels.show()
42+
for q in range(len(leds)):
43+
leds[q].value = True
44+
time.sleep(0.25)
45+
for l in range(len(leds)):
46+
leds[l].value = False
47+
48+
def wheel(pos):
49+
# Input a value 0 to 255 to get a color value.
50+
# The colours are a transition r - g - b - back to r.
51+
if pos < 0 or pos > 255:
52+
return (0, 0, 0)
53+
if pos < 85:
54+
return (255 - pos * 3, pos * 3, 0)
55+
if pos < 170:
56+
pos -= 85
57+
return (0, 255 - pos * 3, pos * 3)
58+
pos -= 170
59+
return (pos * 3, 0, 255 - pos * 3)
60+
61+
def rainbow_cycle(wait):
62+
for j in range(255):
63+
for i in range(num_pixels):
64+
rc_index = (i * 256 // num_pixels) + j
65+
pixels[i] = wheel(rc_index & 255)
66+
pixels.show()
67+
time.sleep(wait)
68+
69+
def read_caps():
70+
t0_count = 0
71+
t0 = touches[0]
72+
t0.direction = Direction.OUTPUT
73+
t0.value = True
74+
t0.direction = Direction.INPUT
75+
# funky idea but we can 'diy' the one non-hardware captouch device by hand
76+
# by reading the drooping voltage on a tri-state pin.
77+
t0_count = t0.value + t0.value + t0.value + t0.value + t0.value + \
78+
t0.value + t0.value + t0.value + t0.value + t0.value + \
79+
t0.value + t0.value + t0.value + t0.value + t0.value
80+
cap_touches[0] = t0_count > 2
81+
cap_touches[1] = touches[1].raw_value > 3000
82+
cap_touches[2] = touches[2].raw_value > 3000
83+
cap_touches[3] = touches[3].raw_value > 3000
84+
return cap_touches
85+
86+
def record_caps(timeout=3):
87+
start_time = time.monotonic() # start 3 second timer waiting for user input
88+
while time.monotonic() - start_time < timeout:
89+
caps = read_caps()
90+
val = None
91+
for i,c in enumerate(caps):
92+
if c:
93+
val = i
94+
print("true!")
95+
time.sleep(0.1)
96+
if val is not None: # if there's input from a pad exit the timer loop
97+
break
98+
return val
99+
100+
def light_cap(cap, duration=0.5):
101+
# turn the LED for the selected cap on
102+
leds[cap].value = True
103+
# wait the requested amount of time
104+
time.sleep(duration)
105+
# turn the LED for the selected region off
106+
leds[cap].value = False
107+
time.sleep(duration)
108+
109+
def play_sequence(sequence):
110+
duration = 1 - len(sequence) * 0.05
111+
if duration < 0.1:
112+
duration = 0.1
113+
for cap in sequence:
114+
light_cap(cap, duration)
115+
116+
def read_sequence(sequence):
117+
pixels.fill(green)
118+
pixels.show()
119+
#time.sleep(1)
120+
for cap in sequence:
121+
if record_caps() != cap:
122+
# the player made a mistake!
123+
return False
124+
light_cap(cap, 0.5)
125+
return True
126+
127+
def play_error():
128+
# make dot star red
129+
pixels.fill(red)
130+
pixels.show()
131+
time.sleep(3)
132+
133+
def play_game():
134+
intro_game() # led light sequence at beginning of each game
135+
sequence = []
136+
while True:
137+
pixels.fill(blue) # blue for showing user sequence
138+
pixels.show()
139+
time.sleep(1)
140+
sequence.append(random.randint(0, 3)) # add new light to sequence each time
141+
play_sequence(sequence) # show the sequence
142+
if not read_sequence(sequence): # if user inputs wrong sequence, gameover
143+
# game over
144+
play_error()
145+
print("gameover")
146+
break
147+
else:
148+
print("Next sequence unlocked!")
149+
rainbow_cycle(0) # Dot star animation after each correct sequence
150+
pixels.fill(0)
151+
pixels.show()
152+
time.sleep(1)
153+
154+
while True:
155+
play_game()

0 commit comments

Comments
 (0)