I have to create a program in python where I have to make thirty balls bounce around the screen using classes. I have created a class called "Ball" and I am trying to create a list of Ball and update all of my objects at once so that I can make all the balls move around at the same time.
from graphics import *
from random import *
from time import sleep
class Ball:
def __init__(self, win):
self.centerX, self.centerY = randrange(720), randrange(720)
radius = randrange(5, 25)
self.ball = Circle(Point(self.centerX, self.centerY), radius)
colour = (randint(0,255), randint(0,255), randint(0,255))
self.ball.setFill('cyan')
self.ball.draw(win)
def update(self):
dx = 1
dy = 1
Point1 = 37
Point2 = 22
for j in range(1000):
x = self.ball.getCenter()
y = x.getX()
z = x.getY()
if y>= 720:
Point1 *= (-1 * dx)
if y<= 0:
Point1 *= (-1 * dx)
if z>= 720:
Point2 *= (-1 * dy)
if z<= 0:
Point2 *= (-1 * dy)
self.ball.move(Point1, Point2)
print(y,z)
sleep(0.05)
def main():
win = GraphWin("Bouncy Many!", 720,720)
for i in range(30):
i = Ball(win)
ballList.append(i)
ballList.update()
main()
ballList.update()at the same time.sleep(0.5), so you cann't runupdatefunction at the same time.