Toggle Navigation
Hatchery
Eggs
Memory Singleplayer
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import display, touchpads, random, virtualtimers as vt, keypad, sndmixer def shuffle(x): for i in reversed(range(1, len(x))): j = int(random.random() * (i+1)) x[i], x[j] = x[j], x[i] colors = [0xff0000, 0xfc6602, 0xfcf153, 0x08ff00, 0x00fff2, 0x001dff, 0xad02cc, 0xfca4f0] board = [int(i / 2) for i in range(0, 16)] caught = [False] * 16 selected = None scale = 0 first = True resetting = False reset_index = 0 seed = 0 def init(): global board, resetting, reset_index, selected, caught, seed, scale random.seed(seed) shuffle(board) resetting = False reset_index = 0 selected = None scale = random.randint(0, 12) caught = [False] * 16 def on_touch(pressed): global mode if pressed == touchpads.LEFT: if mode > 0: mode = mode - 1 elif pressed == touchpads.RIGHT: if mode < 4: mode = mode + 1 print("Key:", pressed, "mode =", mode) def render_board(): global board for i in range(0, 16): display.drawPixel(i % 4, int(i / 4), colors[board[i]] if caught[i] or selected == i else 0) display.flush() def play_index(value): global scale freq = int(440 * (2**((value + scale)/12))) sndmixer.freq(synth, freq) sndmixer.play(synth) def on_key(key_index, pressed): global first, caught, board, selected, colors, scale, resetting if resetting: return if first: first = False init() print("Board shuffled: ", board, "scale:", scale) if not caught[key_index]: if pressed: value = board[key_index] display.drawFill(colors[value]) display.flush() play_index(value) else: sndmixer.pause(synth) if selected is None: selected = key_index else: if board[key_index] == board[selected]: caught[key_index] = True caught[selected] = True selected = None render_board() print(caught) if all(caught): print("All caught, now resetting!") resetting = True selected = None def tick(): global reset_index, resetting, board, caught, first, seed seed += 1 if resetting: print(reset_index) if reset_index >= 8: if reset_index >= 10: resetting = False reset_index = 0 first = True sndmixer.pause(synth) else: reset_index += 1 else: play_index(reset_index) for i in range(0, 16): if board[i] == reset_index: caught[i] = False render_board() reset_index += 1 return 100 sndmixer.begin(1) synth = sndmixer.synth() keypad.add_handler(on_key) render_board() vt.begin() vt.new(100, tick)