def send(msg): global morse y = LINE_SPACING * 2 for letter in msg: letter = letter.lower() print(letter) if letter in morse: symbols = morse[letter] explorer.text(letter + ": " + symbols, 0, y, 256, 3) y += LINE_SPACING if y >= HEIGHT: clear() y = 0 explorer.set_pen(255,0,0) explorer.update() for symbol in symbols: print(symbol, sep="") if symbol == ".": explorer.set_tone(TONE_FREQUENCY) utime.sleep(DURATION_DOT / SPEED) explorer.set_tone(-1) elif symbol == "-": explorer.set_tone(TONE_FREQUENCY) utime.sleep(DURATION_DASH / SPEED) explorer.set_tone(-1) utime.sleep(DURATION_SYMBOL / SPEED) utime.sleep(DURATION_CHARACTER / SPEED) else: print("/") utime.sleep(DURATION_WORD / SPEED)
def lightup(block, sound): if block == 1: blockA(True) tone = 600 if block == 2: blockB(True) tone = 700 if block == 3: blockY(True) tone = 800 if block == 4: blockX(True) tone = 900 if sound: exp.set_tone(tone)
def game(): all(False) say("Simon Says") playing = True difficulty = 1 colours = [] while playing: for i in range(0, difficulty): colours.append(random.randint(1, 4)) shhh() time.sleep(1) lightup(colours[i], True) say("Simon Says") time.sleep(1) shhh() all(False) say("Simon Says") say("Your turn") i = 0 while i < difficulty: button = "" while button == "": button = anykey() if button == buttons[colours[i] - 1]: lightup(colours[i], True) say("Your Turn") i += 1 else: exp.set_tone(150) wrong.off() say("Wrong!") playing = False i = difficulty + 1 time.sleep(1) all(False) wrong.on() say("Your turn") shhh() if playing: difficulty = difficulty + 1
explorer.BUTTON_Y): explorer.set_pen(255, 255, 255) explorer.text("Buttons X and Y pressed", 20, 110, 200) elif explorer.is_pressed(explorer.BUTTON_X): explorer.set_pen(255, 255, 255) explorer.text("Button X pressed", 20, 110, 200) elif explorer.is_pressed(explorer.BUTTON_Y): explorer.set_pen(255, 255, 255) explorer.text("Button Y pressed", 20, 110, 200) else: # no button press was detected explorer.set_pen(255, 255, 255) explorer.text("Plug a jumper wire from GP0 to AUDIO to hear noise!", 20, 110, 200) explorer.set_tone(i) if i > 600: explorer.text("Motor 1: Forwards", 20, 180, 200) explorer.set_motor(0, 0, 1) else: explorer.text("Motor 1: Backwards", 20, 180, 200) explorer.set_motor(0, 1, 1) if i > 600: explorer.text("Motor 2: Forwards", 20, 200, 200) explorer.set_motor(1, 0, 1) else: explorer.text("Motor 2: Backwards", 20, 200, 200) explorer.set_motor(1, 1, 1)
def bequiet(): # this function tells your program how not to make noise explorer.set_tone(-1)
def playtone(frequency): # this function tells your program how to make noise explorer.set_tone(frequency)
def shhh(): exp.set_tone(-1)
def __gameloop(self): while self.__l <= self.__sl: #adjust not duration by level self.__d = max(150, SimonGame.DURATION - (self.__l * self.__sl)) #reset sequence position pos = 0 #Simon while not self.__u: #play sequence for s in range(self.__l): b, t = self.__gameboard(self.__s[s]) self.__playtone(t, self.__d) #switch user self.__u = SimonGame.PLAYER #Player while self.__u: #update gameboard b, t = self.__gameboard(user=True) #if a button was pressed if b > -1: #if the button matches the current sequence value if b == self.__s[pos]: #play tone til user releases the button exp.set_tone(t) if t > -1 else None while (b > -1) and exp.is_pressed(b): pass exp.set_tone(-1) self.__gameboard() #increment sequence position pos += 1 #if the spot matches the current level, increment the level and switch users if pos == self.__l: self.__l += 1 self.__u = SimonGame.SIMON sleep_ms(500) #if the button didn't match the current sequence value else: #play fart sound and increment try counter self.__playtone(self.__fart, 1000) self.__t += 1 #if all 3 tries are used up show loser message, play fart sound more and break master while condition if self.__t == 3: exp.text('You Lose!', 16, 30, 240, 5) exp.update() self.__playtone(self.__fart - 100, 1000) self.__l = self.__sl + 2 #switch users ~ doesn't do anything if you lost self.__u = SimonGame.SIMON #if the game has been won if self.__l == (self.__sl + 1): self.__l -= 1 #adjust for display #play winner animation for s in [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]: self.__gameboard(s) exp.set_pen(65535) exp.text('You Win!', 10, 30, 240, 6) exp.update() self.__playtone(t, 100) #go to start screen self.__startscreen()
def __playtone(self, tone: int, ms: int = 500) -> None: exp.set_tone(tone) sleep_ms(ms) exp.set_tone(-1)
def shhh(): explorer.set_tone(20000)