|
| 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 | +pixels = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, |
| 18 | + 1, brightness=0.1) |
| 19 | +red = (255,0,0) |
| 20 | +green = (0,255,0) |
| 21 | +blue = (0,0,255) |
| 22 | + |
| 23 | +led = DigitalInOut(board.D13) |
| 24 | +led.direction = Direction.OUTPUT |
| 25 | + |
| 26 | +touches = [DigitalInOut(board.CAP0)] |
| 27 | +for p in (board.CAP1, board.CAP2, board.CAP3): |
| 28 | + touches.append(touchio.TouchIn(p)) |
| 29 | + |
| 30 | +leds = [] |
| 31 | +for p in (board.LED4, board.LED5, board.LED6, board.LED7): |
| 32 | + led = DigitalInOut(p) |
| 33 | + led.direction = Direction.OUTPUT |
| 34 | + leds.append(led) |
| 35 | + |
| 36 | +cap_touches = [False, False, False, False] |
| 37 | + |
| 38 | +def wheel(pos): |
| 39 | + # Input a value 0 to 255 to get a color value. |
| 40 | + # The colours are a transition r - g - b - back to r. |
| 41 | + if pos < 0 or pos > 255: |
| 42 | + return (0, 0, 0) |
| 43 | + if pos < 85: |
| 44 | + return (255 - pos * 3, pos * 3, 0) |
| 45 | + if pos < 170: |
| 46 | + pos -= 85 |
| 47 | + return (0, 255 - pos * 3, pos * 3) |
| 48 | + pos -= 170 |
| 49 | + return (pos * 3, 0, 255 - pos * 3) |
| 50 | + |
| 51 | +def rainbow_cycle(wait): |
| 52 | + for j in range(255): |
| 53 | + for i in range(len(pixels)): |
| 54 | + rc_index = (i * 256 // len(pixels)) + j |
| 55 | + pixels[i] = wheel(rc_index & 255) |
| 56 | + time.sleep(wait) |
| 57 | + |
| 58 | +def read_caps(): |
| 59 | + t0_count = 0 |
| 60 | + t0 = touches[0] |
| 61 | + t0.direction = Direction.OUTPUT |
| 62 | + t0.value = True |
| 63 | + t0.direction = Direction.INPUT |
| 64 | + # funky idea but we can 'diy' the one non-hardware captouch device by hand |
| 65 | + # by reading the drooping voltage on a tri-state pin. |
| 66 | + t0_count = t0.value + t0.value + t0.value + t0.value + t0.value + \ |
| 67 | + t0.value + t0.value + t0.value + t0.value + t0.value + \ |
| 68 | + t0.value + t0.value + t0.value + t0.value + t0.value |
| 69 | + cap_touches[0] = t0_count > 2 |
| 70 | + cap_touches[1] = touches[1].raw_value > 3000 |
| 71 | + cap_touches[2] = touches[2].raw_value > 3000 |
| 72 | + cap_touches[3] = touches[3].raw_value > 3000 |
| 73 | + return cap_touches |
| 74 | + |
| 75 | +def timeout_touch(timeout=3): |
| 76 | + start_time = time.monotonic() # start 3 second timer waiting for user input |
| 77 | + while time.monotonic() - start_time < timeout: |
| 78 | + caps = read_caps() |
| 79 | + for i,c in enumerate(caps): |
| 80 | + if c: |
| 81 | + return i |
| 82 | + |
| 83 | +def light_cap(cap, duration=0.5): |
| 84 | + # turn the LED for the selected cap on |
| 85 | + leds[cap].value = True |
| 86 | + time.sleep(duration) |
| 87 | + leds[cap].value = False |
| 88 | + time.sleep(duration) |
| 89 | + |
| 90 | +def play_sequence(seq): |
| 91 | + duration = max(0.1, 1 - len(sequence) * 0.05) |
| 92 | + for cap in seq: |
| 93 | + light_cap(cap, duration) |
| 94 | + |
| 95 | +def read_sequence(seq): |
| 96 | + pixels.fill(green) |
| 97 | + for cap in seq: |
| 98 | + if timeout_touch() != cap: |
| 99 | + # the player made a mistake! |
| 100 | + return False |
| 101 | + light_cap(cap, 0.5) |
| 102 | + return True |
| 103 | + |
| 104 | +while True: |
| 105 | + # led light sequence at beginning of each game |
| 106 | + pixels.fill(blue) |
| 107 | + time.sleep(1) |
| 108 | + for led in leds: |
| 109 | + led.value = True |
| 110 | + time.sleep(0.25) |
| 111 | + for led in leds: |
| 112 | + led.value = False |
| 113 | + sequence = [] |
| 114 | + while True: |
| 115 | + pixels.fill(blue) # blue for showing user sequence |
| 116 | + time.sleep(1) |
| 117 | + sequence.append(random.randint(0, 3)) # add new light to sequence each time |
| 118 | + play_sequence(sequence) # show the sequence |
| 119 | + if not read_sequence(sequence): # if user inputs wrong sequence, gameover |
| 120 | + # game over, make dot star red |
| 121 | + pixels.fill(red) |
| 122 | + time.sleep(3) |
| 123 | + print("gameover") |
| 124 | + break |
| 125 | + else: |
| 126 | + print("Next sequence unlocked!") |
| 127 | + rainbow_cycle(0) # Dot star animation after each correct sequence |
| 128 | + pixels.fill(0) |
| 129 | + time.sleep(1) |
0 commit comments