def draw_sprite(self, colour): display.set_pen(colour["r"], colour["g"], colour["b"]) for dy, row in enumerate(self.pixel_map): for dx, pixel in enumerate(row): if pixel: display.pixel(self._x + dx, self._y + dy)
def update(self): for y in range(0, height): # We precompute the rows for a small performance gain. row = y * width # For each pixel in each row, we calculate the colours that will be # rendered on the previous row, on the next call to update. next_row = (y - 1) * width for x in range(0, width): color = self.fire[row + x] pen = colorScale[color] if y > 0: new_x = x # We're already at the first color index and can't move further. if color > 0: rand = random.randint(0, 3) # Maybe move to the next colour. color = color - (rand & 1) # Spread the fire left and right. new_x = new_x + rand - 1 self.fire[next_row + new_x] = color display.set_pen(pen) display.pixel(x, y) display.update()
def time_user(side, max_reaction_time): # Time user's reactions t1 = utime.ticks_ms() # Start timer while True: utime.sleep_ms(1) if ( (side == "<") and (display.is_pressed(display.BUTTON_B)) and (display.is_pressed(display.BUTTON_Y) == False) ) \ or ( (side == ">") and (display.is_pressed(display.BUTTON_Y)) and (display.is_pressed(display.BUTTON_B) == False) ): t2 = utime.ticks_ms() # End timer break elif utime.ticks_diff( utime.ticks_ms(), t1) > max_reaction_time: # If 3 seconds has passed t2 = utime.ticks_add( t1, max_reaction_time ) # End timer, set to max time even if it's slightly passed 3 seconds break # Calculate reaction time and display results reaction_time = utime.ticks_diff(t2, t1) / 1000 time_text = "T: " + "{:0<5}".format( str(reaction_time)) # Display reaction time to 3 decimals (ms) clear_screen() display.set_pen(255, 255, 255) display.text(time_text, 50, 30, 240, 4) display.update() utime.sleep_ms(750) # Pause to read reaction time return reaction_time
def clear_screen(background="black"): if background == "white": display.set_pen(255, 255, 255) else: display.set_pen(0, 0, 0) display.clear() display.update()
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 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 one_sample(): 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
def initDisplay(): width = display.get_width() height = display.get_height() display_buffer = bytearray(width * height * 2) # 2-bytes per pixel (RGB565) display.init(display_buffer) display.set_backlight(1) display.set_pen(0, 0, 0) # black display.clear() display.set_pen(100, 100, 100) # white
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 straight(lead_time): clear_screen() # Green circle for Go, straight-ahead until next turn display.set_pen(0, 255, 0) display.circle(120, 50, 20) display.update() # Leave straight-ahead on screen for the length of the straight utime.sleep(lead_time) # Clear screen, small pause for transistion clear_screen() utime.sleep_ms(1)
def show(self): food_x, food_y = self.pos food_red, food_green, food_blue = food_color display.set_pen(food_red, food_green, food_blue) # calculate center of tile on canvas tile_x = (tile_size * food_x) + tile_size//2 tile_y = (tile_size * food_y) + tile_size//2 radius = (tile_size-2)//2 display.circle(tile_x, tile_y, radius)
def menu(): display.set_led(0, 0, 0) display.set_pen(BLACK) display.clear() display.set_pen(WHITE) display.text("Press any button!", 10, 10, 240, (3, 6)[best == 0]) if best > 0: display.text("Hiscore: " + str(best), 10, 68, 240, 3) display.update() # Update the display while True: if display.is_pressed(display.BUTTON_A) or display.is_pressed( display.BUTTON_B) or display.is_pressed( display.BUTTON_X) or display.is_pressed(display.BUTTON_Y): return utime.sleep(0.5)
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 show(self): snake_red, snake_green, snake_blue = snake_color display.set_pen(snake_red, snake_green, snake_blue) current_node = self.head while current_node != None: if current_node != self.head: x1, y1 = current_node.pos x2, y2 = previous_node.pos invisible = (abs(x1-x2)>1)or(abs(y1-y2)>1) if not invisible: x1 *= tile_size y1 *= tile_size x2 *= tile_size y2 *= tile_size x1 += tile_size//2 y1 += tile_size//2 x2 += tile_size//2 y2 += tile_size//2 self.line(x1, y1, x2, y2) else: # draw circle for snake head grid_x, grid_y = current_node.pos canvas_x = (tile_size * grid_x) canvas_y = (tile_size * grid_y) center_x = canvas_x+(tile_size//2) center_y = canvas_y+(tile_size//2) radius = (tile_size-4)//2 display.circle(center_x, center_y, radius) previous_node = current_node current_node = current_node.next
def next_turn(lap_delta, max_reaction_time, direction, lead_time): clear_screen() utime.sleep_ms(250) # Small pause for transition straight(lead_time) # Prepare large chevrons indicating turn direction turn_text = direction * 3 display.set_pen(255, 255, 255) display.text(turn_text, 100, 50, 240, 4) # Prepare correct key, label with chevrons for the turn display.set_pen(0, 255, 0) if direction == "<": display.text(turn_text, 20, 105, 240, 2) else: display.text(turn_text, 200, 105, 240, 2) # Show the turn direction now display.update() # Time the user's reaction lap_delta += time_user(direction, max_reaction_time) return lap_delta
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 run(self): # Starting angle (unrotated in any dimension) angleX, angleY, angleZ = 0, 0, 0 distance_delta = 0.1 while 1: # It will hold transformed vertices. t = [] for v in self.vertices: # Rotate the point around X axis, then around Y axis, and finally around Z axis. r = v.rotateX(angleX).rotateY(angleY).rotateZ(angleZ) # Transform the point from 3D to 2D p = r.project(*self.projection) # Put the point in the list of transformed vertices t.append(p) display.set_pen(40, 40, 40) display.clear() display.set_pen(255, 255, 255) for e in self.edges: drawDDA(*to_int(t[e[0]].x, t[e[0]].y, t[e[1]].x, t[e[1]].y)) display.update() # Continue the rotation angleX += self.rotateX angleY += self.rotateY angleZ += self.rotateZ self.projection[3] += distance_delta if self.projection[3] > 12.0: distance_delta = -0.1 if self.projection[3] < 2.0: distance_delta = 0.1
def flash_a(): picodisplay.set_pen(175, 175, 175) picodisplay.clear() picodisplay.set_pen(255, 255, 255) picodisplay.text("Morning...", 5, 10, 240, 4) picodisplay.set_pen(0, 0, 0) picodisplay.text("Its monday!", 5, 50, 240, 5) picodisplay.update()
def flash_b(): picodisplay.set_pen(100, 100, 100) picodisplay.clear() picodisplay.set_pen(255, 255, 255) picodisplay.text("F#$%!", 5, 10, 240, 5) picodisplay.set_pen(0, 0, 0) picodisplay.text("Its monday!", 5, 50, 240, 5) picodisplay.update()
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 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 failed(): global best display.set_pen(RED) display.clear() display.set_pen(YELLOW) display.text("Game Over!", 10, 10, 240, 6) # Add some text display.update() # Update the display utime.sleep(1) display.set_pen(RED) display.clear() display.set_pen(YELLOW) display.text("Score: " + str(score), 10, 10, 240, 3) if score > best: display.text("New Hiscore!", 10, 68, 240, 3) best = score else: display.text("Hiscore: " + str(best), 10, 68, 240, 3) display.update() # Update the display utime.sleep(3)
24, Pin.IN) # reading GP24 tells us whether or not USB power is connected conversion_factor = 3 * 3.3 / 65535 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)
def __init__(self, y): self.y = y # initialise shapes balls = [] for i in range(0, 50): balls.append( Ball(random.randint(0, width), random.randint(0, height), random.randint(0, 10) + 3, random.randint(0, 255) / 128, random.randint(0, 255) / 128, display.create_pen(0, 0, 155))) bat = Bat(1) while True: display.set_pen(0, 0, 0) display.clear() for ball in balls: 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:
random.randint(0,width), random.randint(0,height), random.randint(0,10) + 5, random.randint(0,255) / 128, random.randint(0,255) / 128, display.create_pen(random.randint(75, 200), random.randint(75, 200), random.randint(75, 200)) ) ) bat = Bat(1) missile = Missile(width-20,0) #display.set_led(0,150,0) while True: display.set_pen(40, 40, 40) display.clear() #while ballcount>0: print(ballcount) for ball in balls: ball.x += ball.dx ball.y += ball.dy if ball.x < 0 or ball.x > width: ball.dx *= -1 if ball.y < 0 or ball.y > height: ball.dy *= -1 if missile.active:
balls = [] for i in range(0, 100): r = random.randint(0, 10) + 3 balls.append( Ball( random.randint(r, r + (width - 2 * r)), random.randint(r, r + (height - 2 * r)), r, (14 - r) / 2, (14 - r) / 2, display.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), )) while True: display.set_pen(40, 40, 40) display.clear() for ball in balls: ball.x += ball.dx ball.y += ball.dy xmax = width - ball.r xmin = ball.r ymax = height - ball.r ymin = ball.r if ball.x < xmin or ball.x > xmax: ball.dx *= -1 if ball.y < ymin or ball.y > ymax:
def clearDisplay(): display.set_pen(BLACK) display.clear() display.update()