prithajnath
prithajnath

Reputation: 2115

Updating Objects Simultaneously

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()

Upvotes: 0

Views: 212

Answers (1)

JGallo
JGallo

Reputation: 181

Instead of running self.ball.move 1000 times INSIDE the function update; you could call the function 1000 times from outside. The problem is: every call to the function update runs the loop 1000 times; and you can't update other balls while it is running. My suggestion is writing an external function that loops over the balllist updating every ball. Then it sleeps(0.05) and does it again 1000 times:

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)

        #I put Point1 and Point2 here so that they will not reset to
        #37, 22 every time you call update()
        self.Point1 = 37
        self.Point2 = 22

    def update(self):

        #Also, if you never plan to change dx, dy, you should declare them
        #inside the __init__ method as self.dx and self.dy, because they are not
        #local variables of update()
        dx = 1 
        dy = 1
        x = self.ball.getCenter()
        y = x.getX()
        z = x.getY()
        if y>= 720:
            self.Point1 *= (-1 * dx)
        if y<= 0:
            self.Point1 *= (-1 * dx)
        if z>= 720:
            self.Point2 *= (-1 * dy)
        if z<= 0:
            self.Point2 *= (-1 * dy)
        self.ball.move(self.Point1, self.Point2)
        print(y,z)

def moveAll(n):
#This updates all the balls, then sleeps(0.05)
#and does it again n times
    for i in range(n):
        for ball in ballList:
            ball.update()
        sleep(0.05)


def main():
    win = GraphWin("Bouncy Many!", 720,720)
    for i in range(30):
        i = Ball(win)
        ballList.append(i)
    moveAll(1000)


main()

Upvotes: 1

Related Questions