def anykey(): pressed = False if explorer.is_pressed(explorer.BUTTON_A): pressed = True if explorer.is_pressed(explorer.BUTTON_Y): pressed = True return pressed
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 anykey(): key = "" if exp.is_pressed(exp.BUTTON_A): while exp.is_pressed(exp.BUTTON_A): pass key = "A" if exp.is_pressed(exp.BUTTON_B): while exp.is_pressed(exp.BUTTON_B): pass key = "B" if exp.is_pressed(exp.BUTTON_X): while exp.is_pressed(exp.BUTTON_X): pass key = "X" if exp.is_pressed(exp.BUTTON_Y): while exp.is_pressed(exp.BUTTON_Y): pass key = "Y" return key
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.text("ADC0:", 20, 20, 100) explorer.text("ADC1:", 20, 40, 100) explorer.text("ADC2:", 20, 60, 100) explorer.set_pen(adc0 * 2, 0, 0) explorer.circle(90 + adc0, 26, 10) explorer.set_pen(0, adc1 * 2, 0) explorer.circle(90 + adc1, 46, 10) explorer.set_pen(0, 0, adc2 * 2) explorer.circle(90 + adc2, 66, 10) # example for the on-board A/B/X/Y buttons if explorer.is_pressed(explorer.BUTTON_A): explorer.set_pen(255, 255, 255) explorer.text("Button A pressed", 20, 110, 200) elif explorer.is_pressed(explorer.BUTTON_B): explorer.set_pen(255, 255, 255) explorer.text("Button B pressed", 20, 110, 200) elif explorer.is_pressed(explorer.BUTTON_X) and explorer.is_pressed( 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)
x = 0 # ADC values are stored as 16 bit values (2^16 = 65535). First 20 pixels are used for the title scale_factor = (HEIGHT - 20) / 65535 # loop forever while True: # clear current x position of screen in black display.set_pen(0, 0, 0) display.rectangle(128, 20, WIDTH - 128, 10) display.rectangle(x, 20, 1, HEIGHT) # loop through each ADC channel and toggle for i in range(4): # toggle ADC channel enabled if button is pressed if display.is_pressed(buttons[i]): while display.is_pressed(buttons[i]): utime.sleep(0.1) enabled[i] = not enabled[i] # draw channel name in black if disabled, or in correct colour if enabled if enabled[i]: display.set_pen(*colors[i]) # draw trace value = adc[i].read_u16() y = int(HEIGHT - (value * scale_factor)) display.rectangle(x, min(previous_values[i], y), 1, abs(y - previous_values[i]) + 1) voltage = "{:.2f}".format(value * 3.3 / 65536) display.text(voltage, 128 + (i * 22), 20, 1, 1)
# # This is the file that I saved as main.py to act as the beginnings of a boot menu, which will then call the bird game after a button click # width = explorer.get_width() height = explorer.get_height() display_buffer = bytearray(width * height * 2) # 2-bytes per pixel (RGB565) explorer.init(display_buffer) #explorer.set_backlight(1.0) explorer.set_audio_pin(0) clicked = False unclicked = True while unclicked: explorer.set_pen(0, 0, 0) explorer.clear() explorer.set_pen(155, 155, 155) explorer.text("Press B to flap the bird", 20, 110, 200) if explorer.is_pressed(explorer.BUTTON_B): unclicked = False explorer.update() time.sleep(0.01) explorer.set_pen(0, 0, 0) explorer.clear() explorer.update()
# draw title display.set_pen(0, 0, 0) display.text("Sunflower", 0, 0, 240, 5) # draw bar to show how far through the day we are display.set_pen(255, 0, 0) display.rectangle(0, 50, int(240 * day_progress), 10) display.set_pen(0, 0, 0) # manually control servo 1 if mode == 0: # move the servos if the button is pressed if display.is_pressed(display.BUTTON_A): display.text("A", 0, 50, 240, 8) if angle1 > 0: angle1 -= 2 elif display.is_pressed(display.BUTTON_B): display.text("B", 0, 150, 240, 8) if angle1 < 180: angle1 += 2 # manually control servo 2 elif mode == 1: # move the servos if the button is pressed if display.is_pressed(display.BUTTON_A): display.text("A", 0, 50, 240, 8) if angle2 > 0: angle2 -= 2
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()
import utime # Initialise display with a bytearray display buffer buf = bytearray(display.get_width() * display.get_height() * 2) 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()
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()
while True: explorer.set_pen(120, 40, 60) explorer.clear() adc0 = int(explorer.get_adc(0) * 120) adc1 = int(explorer.get_adc(1) * 120) adc2 = int(explorer.get_adc(2) * 120) explorer.set_pen(255, 255, 255) explorer.text("ADC0:", 20, 20, 100) explorer.text("ADC1:", 20, 40, 100) explorer.text("ADC2:", 20, 60, 100) explorer.text("Button:", 20, 80, 100) if explorer.is_pressed(explorer.BUTTON_A): if A_Pressed == True: A_Pressed = False else: A_Pressed = True if explorer.is_pressed(explorer.BUTTON_B): if B_Pressed == True: B_Pressed = False else: B_Pressed = True if explorer.is_pressed(explorer.BUTTON_X): if X_Pressed == True: X_Pressed = False else: