-
Notifications
You must be signed in to change notification settings - Fork 131
/
cross.py
executable file
·83 lines (61 loc) · 1.88 KB
/
cross.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
import time
from random import randint
import unicornhat as unicorn
print("""Cross
You should see randomly coloured dots crossing paths with each other.
If you're using a Unicorn HAT and only half the screen lights up,
edit this example and change 'unicorn.AUTO' to 'unicorn.HAT' below.
""")
unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(0)
unicorn.brightness(0.5)
width,height=unicorn.get_shape()
points = []
class LightPoint:
def __init__(self):
self.direction = randint(1, 4)
if self.direction == 1:
self.x = randint(0, width - 1)
self.y = 0
elif self.direction == 2:
self.x = 0
self.y = randint(0, height - 1)
elif self.direction == 3:
self.x = randint(0, width - 1)
self.y = height - 1
else:
self.x = width - 1
self.y = randint(0, height - 1)
self.colour = []
for i in range(0, 3):
self.colour.append(randint(100, 255))
def update_positions():
for point in points:
if point.direction == 1:
point.y += 1
if point.y > height - 1:
points.remove(point)
elif point.direction == 2:
point.x += 1
if point.x > width - 1:
points.remove(point)
elif point.direction == 3:
point.y -= 1
if point.y < 0:
points.remove(point)
else:
point.x -= 1
if point.x < 0:
points.remove(point)
def plot_points():
unicorn.clear()
for point in points:
unicorn.set_pixel(point.x, point.y, point.colour[0], point.colour[1], point.colour[2])
unicorn.show()
while True:
if len(points) < 10 and randint(0, 5) > 1:
points.append(LightPoint())
plot_points()
update_positions()
time.sleep(0.03)