|
| 1 | +import time |
| 2 | +import random |
| 3 | +import board |
| 4 | +import neopixel |
| 5 | +import digitalio |
| 6 | +import adafruit_led_animation.color as color |
| 7 | + |
| 8 | +# button pin setup |
| 9 | +button = digitalio.DigitalInOut(board.D5) |
| 10 | +button.direction = digitalio.Direction.INPUT |
| 11 | +button.pull = digitalio.Pull.UP |
| 12 | + |
| 13 | +# neopixel setup |
| 14 | +pixel_pin = board.D6 |
| 15 | +num_pixels = 61 |
| 16 | + |
| 17 | +pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=False) |
| 18 | + |
| 19 | +# wheel and rainbow_cycle setup |
| 20 | +def wheel(pos): |
| 21 | + if pos < 0 or pos > 255: |
| 22 | + return (0, 0, 0) |
| 23 | + if pos < 85: |
| 24 | + return (255 - pos * 3, pos * 3, 0) |
| 25 | + if pos < 170: |
| 26 | + pos -= 85 |
| 27 | + return (0, 255 - pos * 3, pos * 3) |
| 28 | + pos -= 170 |
| 29 | + return (pos * 3, 0, 255 - pos * 3) |
| 30 | + |
| 31 | +def rainbow_cycle(wait): |
| 32 | + for j in range(255): |
| 33 | + for i in range(num_pixels): |
| 34 | + rc_index = (i * 256 // 10) + j |
| 35 | + pixels[i] = wheel(rc_index & 255) |
| 36 | + pixels.show() |
| 37 | + time.sleep(wait) |
| 38 | + |
| 39 | +# color_chase setup |
| 40 | +def color_chase(c, wait): |
| 41 | + for i in range(num_pixels): |
| 42 | + pixels[i] = c |
| 43 | + time.sleep(wait) |
| 44 | + pixels.show() |
| 45 | + time.sleep(0.5) |
| 46 | + |
| 47 | +# function to blink the neopixels when you lose |
| 48 | +def game_over(): |
| 49 | + color_chase(color.BLACK, 0.05) |
| 50 | + pixels.fill(color.RED) |
| 51 | + pixels.show() |
| 52 | + time.sleep(0.5) |
| 53 | + pixels.fill(color.BLACK) |
| 54 | + pixels.show() |
| 55 | + time.sleep(0.5) |
| 56 | + pixels.fill(color.RED) |
| 57 | + pixels.show() |
| 58 | + time.sleep(0.5) |
| 59 | + pixels.fill(color.BLACK) |
| 60 | + pixels.show() |
| 61 | + time.sleep(0.5) |
| 62 | + pixels.fill(color.RED) |
| 63 | + pixels.show() |
| 64 | + time.sleep(1) |
| 65 | + |
| 66 | +# variables and states |
| 67 | +pixel = 0 |
| 68 | +num = 0 |
| 69 | +last_num = 0 |
| 70 | +now_color = 0 |
| 71 | +next_color = 1 |
| 72 | +speed = 0.1 |
| 73 | +level = 0.005 |
| 74 | +final_level = 0.001 |
| 75 | +new_target = True |
| 76 | +button_state = False |
| 77 | + |
| 78 | +# neopixel colors |
| 79 | +colors = [color.RED, color.ORANGE, color.YELLOW, color.GREEN, color.TEAL, color.CYAN, |
| 80 | + color.BLUE, color.PURPLE, color.MAGENTA, color.GOLD, color.AQUA, color.PINK] |
| 81 | + |
| 82 | +while True: |
| 83 | + |
| 84 | + # button debouncing |
| 85 | + if not button.value and not button_state: |
| 86 | + button_state = True |
| 87 | + |
| 88 | + # if new level starting.. |
| 89 | + if new_target: |
| 90 | + # randomize target location |
| 91 | + y = int(random.randint(5, 55)) |
| 92 | + x = int(y - 1) |
| 93 | + z = int(y + 1) |
| 94 | + new_target = False |
| 95 | + print(x, y, z) |
| 96 | + pixels[x] = color.WHITE |
| 97 | + pixels[y] = colors[next_color] |
| 98 | + pixels[z] = color.WHITE |
| 99 | + # delay without time.sleep() |
| 100 | + if (pixel + speed) < time.monotonic(): |
| 101 | + # turn off pixel behind chaser |
| 102 | + if num > 0: |
| 103 | + last_num = num - 1 |
| 104 | + pixels[last_num] = color.BLACK |
| 105 | + pixels.show() |
| 106 | + # keep target pixels their colors when the chaser passes |
| 107 | + if last_num in (x, y, z): |
| 108 | + pixels[x] = color.WHITE |
| 109 | + pixels[y] = colors[next_color] |
| 110 | + pixels[z] = color.WHITE |
| 111 | + # move chaser pixel by one |
| 112 | + if num < num_pixels: |
| 113 | + pixels[num] = colors[now_color] |
| 114 | + pixels.show() |
| 115 | + #print(num) |
| 116 | + #print("target is", y) |
| 117 | + num += 1 |
| 118 | + # send chaser back to the beginning of the circle |
| 119 | + if num == num_pixels: |
| 120 | + last_num = num - 1 |
| 121 | + pixels[last_num] = color.BLACK |
| 122 | + pixels.show() |
| 123 | + num = 0 |
| 124 | + # if the chaser hits the target... |
| 125 | + if last_num in [x, y, z] and not button.value: |
| 126 | + button_state = False |
| 127 | + # fills with the next color |
| 128 | + pixels.fill(colors[next_color]) |
| 129 | + pixels.show() |
| 130 | + print(num) |
| 131 | + print(x, y, z) |
| 132 | + # chaser resets |
| 133 | + num = 0 |
| 134 | + time.sleep(0.5) |
| 135 | + pixels.fill(color.BLACK) |
| 136 | + pixels.show() |
| 137 | + # speed increases for next level |
| 138 | + speed = speed - level |
| 139 | + # color updates |
| 140 | + next_color = next_color + 1 |
| 141 | + if next_color > 11: |
| 142 | + next_color = 0 |
| 143 | + now_color = now_color + 1 |
| 144 | + if now_color > 11: |
| 145 | + now_color = 0 |
| 146 | + # setup for new target |
| 147 | + new_target = True |
| 148 | + print("speed is", speed) |
| 149 | + print("button is", button.value) |
| 150 | + # if the chaser misses the target... |
| 151 | + if last_num not in [x, y, z] and not button.value: |
| 152 | + button_state = False |
| 153 | + print(num) |
| 154 | + print(x, y, z) |
| 155 | + # fills with current chaser color |
| 156 | + pixels.fill(colors[now_color]) |
| 157 | + pixels.show() |
| 158 | + # function to flash all pixels red |
| 159 | + game_over() |
| 160 | + # chaser is reset |
| 161 | + num = 0 |
| 162 | + pixels.fill(color.BLACK) |
| 163 | + pixels.show() |
| 164 | + # speed is reset to default |
| 165 | + speed = 0.1 |
| 166 | + # colors are reset |
| 167 | + next_color = 1 |
| 168 | + now_color = 0 |
| 169 | + # setup for new target |
| 170 | + new_target = True |
| 171 | + print("speed is", speed) |
| 172 | + print("button is", button.value) |
| 173 | + # when you have beaten all the levels... |
| 174 | + if speed < final_level: |
| 175 | + # rainbows! |
| 176 | + rainbow_cycle(0.01) |
| 177 | + time.sleep(1) |
| 178 | + # chaser is reset |
| 179 | + num = 0 |
| 180 | + pixels.fill(color.BLACK) |
| 181 | + pixels.show() |
| 182 | + # speed is reset to default |
| 183 | + speed = 0.1 |
| 184 | + # colors are reset |
| 185 | + next_color = 1 |
| 186 | + now_color = 0 |
| 187 | + # setup for new target |
| 188 | + new_target = True |
| 189 | + # time.monotonic() is reset for the delay |
| 190 | + pixel = time.monotonic() |
0 commit comments