0

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()
6
  • I think the method you used is ok. Commented Nov 19, 2013 at 7:14
  • 4
    So, what's the question here. Does your program not work? Do you need a code review? Commented Nov 19, 2013 at 7:14
  • @Chandranshu He want call the function ballList.update() at the same time. Commented Nov 19, 2013 at 7:17
  • Please format/indent your code properly. Commented Nov 19, 2013 at 7:18
  • @user3007613 Why do you use sleep(0.5), so you cann't run update function at the same time. Commented Nov 19, 2013 at 7:34

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

6 Comments

This looks awesome but when I try to run it I get an error saying "the Global name "moveAll" is not defined". Although it clearly is...
@user3007613 Maybe you declared the function inside class Ball??
@user3007613 Also, I'm assuming that ballList is a global variable (which actually is not really nice) but otherwise you could just pass it to moveAll as another argument.
ballList is not a global variable and I think that's my problem. Ha
It would be more "Pythonic" (as in shorter and faster) to write for ball in balllist:, ball.update().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.