A simple animated bouncing duck on the Cosmic Unicorn display.
Created
March 29, 2024 20:21
-
-
Save mikegoatly/d48023390d0c56c991db2a5ebaf00d2b to your computer and use it in GitHub Desktop.
Flying duck animation for the Pimorini Cosmic Unicorn display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from cosmic import CosmicUnicorn | |
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY | |
# create cosmic object and graphics surface for drawing | |
cu = CosmicUnicorn() | |
graphics = PicoGraphics(DISPLAY) | |
width = CosmicUnicorn.WIDTH | |
height = CosmicUnicorn.HEIGHT | |
last_time = time.ticks_ms() | |
x = 0 | |
y = 0 | |
vx = 0.5 | |
vy = 0 | |
size = 6 | |
flap = 0 | |
black = graphics.create_pen(0, 0, 0) | |
yellow = graphics.create_pen(255, 255, 0) | |
orange = graphics.create_pen(255, 65, 0) | |
dark_yellow = graphics.create_pen(120, 70, 0) | |
cu.set_brightness(0.8) | |
def draw_duck(x, y, flap_up): | |
# Draw the body | |
graphics.set_pen(yellow) | |
graphics.rectangle(x + 1, y, 2, 3) | |
graphics.pixel(x + 3, y + 2) | |
graphics.rectangle(x + 1, y + 3, 4, 2) | |
graphics.pixel(x + 5, y + 3) | |
# Draw the beak | |
graphics.set_pen(orange) | |
graphics.pixel(x, y + 1) | |
# And the feet | |
graphics.line(x + 2, y + 5, x + 4, y + 5) | |
# The wings... | |
graphics.set_pen(dark_yellow) | |
graphics.rectangle(x + 2, y + 2, 2, 2) | |
if flap_up: | |
graphics.pixel(x + 4, y + 1) | |
graphics.pixel(x + 5, y + 2) | |
graphics.pixel(x + 4, y + 2) | |
else: | |
graphics.line(x + 3, y + 4, x + 5, y + 4) | |
while True: | |
time_ms = time.ticks_ms() | |
# clear the screen | |
graphics.set_pen(black) | |
graphics.clear() | |
draw_duck(int(x), int(y), int(flap)) | |
flap += 0.2 | |
flap = flap % 2 | |
# update the ball position | |
x += vx | |
y += vy | |
# Apply gravity to acceleration | |
vy += 0.03 | |
# bounce the ball off the edges | |
if x < 0: | |
vx = -vx | |
x = 0 | |
elif x > width - size: | |
vx = -vx | |
x = width - size | |
if y < 0: | |
vy = 0 | |
y = 0 | |
elif y > height - size: | |
vy = -vy | |
y = height - size | |
# update the display | |
cu.update(graphics) | |
# Sleep to maintain a constant frame rate | |
time.sleep_ms(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment