-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex9_1CreatingRandomSizedBalls.pyde
More file actions
34 lines (29 loc) · 1011 Bytes
/
ex9_1CreatingRandomSizedBalls.pyde
File metadata and controls
34 lines (29 loc) · 1011 Bytes
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
# Creating balls of different sizes (5 to 50 units randomly) using CLASS and METHODS.
ballList = [] # empty list to put the balls in
class Ball:
def __init__(self, x, y):
'''initializing a Ball'''
self.xcor = x
self.ycor = y
self.xvel = random(-2, 2)
self.yvel = random(-2, 2)
self.col = color(random(255), random(255), random(255))
self.sz = random(5, 50)
def update(self):
self.xcor += self.xvel
self.ycor += self.yvel
# if the ball reaches a wall, switch direction
if self.xcor > width or self.xcor < 0:
self.xvel = -self.xvel
if self.ycor > height or self.ycor < 0:
self.yvel = -self.yvel
fill(self.col)
ellipse(self.xcor, self.ycor, self.sz, self.sz)
def setup():
size(600, 600)
for i in range(100):
ballList.append(Ball(random(width), random(height)))
def draw():
background(0) # black
for ball in ballList:
ball.update()