forked from pimoroni/interstate75
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathballs_demo.py
60 lines (46 loc) · 1.3 KB
/
balls_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import random
from interstate75 import Interstate75, DISPLAY_INTERSTATE75_128X128
i75 = Interstate75(display=DISPLAY_INTERSTATE75_128X128)
graphics = i75.display
width = i75.width
height = i75.height
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, 75):
r = random.randint(0, 3) + 3
balls.append(
Ball(
random.randint(r, r + (width - 2 * r)),
random.randint(r, r + (height - 2 * r)),
r,
(7 - r) / 4,
(7 - r) / 4,
graphics.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
)
)
BG = graphics.create_pen(0, 0, 0)
while True:
graphics.set_pen(BG)
graphics.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
graphics.set_pen(ball.pen)
graphics.circle(int(ball.x), int(ball.y), int(ball.r))
i75.update()