def task(s, radius, pen): global display # print(pen) for i in range(1, s): display.set_pen(pen) display.rectangle(random.randint(0, width - 1), random.randint(0, height - 1), radius, radius)
def play(): count = 0 seq = [] while True: seq.append(random.randint(0, 3)) for i in seq: # display the sequence clearDisplay() utime.sleep(0.5) display.set_pen(rpen[i]) display.rectangle(rect[i]["x"], rect[i]["y"], rwidth, rheight) display.update() utime.sleep(sleep) clearDisplay() for i in seq: # get buttons for the sequence b = getButton() display.set_pen(rpen[b]) display.rectangle(rect[b]["x"], rect[b]["y"], rwidth, rheight) display.update() if b != i: display.set_led(255, 0, 0) utime.sleep(2) display.set_led(0, 0, 0) return len(seq) - 1 display.set_led(0, 255, 0) utime.sleep(1) display.set_led(0, 0, 0) clearDisplay()
def start(lap_delta, max_reaction_time): # Draw the red start lights one at a time clear_screen() display.set_pen(255, 0, 0) for light in range(5): display.circle(40 + (light * 40), 50, 10) display.update() utime.sleep(1) delay = math.fmod(utime.ticks_ms(), 4) + 1 utime.sleep(delay) # Random delay before go lights out # Remove lights display.set_pen(0, 0, 0) display.rectangle(20, 35, 200, 30) # No effect until next display.update() # Click the correct side to "Go" display.set_pen(0, 255, 0) # Green for GO # display.circle(120, 50, 20) # Pick side randomly for the start button, indicated by GO side = random.choice(["<", ">"]) if side == "<": display.text("G0", 20, 105, 240, 2) else: display.text("G0", 200, 105, 240, 2) # Cover up start lights and display "Go" display.update() # Time the user's reaction lap_delta += time_user(side, max_reaction_time) return lap_delta
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
def drawtree(x, h): #draw the trees x = x * 10 h = h * 10 display.set_pen(brown) display.rectangle(x + 3, height - h, 4, h) display.set_pen(green) display.rectangle(x, height - h, 10, 10)
def makemagazin(): display.set_pen(makeRed) display.text("Make:", 10, 10, 64, 6) display.set_pen(makeBlue) #display.rectangle(20, 20, 40, 20) display.text("Deutschlands gefaehrlichstes DIY-Magazin", 10, 5, 240, 1) display.set_pen(makeGray) display.rectangle(5, 50, width - 10, height - 55)
def show(self): wall_red, wall_blue, wall_green = wall_color for wall in self.walls: grid_x, grid_y = wall tile_x = grid_x * tile_size tile_y = grid_y * tile_size display.set_pen(wall_red, wall_blue, wall_green) display.rectangle(tile_x, tile_y, tile_size, tile_size) display.set_pen(wall_red//2, wall_blue//2, wall_green//2) display.rectangle(tile_x+2, tile_y+2, tile_size-4, tile_size-4)
def drawufo(x, y, r, ufopen): #display.circle(int(x), int(y), int(r)) x = int(x) y = int(y) r = 2 row = 0 col = 0 for line in ufogrid: for pixel in line: colour = str(pixel) if colour != "0": display.set_pen(ufopen) display.rectangle(x + (col * r), y + (row * r), r, r) col += 1 row += 1 col = 0
def debug_pattern(self): alternate_color = False for j in range(grid_h): for i in range(grid_w): if alternate_color: display.set_pen(0, 0, 255) else: display.set_pen(255, 0, 255) tile_x = i * tile_size tile_y = j * tile_size display.rectangle(tile_x, tile_y, tile_size, tile_size) alternate_color = not alternate_color alternate_color = not alternate_color
def line(self, x1, y1, x2, y2): start_x = min(x1, x2) start_y = min(y1, y2) line_thickness = 4 offset = line_thickness//2 start_x -= offset start_y -= offset # for horizontal lines if x1 == x2: line_width = offset * 2 line_height = offset + abs(y1-y2) + offset # for vertical lines elif y1 == y2: line_width = offset + abs(x1-x2) + offset line_height = offset * 2 display.rectangle(start_x, start_y, line_width, line_height)
def intro(): clear_screen("white") # Draw red border display.set_pen(255, 0, 0) display.rectangle(1, 1, 238, 133) display.set_pen(255, 255, 255) display.rectangle(4, 4, 232, 127) # Display "Pico" pico_x_base = 30 # 93 works well for centre, 70 original pico_y_base = 20 display.set_pen(0, 0, 0) display.text("Pico", pico_x_base, pico_y_base, 0, 3) # Display "F-Won" logo_x_base = 30 ## 70 original logo_y_base = 50 display.set_pen(255, 0, 0) display.text("F-Won", logo_x_base, logo_y_base, 0, 4) display.set_pen(0, 0, 0) display.text("o", logo_x_base + 60, logo_y_base, 0, 4) display.set_pen(255, 255, 255) display.rectangle(logo_x_base + 76, logo_y_base + 4, 4, 4) # Display "By CT" name_x_base = 140 # 100 original name_y_base = 100 display.set_pen(0, 0, 0) display.text("By CT", name_x_base, name_y_base, 240, 3) display.update() utime.sleep(3)
def drawplane(x, y): #draw the plane display.set_pen(red) display.rectangle(x, y, 4, 5) display.set_pen(white) display.rectangle(x + 2, y + 5, 18, 5) display.set_pen(black) display.rectangle(x + 18, y + 5, 2, 2)
def finish(lap_delta, time_to_line): clear_screen() straight(time_to_line) # Draw chequered flag display.set_pen(255, 255, 255) display.rectangle(29, 19, 182, 102) pen_white = True rw = 20 rh = 20 for ry in range(20, 120, 20): # 20 to 100 in 20's for rx in range(30, 210, 20): # 30 to 190 in 20's if pen_white: display.set_pen(0, 0, 0) else: display.set_pen(255, 255, 255) pen_white = not (pen_white) display.rectangle(rx, ry, rw, rh) # Display the chequered flag for a few seconds display.update() utime.sleep(5) # Show finish time clear_screen() display.set_pen(255, 255, 255) display.text("Lap Delta:", 50, 20, 240, 3) display.update() display.set_pen(255, 0, 255) time_text = "{:.3f}".format( lap_delta) # Display reaction time to 3 decimals (ms) display.text(time_text, 50, 50, 240, 4) # Wait for key press to continue display.set_pen(0, 255, 0) display.text("Continue", 125, 105, 240, 2) display.update() while True: utime.sleep_ms(1) if display.is_pressed(display.BUTTON_Y): break # Break loop to continue return lap_delta
y, x = random.random(), random.random() if math.pow(y, 2) + math.pow(x, 2) <= 1.0: display.set_pen(0, 255, 0) display.pixel(int(x * height), int(y * height)) return 1 else: display.set_pen(255, 0, 0) display.pixel(int(x * height), int(y * height)) return 0 display.set_pen(0, 0, 200) display.text("Happy Pi-Day!", 140, 90, 100, 3) runs = 100 #wieviele Iterationen zwischen Screen Refresh iterations = 100000 # Durchläufe pi = 0.0 for it in range(1, iterations): for i in range(runs): pi += one_sample() # Text Info display.set_pen(255, 255, 255) display.rectangle(140, 10, 120, 46) display.set_pen(0, 0, 200) display.text("Pi: " + str(pi * 4.0 / runs / it), 140, 10, 200, 2) # Pi display.text("It: " + str(runs * it), 140, 26, 200, 2) # Iterationen display.text("Fe: " + str((pi * 4.0 / runs / it - math.pi) * 100), 140, 42, 200, 2) # Fehler % display.update()
reading = sensor_temp.read_u16() * conversion_factor temperature = 27 - (reading - 0.706) / 0.001721 temperatures.append(temperature) # shifts the temperatures history to the left by one sample if len(temperatures) > width // bar_width: temperatures.pop(0) i = 0 for t in temperatures: # chooses a pen colour based on the temperature display.set_pen(*temperature_to_color(t)) # draws the reading as a tall, thin rectangle display.rectangle(i, height - (round(t) * 4), bar_width, height) # the next tall thin rectangle needs to be drawn # "bar_width" (default: 5) pixels to the right of the last one i += bar_width # heck lets also set the LED to match display.set_led(*temperature_to_color(temperature)) # draws a white background for the text display.set_pen(255, 255, 255) display.rectangle(1, 1, 100, 25) # writes the reading as text in the white rectangle display.set_pen(0, 0, 0) display.text("{:.2f}".format(temperature) + "c", 3, 3, 0, 3)
# 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) # heck lets also set the LED to match display.set_led(0, 255, 0) if temperature > 20: display.set_led(255, 0, 0) if temperature < 13: display.set_led(0, 0, 255) # draws the reading as a tall, thin rectangle display.rectangle(i, height - (temperature * 4), 5, height) # draws a white background for the text display.set_pen(255, 255, 255) display.rectangle(1, 1, 50, 25) # writes the reading as text in the white rectangle display.set_pen(0, 0, 0) display.text("{:.0f}".format(temperature) + "c", 3, 3, 0, 3) # time to update the display display.update() # waits for 5 seconds utime.sleep(5)
drawplane(plane.x, plane.y) #move the plane if not plane.crashed: if plane.x < width: plane.x += 1 else: if plane.y < height - 10: plane.y += 5 plane.x = 0 else: plane.flying = False if missile.active: display.set_pen(brightwhite) display.rectangle(missile.x, missile.y, 5, 5) if missile.y < height: missile.y += 3 else: missile.active = False if display.is_pressed(display.BUTTON_A): if not missile.active: #fire missile missile.active = True missile.x = plane.x + 5 missile.y = plane.y + 10 if display.is_pressed(display.BUTTON_B): jungle = [] plane = Plane(0, 0)
ball.x += ball.dx ball.y += ball.dy if ball.x <= 0: if ball.y > bat.y + 25 or ball.y < bat.y: #display.set_led(255,0,0) ball.dx = 0 ball.dy = 0 ball.x = 500 ball.y = 500 if ball.x < 0 or ball.x > width: ball.dx *= -1 if ball.y < 0 or ball.y > height: ball.dy *= -1 display.set_pen(ball.pen) display.circle(int(ball.x), int(ball.y), int(ball.r)) if display.is_pressed(display.BUTTON_Y) and bat.y < height - 25: bat.y = bat.y + 1 if display.is_pressed(display.BUTTON_X) and bat.y > 0: bat.y = bat.y - 1 display.set_pen(100, 100, 100) display.rectangle(0, bat.y, 10, 25) if display.is_pressed(display.BUTTON_B): display.set_led(0, 0, 0) display.update()
def mandelbrot(c): z = 0 n = 0 while abs(z) <= 2 and n < MAX_ITER: z = z * z + c n += 1 return n display.set_pen(255, 255, 255) display.clear() display.update() # calculates some resolutions and refines (stupidly) step = 8 MAX_ITER = 64 for step in range(8, 0, -1): for x in range(0, width - 1, step): for y in range(0, height - 1, step): # Convert pixel coordinate to complex number c = complex(RE_START + (x / width) * (RE_END - RE_START), IM_START + (y / height) * (IM_END - IM_START)) # Compute the number of iterations m = mandelbrot(c) color = 255 - int(m * 255 / MAX_ITER) display.set_pen(color, color, color) display.rectangle(x, y, step, step) display.update()
ball.dx = 0 ball.dy = 0 ball.x = 400 ball.y = 400 ballcount = ballcount-1 print(ballcount)#maxballs = maxballs-1 display.set_pen(ball.pen) display.circle(int(ball.x), int(ball.y), int(ball.r)) if display.is_pressed(display.BUTTON_Y) and bat.y < height- 25: bat.y = bat.y +1 if display.is_pressed(display.BUTTON_X) and bat.y > 0: bat.y = bat.y -1 display.set_pen(100,100,100) display.rectangle(width-10,bat.y,10,20) if missile.active: display.rectangle(missile.x,missile.y, 20, 5) if missile.x>-10: missile.x -=4 else: missile.active=False if display.is_pressed(display.BUTTON_A): if not missile.active: missile.x=width-20 missile.y=bat.y+10 missile.active=True if display.is_pressed(display.BUTTON_B): balls = []
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, 75) 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 + 75 score += 1 if pillar.x < 39 and pillar.x > 6: if bird.y < pillar.holetop or bird.y + 28 > pillar.holebottom: bird.crashed = True
full_battery = 4.2 # these are our reference voltages for a full/empty battery, in volts empty_battery = 2.8 # the values could vary by battery size/manufacturer so you might need to adjust them while True: # convert the raw ADC read into a voltage, and then a percentage voltage = vsys.read_u16() * conversion_factor percentage = 100 * ((voltage - empty_battery) / (full_battery - empty_battery)) if percentage > 100: percentage = 100.00 # draw the battery outline display.set_pen(0, 0, 0) display.clear() display.set_pen(190, 190, 190) display.rectangle(0, 0, 220, 135) display.rectangle(220, 40, 20, 55) display.set_pen(0, 0, 0) display.rectangle(3, 3, 214, 129) # draw a green box for the battery level display.set_pen(0, 255, 0) display.rectangle(5, 5, round(210 / 100 * percentage), 125) # add text display.set_pen(255, 0, 0) if charging.value() == 1: # if it's plugged into USB power... display.text("Charging!", 15, 55, 240, 4) else: # if not, display the battery stats display.text('{:.2f}'.format(voltage) + "v", 15, 10, 240, 5) display.text('{:.0f}%'.format(percentage), 15, 50, 240, 5)
# "GUI" display.set_pen(200, 30, 30) display.text("Exit", 5, 15, 230, 3) display.set_pen(30, 200, 30) display.text("Up", 200, 15, 30, 3) display.set_pen(30, 30, 200) display.text("Start", 5, 99, 230, 3) display.set_pen(30, 200, 30) display.text("Down", 164, 99, 230, 3) display.update() p = 0 while not (display.is_pressed(display.BUTTON_A)): display.set_pen(255, 255, 255) display.rectangle(0, 40, 240, 60) if display.is_pressed(display.BUTTON_Y): p = p + 1 time.sleep(0.07) if p > len(files) - 1: p = 0 if display.is_pressed(display.BUTTON_X): p = p - 1 time.sleep(0.07) if p < 0: p = len(files) - 1 if display.is_pressed(display.BUTTON_B): __import__(files[p]) exit(0)