Skip to content

Commit be972b6

Browse files
committed
Examples: adding balls_demo.py
1 parent 95124f5 commit be972b6

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

examples/balls_demo.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import random
2+
from interstate75 import Interstate75, DISPLAY_INTERSTATE75_128X128
3+
4+
i75 = Interstate75(display=DISPLAY_INTERSTATE75_128X128)
5+
graphics = i75.display
6+
7+
width = i75.width
8+
height = i75.height
9+
10+
11+
class Ball:
12+
def __init__(self, x, y, r, dx, dy, pen):
13+
self.x = x
14+
self.y = y
15+
self.r = r
16+
self.dx = dx
17+
self.dy = dy
18+
self.pen = pen
19+
20+
21+
# initialise shapes
22+
balls = []
23+
for i in range(0, 75):
24+
r = random.randint(0, 3) + 3
25+
balls.append(
26+
Ball(
27+
random.randint(r, r + (width - 2 * r)),
28+
random.randint(r, r + (height - 2 * r)),
29+
r,
30+
(7 - r) / 4,
31+
(7 - r) / 4,
32+
graphics.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
33+
)
34+
)
35+
36+
BG = graphics.create_pen(0, 0, 0)
37+
38+
while True:
39+
graphics.set_pen(BG)
40+
graphics.clear()
41+
42+
for ball in balls:
43+
ball.x += ball.dx
44+
ball.y += ball.dy
45+
46+
xmax = width - ball.r
47+
xmin = ball.r
48+
ymax = height - ball.r
49+
ymin = ball.r
50+
51+
if ball.x < xmin or ball.x > xmax:
52+
ball.dx *= -1
53+
54+
if ball.y < ymin or ball.y > ymax:
55+
ball.dy *= -1
56+
57+
graphics.set_pen(ball.pen)
58+
graphics.circle(int(ball.x), int(ball.y), int(ball.r))
59+
60+
i75.update()

0 commit comments

Comments
 (0)