kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Improved bouncing balls demo
Keep the bouncing balls in the court and gives balls a speed in proportion to their size. Created from code in #63 Co-authored-by: Mark Emery <mark@markemerylimited.co.uk>pull/69/head
rodzic
890524ae9e
commit
e17aa01f5d
|
|
@ -22,32 +22,39 @@ class Ball:
|
||||||
# initialise shapes
|
# initialise shapes
|
||||||
balls = []
|
balls = []
|
||||||
for i in range(0, 100):
|
for i in range(0, 100):
|
||||||
|
r = random.randint(0, 10) + 3
|
||||||
balls.append(
|
balls.append(
|
||||||
Ball(
|
Ball(
|
||||||
random.randint(0, width),
|
random.randint(r, r + (width - 2 * r)),
|
||||||
random.randint(0, height),
|
random.randint(r, r + (height - 2 * r)),
|
||||||
random.randint(0, 10) + 3,
|
r,
|
||||||
random.randint(0, 255) / 128,
|
(14 - r) / 2,
|
||||||
random.randint(0, 255) / 128,
|
(14 - r) / 2,
|
||||||
display.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
|
display.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
display.set_pen(120, 40, 60)
|
display.set_pen(40, 40, 40)
|
||||||
display.clear()
|
display.clear()
|
||||||
|
|
||||||
for ball in balls:
|
for ball in balls:
|
||||||
ball.x += ball.dx
|
ball.x += ball.dx
|
||||||
ball.y += ball.dy
|
ball.y += ball.dy
|
||||||
|
|
||||||
if ball.x < 0 or ball.x > width:
|
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
|
ball.dx *= -1
|
||||||
if ball.y < 0 or ball.y > height:
|
|
||||||
|
if ball.y < ymin or ball.y > ymax:
|
||||||
ball.dy *= -1
|
ball.dy *= -1
|
||||||
|
|
||||||
display.set_pen(ball.pen)
|
display.set_pen(ball.pen)
|
||||||
display.circle(int(ball.x), int(ball.y), int(ball.r))
|
display.circle(int(ball.x), int(ball.y), int(ball.r))
|
||||||
|
|
||||||
display.update()
|
display.update()
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
|
|
|
||||||
Ładowanie…
Reference in New Issue