pimoroni-pico/micropython/examples/gfx_pack/balls_demo.py

66 wiersze
1.2 KiB
Python

import time
import random
from gfx_pack import GfxPack
"""
Add something here Gee
"""
gp = GfxPack()
gp.set_backlight(0, 0, 0, 255)
display = gp.display
WIDTH, HEIGHT = display.get_bounds()
class Ball:
def __init__(self, x, y, r, dx, dy, pen):
self.x = x
self.y = y
self.r = r
self.dx = dx
self.dy = dy
self.pen = pen
# initialise shapes
balls = []
for i in range(0, 10):
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,
random.randint(0, 15),
)
)
while True:
display.set_pen(0)
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:
ball.dy *= -1
display.set_pen(ball.pen)
display.circle(int(ball.x), int(ball.y), int(ball.r))
display.update()
time.sleep(0.05)