-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquare.py
74 lines (62 loc) · 2.35 KB
/
Square.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
import random
class Square:
def __init__(self, w, h, c, p):
# Top left x/y coordinates of the square.
self.x = random.randint(0,p.width)
self.y = random.randint(0,p.height)
# Dimensions of the square
self.width = w
self.height = h
# Reference to the panel object
self.panel = p
# Color of square
self.col = c
# Movement
self.velocity = (random.uniform(1.0,2.0), random.uniform(1.0,2.0))
#self.brightness = random.random()
self.brightness = 0.9
self.bAdjust = 0.01
def update(self):
# Move by x and y from velocity
self.x += self.velocity[0]
self.y += self.velocity[1]
#self.brightness += self.bAdjust
self.bound()
self.show()
def bound(self):
if (self.x < 0):
self.x = 0
self.velocity = (self.velocity[0] * -1, self.velocity[1])
if (self.x + self.width > self.panel.width - 1):
self.x = self.panel.width - self.width
self.velocity = (self.velocity[0] * -1, self.velocity[1])
if (self.y < 0):
self.y = 0
self.velocity = (self.velocity[0], self.velocity[1] * -1)
if (self.y + self.height > self.panel.height - 1):
self.y = self.panel.height - self.height
self.velocity = (self.velocity[0], self.velocity[1] * -1)
if (self.brightness < 0):
self.brightness = 0
self.bAdjust *= -1
if (self.brightness > 1.0):
self.brightness = 1.0
self.bAdjust *= -1
def show(self):
roundX = round(self.x)
roundY = round(self.y)
for curX in range(roundX, roundX + self.width):
for curY in range(roundY, roundY + self.height):
self.panel.setPixel(curX, curY, self.col, self.brightness)
class BouncingSquares():
def __init__(self, w, h, numSquares, p):
self.squares = []
self.panelRef = p
#col = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
for i in range(numSquares):
col = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
self.squares.append(Square(w,h,col,p))
def update(self):
self.panelRef.setBackground((0,0,0))
for i in self.squares:
i.update()