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 __startscreen(self) -> None: self.__l = 1 #level self.__d = SimonGame.DURATION #initial note duration self.__u = SimonGame.SIMON #current user self.__t = 0 #tries self.__c = [12, 6, 10, 10, 12, 10, 12, 12, 12, 12] #character widths #draw start screen exp.set_pen(self.__pal(5)) exp.clear() exp.set_pen(self.__pal(2)) exp.text('DO SAME', 10, 20, 235, 6) exp.set_pen(0) exp.text('by: OneMadGypsy 2021', 10, 60, 240, 1) exp.text('A = Easy 10', 40, 100, 240, 2) exp.text('B = Medium 15', 40, 120, 240, 2) exp.text('X = Hard 25', 40, 140, 240, 2) exp.text('Y = Nightmare 50', 40, 160, 240, 2) exp.update() dfclt = [10, 15, 5, 50] #difficulties self.__sl = 0 #sequence length #wait for difficulty selection while not self.__sl: for n in range(4): if exp.is_pressed(n): self.__sl = dfclt[n] break self.__s = [randint(0, 3) for _ in range(self.__sl)] #sequence self.__gameboard() #draw gameboard sleep(1) #pause 1 second for player to prepare self.__gameloop() #start game
def say(string): x = half_width // 2 y = height // 3 h = half_width w = height // 3 exp.set_pen(black) exp.rectangle(x, y, h, w) exp.set_pen(white) exp.text(string, x + 10, y + 10, 15) exp.update()
def say(string): x = int(half_width / 2) y = int(height / 3) h = half_width w = int(height / 3) explorer.set_pen(black) explorer.rectangle(x, y, h, w) explorer.set_pen(white) explorer.text(string, x + 10, y + 10, 15) explorer.update()
def playsong(song): # this function plays your song a = 0 # this variable keeps track of the visualiser bars for i in range(len(song)): if (song[i] == "P"): bequiet() else: playtone(tones[song[i]]) explorer.set_pen(0, 255, 0) # switch to green pen explorer.rectangle( a, 240 - (int((tones[song[i]]) / 21)), 5, 240 ) # draw a green bar corresponding to the frequency of the note a += 7 if a >= 240: # clears the screen if the green bars reach the right hand edge clear() a = 0 explorer.update() utime.sleep( 0.15 ) # change this number if you want to alter how long the notes play for bequiet()
def __gameboard(self, seq: int = -1, user: bool = False) -> int: bt, tn = -1, -1 exp.set_pen(0) exp.clear() #draw squares ~ capture button presses (if avail) OR Simon ~ store button tone for n, d in enumerate([2093, 2249, 2637, 2794]): p = ((exp.is_pressed(n) and user) or (n == seq)) bt = n if p else bt tn = d if p else tn x, y = (n > 1) * 120, (n % 2) * 120 exp.set_pen(self.__pal(n + 8)) exp.rectangle(x + 2, y + 2, 116, 116) exp.set_pen(self.__pal(n + (4 * p))) exp.rectangle(x + 7, y + 7, 106, 106) #center circle for level display exp.set_pen(0) exp.circle(120, 120, 40) exp.set_pen(self.__pal(12)) exp.circle(120, 120, 36) exp.set_pen(self.__pal(13)) exp.circle(120, 120, 31) exp.set_pen(65535) #find x for center placement of level display if self.__l < 10: c = self.__c[self.__l] else: a = self.__l // 10 b = self.__l - int(a * 10) c = self.__c[a] + self.__c[b] #display level number exp.text("{}".format(self.__l), 121 - c, 108, 160, 4) exp.update() return bt, tn
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) i = i + 20 if i > 1000: i = 1 explorer.update() time.sleep(0.01)
def clear(): # this function clears Pico Explorer's screen to black display.set_pen(0, 0, 0) display.clear() display.update()
def update(): display.update()
def birdgame(width, height): #colours brightwhite = display.create_pen(255, 255, 255) white = display.create_pen(155, 155, 155) red = display.create_pen(155, 0, 0) green = display.create_pen(0, 155, 0) brown = display.create_pen(210, 105, 30) skyblue = display.create_pen(50, 50, 115) black = display.create_pen(0, 0, 0) birdgrid = [ "00000011111100000", "00001144441510000", "00014444415551000", "00014444415515100", "01111444415515100", "15555144415555100", "15555514441555100", "14555414444111110", "01444144441333331", "00111222213111110", "00001222221333100", "00000112222111100", "00000001111000000" ] def drawbird(x, y, zoom): #draw the bird row = 0 col = 0 for line in birdgrid: #print(line) for pixel in line: #print(pixel) colour = str(pixel) if colour != "0": if colour == "1": display.set_pen(black) if colour == "2": display.set_pen(green) if pixel == "3": display.set_pen(brown) if pixel == "4": display.set_pen(red) if pixel == "5": display.set_pen(white) display.rectangle(x + (col * zoom), y + (row * zoom), zoom, zoom) col += 1 row += 1 col = 0 class Bird: def __init__(self, x, y): self.x = x self.y = y self.flying = True self.crashed = False class Pillar: def __init__(self, x, hole): self.x = x self.holetop = hole self.holebottom = hole + 70 bird = Bird(5, 20) colonade = [] score = 0 # reset def reset(): x = 0 for i in range(0, 3): hole = random.randint(1, height - 100) colonade.append(Pillar(x + 100, hole)) x += int(width / 3) reset() while bird.flying: display.set_pen(skyblue) display.clear() #draw the pillars for pillar in colonade: display.set_pen(white) display.rectangle(pillar.x, 0, 10, height) display.set_pen(skyblue) display.rectangle(pillar.x, pillar.holetop, 10, 70) if not bird.crashed: if pillar.x > -10: pillar.x -= 1 else: pillar.x = width hole = random.randint(1, height - 100) pillar.holetop = hole pillar.holebottom = hole + 50 score += 1 if pillar.x < 39 and pillar.x > 6: if bird.y < pillar.holetop or bird.y + 28 > pillar.holebottom: bird.crashed = True #draw the bird drawbird(bird.x, bird.y, 2) #draw score display.set_pen(white) display.text(str(score), width - 70, 5, 1, 5) #move the bird if not bird.crashed: if display.is_pressed(display.BUTTON_A): #flap if bird.y > -22: bird.y -= 3 else: if bird.y < height - 25: bird.y += 3 if display.is_pressed(display.BUTTON_B): colonade = [] reset() bird.y = 20 bird.crashed = False display.update()
def clear(): # this function clears Pico Explorer's screen to black explorer.set_pen(0, 0, 0) explorer.clear() explorer.update()
def clear(): display.set_pen(0, 0, 0) display.clear() display.update()
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()
i = i % 6 if i == 0: return v, t, p if i == 1: return q, v, p if i == 2: return p, v, t if i == 3: return p, q, v if i == 4: return t, p, v if i == 5: return v, p, q h = 0 while True: h += 1 r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic display.set_pen(r, g, b) # Set pen to a converted HSV value display.clear() # Fill the screen with the colour display.set_pen(0, 0, 0) # Set pen to black display.text("pico disco!", 25, 20, 240, 6) # Add some text display.text("\o/ \o/ \o/ \o/ \o/ \o/ \o/ \o/ \o/", 25, 120, 240, 4) # and some more text display.text("oontz oontz oontz", 25, 220, 240, 2) # and a bit more tiny text display.update() # Update the display utime.sleep(1.0 / 60)
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 exp.set_pen(black) exp.clear() exp.update() all(False) exp.update() while True: attract() game()
def show(self): display.update()
def blk(self): display.set_pen(0, 0, 0) display.clear() display.update()
i = 0 display.set_pen(0, 0, 0) display.clear() # chooses a pen colour based on the temperature display.set_pen(0, 255, 0) if temperature > 20: display.set_pen(255, 0, 0) if temperature < 13: display.set_pen(0, 0, 255) # draws the reading as a tall, thin rectangle display.rectangle(i, height - (temperature * 6), 6, height) # draws a white background for the text display.set_pen(255, 255, 255) display.rectangle(1, 1, 65, 33) # writes the reading as text in the white rectangle display.set_pen(0, 0, 0) display.text("{:.0f}".format(temperature) + "c", 3, 3, 0, 4) # time to update the display display.update() # waits for 5 seconds utime.sleep(5) # the next tall thin rectangle needs to be drawn 6 pixels to the right of the last one i += 6
display.init(buf) # sets up a handy function we can call to clear the screen def clear(): display.set_pen(0, 0, 0) display.clear() display.update() while True: if display.is_pressed(display.BUTTON_A): # if a button press is detected then... clear() # clear to black display.set_pen(255, 255, 255) # change the pen colour display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen display.update() # update the display utime.sleep(1) # pause for a sec clear() # clear to black again elif display.is_pressed(display.BUTTON_B): clear() display.set_pen(0, 255, 255) display.text("Button B pressed", 10, 10, 240, 4) display.update() utime.sleep(1) clear() elif display.is_pressed(display.BUTTON_X): clear() display.set_pen(255, 0, 255) display.text("Button X pressed", 10, 10, 240, 4) display.update() utime.sleep(1)