Classes
from graphics import *
import random
import time
import math
class MovingBall():
def __init__(self, center, radius):
self.circle = Circle(center, radius)
self.circle.setFill('red')
angle = random.uniform(0, 2 * math.pi)
self.dx = 3 * math.cos(angle)
self.dy = 3 * math.sin(angle)
def draw(self, window):
self.circle.draw(window)
def step(self):
self.circle.move(self.dx, self.dy)
center = self.circle.getCenter()
if center.getX() < 0 or center.getX() > 200:
self.dx = -self.dx
if center.getY() < 0 or center.getY() > 200:
self.dy = -self.dy
def run():
win = GraphWin('Moving')
balls = []
while not win.isClosed():
for ball in balls:
ball.step()
click = win.checkMouse()
if click is not None:
ball = MovingBall(click, 5)
ball.draw(win)
balls.append(ball)
time.sleep(0.03)
run()