|
| 1 | +import time |
| 2 | +import random |
| 3 | +from adafruit_circuitplayground.express import cpx |
| 4 | + |
| 5 | +cpx.pixels.brightness = 0.1 # adjust NeoPixel brightness to your liking |
| 6 | + |
| 7 | +REGION_LEDS = ( |
| 8 | + (5, 6, 7), # yellow region |
| 9 | + (2, 3, 4), # blue region |
| 10 | + (7, 8, 9), # red region |
| 11 | + (0, 1, 2), # green region |
| 12 | +) |
| 13 | + |
| 14 | +REGION_COLOR = ( |
| 15 | + (255, 255, 0), # yellow region |
| 16 | + (0, 0, 255), # blue region |
| 17 | + (255, 0, 0), # red region |
| 18 | + (0, 255, 0), # green region |
| 19 | +) |
| 20 | + |
| 21 | +REGION_TONE = ( |
| 22 | + 252, # yellow region |
| 23 | + 209, # blue region |
| 24 | + 310, # red region |
| 25 | + 415, # green region |
| 26 | +) |
| 27 | + |
| 28 | +PAD_REGION = { |
| 29 | + 'A1': 0, # yellow region |
| 30 | + 'A2': 2, # red region |
| 31 | + 'A3': 2, # red region |
| 32 | + 'A4': 3, # green region |
| 33 | + 'A5': 3, # green region |
| 34 | + 'A6': 1, # blue region |
| 35 | + 'A7': 1, # blue region |
| 36 | +} |
| 37 | + |
| 38 | +def light_region(region, duration=1): |
| 39 | + # turn the LEDs for the selected region on |
| 40 | + for led in REGION_LEDS[region]: |
| 41 | + cpx.pixels[led] = REGION_COLOR[region] |
| 42 | + |
| 43 | + # play a tone for the selected region |
| 44 | + cpx.start_tone(REGION_TONE[region]) |
| 45 | + |
| 46 | + # wait the requested amount of time |
| 47 | + time.sleep(duration) |
| 48 | + |
| 49 | + # stop the tone |
| 50 | + cpx.stop_tone() |
| 51 | + |
| 52 | + # turn the LEDs for the selected region off |
| 53 | + for led in REGION_LEDS[region]: |
| 54 | + cpx.pixels[led] = (0, 0, 0) |
| 55 | + |
| 56 | +def read_region(timeout=30): |
| 57 | + val = 0 |
| 58 | + start_time = time.time() |
| 59 | + while time.time() - start_time < timeout: |
| 60 | + if cpx.touch_A1: |
| 61 | + val = PAD_REGION['A1'] |
| 62 | + elif cpx.touch_A2: |
| 63 | + val = PAD_REGION['A2'] |
| 64 | + elif cpx.touch_A3: |
| 65 | + val = PAD_REGION['A3'] |
| 66 | + elif cpx.touch_A4: |
| 67 | + val = PAD_REGION['A4'] |
| 68 | + elif cpx.touch_A5: |
| 69 | + val = PAD_REGION['A5'] |
| 70 | + elif cpx.touch_A6: |
| 71 | + val = PAD_REGION['A6'] |
| 72 | + elif cpx.touch_A7: |
| 73 | + val = PAD_REGION['A7'] |
| 74 | + return val |
| 75 | + |
| 76 | +def play_sequence(sequence): |
| 77 | + duration = 1 - len(sequence) * 0.05 |
| 78 | + if duration < 0.1: |
| 79 | + duration = 0.1 |
| 80 | + for region in sequence: |
| 81 | + light_region(region, duration) |
| 82 | + |
| 83 | +def read_sequence(sequence): |
| 84 | + for region in sequence: |
| 85 | + if read_region() != region: |
| 86 | + # the player made a mistake! |
| 87 | + return False |
| 88 | + light_region(region, 0.25) |
| 89 | + return True |
| 90 | + |
| 91 | +def play_error(): |
| 92 | + cpx.start_tone(160) |
| 93 | + time.sleep(1) |
| 94 | + cpx.stop_tone() |
| 95 | + |
| 96 | +def play_game(): |
| 97 | + sequence = [] |
| 98 | + while True: |
| 99 | + sequence.append(random.randint(0, 3)) |
| 100 | + play_sequence(sequence) |
| 101 | + if not read_sequence(sequence): |
| 102 | + # game over |
| 103 | + play_error() |
| 104 | + break |
| 105 | + time.sleep(1) |
| 106 | + |
| 107 | +while True: |
| 108 | + play_game() |
0 commit comments