Toggle Navigation
Hatchery
Eggs
simonnn_cz20
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import display, keypad, sndmixer, virtualtimers as vt, random def draw(state): sound(state) display.drawPixel(0, 0, 0xFF0000 if state == 1 else 0) display.drawPixel(1, 0, 0x00FF00 if state == 2 else 0) display.drawPixel(2, 0, 0x0000FF if state == 3 else 0) display.drawPixel(3, 0, 0xFFFF00 if state == 4 else 0) display.drawPixel(0, 1, 0xFF00FF if state == 5 else 0) display.drawPixel(1, 1, 0x00FFFF if state == 6 else 0) display.drawPixel(2, 1, 0xFFFFFF if state == 7 else 0) display.drawPixel(3, 1, 0xFF6000 if state == 8 else 0) display.drawPixel(0, 2, 0x10FF10 if state == 9 else 0) display.drawPixel(1, 2, 0x00FCFF if state == 10 else 0) display.drawPixel(2, 2, 0xDE00FF if state == 11 else 0) display.drawPixel(3, 2, 0x00FFA2 if state == 12 else 0) display.drawPixel(0, 3, 0x0042FE if state == 13 else 0) display.drawPixel(1, 3, 0x00FF42 if state == 14 else 0) display.drawPixel(2, 3, 0xDEFF00 if state == 15 else 0) display.drawPixel(3, 3, 0xF00FF0 if state == 16 else 0) if state == 17: for i in range(4): for j in range(4): display.drawPixel(i, j, 0xFF0000) if state == 18: for i in range(4): for j in range(4): display.drawPixel(i, j, 0x00FF00) display.flush() def sound(state): global synth f = 0 if state == 1: f = 600 if state == 2: f = 800 if state == 3: f = 1000 if state == 4: f = 1200 if state == 5: f = 600 if state == 6: f = 800 if state == 7: f = 1000 if state == 8: f = 1200 if state == 9: f = 600 if state == 10: f = 800 if state == 11: f = 1000 if state == 12: f = 1200 if state == 13: f = 600 if state == 14: f = 800 if state == 15: f = 1000 if state == 16: f = 1200 if state == 17: f = 400 if state == 18: f = 1400 sndmixer.freq(synth, f) def on_key(key_index, pressed): global userInput, userTimeout, resultTimeout, showTimeout, showInput, resultPositive, userPressed if pressed: if resultTimeout <= 0: userTimeout = 30 draw(key_index+1) userInput.append(key_index+1) userPressed = True gotFail = False for i in range(len(userInput)): if showInput[i] != userInput[i]: userInput = [] showInput = [random.randint(1,16)] userTimeout = -1 showTimeout = 5 resultTimeout = 5 resultPositive = False gotFail = True break if (not gotFail) and (len(userInput) >= len(showInput)): userInput = [] userTimeout = -1 showTimeout = 5 showInput.append(random.randint(1,16)) resultTimeout = 5 resultPositive = True else: if resultTimeout <= 0: draw(0) userPressed = False def gameTask(): global userTimeout, userInput, showTimeout, showStep, showInput, resultTimeout, userPressed, resultPositive if userPressed: return 50 if resultTimeout > 0: resultTimeout -= 1 print("Result wait") if resultTimeout % 2: if resultPositive: draw(18) else: draw(17) else: draw(0) else: if resultTimeout == 0: resultTimeout -= 1 draw(0) if userTimeout < 0: userTimeout = -1 userInput = [] if showTimeout < 0: showTimeout = 5 if (showStep < len(showInput)): draw(showInput[showStep]) showStep += 1 print("Show step") else: showStep = 0 showTimeout = 30 print("Show end") else: print("Show wait", showTimeout) draw(0) showTimeout -= 1 else: userTimeout -= 1 showTimeout = 0 showStep = 0 print("User wait") return 50 sndmixer.begin(2, True) synth = sndmixer.synth() try: f = open("/sd/bgm.mp3", "rb") m = sndmixer.mp3_stream(f) sndmixer.play(m) except: pass sndmixer.play(synth) userInput = [] showInput = [random.randint(1,16)] userTimeout = 0 showTimeout = 50 showStep = 0 showActive = False userPressed = False resultPositive = False resultTimeout = 0 vt.begin() vt.new(50, gameTask) draw(0) keypad.add_handler(on_key)