-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animate.py
46 lines (35 loc) · 1.14 KB
/
Animate.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
from tkinter import *
import time
import random
WIDTH = 900
HEIGHT = 800
tk = Tk()
canvas = Canvas(tk,width = WIDTH, height = HEIGHT)
tk.title('Bouncing balls')
canvas.pack()
class Ball:
def __init__(self, color, size=50):
self.shape = canvas.create_rectangle(10,10,size,size, fill = color)
self.xspeed = random.randrange(-20,20)
self.yspeed = random.randrange(-20,20)
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
pos = canvas.coords(self.shape)
if pos[3] >= HEIGHT or pos[1] <= 0:
self.yspeed = -self.yspeed
elif pos[2] >= WIDTH or pos[0] <= 0:
self.xspeed = -self.xspeed
colorList = ['blue', 'green', 'purple', 'red', 'orange', 'pink','cyan','dodgerblue',
'grey', 'gold', 'turquoise', 'magenta', 'yellow' ]
ballList = []
for x in range(200):
ballList.append(Ball(random.choice(colorList),random.randrange(20,50)))
while True:
#move each ball in list
for ball in ballList:
ball.move()
#update the balls
tk.update()
#slow down 0.01 secods pause after each frame
time.sleep(0.03)
tk.mainloop()