How to do animation using Zelle graphics module?

1.6k Views Asked by At

I need help to design my graphics, without turtle nor tkinter, but with Zelle graphics.py. The problem is that I need to run 4 circles moving at the same time. Here's the code I have so far:

from graphics import *
import time #import time module
from random import randrange

def rand_color():#generates a random color and returns that color
    return(color_rgb(randrange(256),randrange(256),randrange(256)))

def main():
    win = GraphWin("My Circle",500,500)
    c = Circle(Point(20,20),20)
    c.setFill(rand_color())
    c.draw(win)
    for i in range(1,461):
        c.move(1,1)
        time.sleep(.005)
        
    c = Circle(Point(20,20),20)
    c.setFill(rand_color())
    c.draw(win)
    for i in range(1,461):
        c.move(-1,1)
        time.sleep(.005)
        
    c = Circle(Point(20,20),20)
    c.setFill(rand_color())
    c.draw(win)
    for i in range(1,461):
        c.move(1,-1)
        time.sleep(.005)
        
    c = Circle(Point(20,20),20)
    c.setFill(rand_color())
    c.draw(win)
    for i in range(1,461):
        c.move(1,1)
        time.sleep(.005)
main()

I don't know how to move multiple objects at once. How would one go about this?

1

There are 1 best solutions below

0
On

Rather move each circle completely in turn, chop up the movements and alternate them so each circle moves a little at a time in round robin. I'm guessing this is close to what you're trying to do:

from random import randrange
from graphics import *

def rand_color():
    """ Generate a random color and return it. """

    return color_rgb(randrange(256), randrange(256), randrange(256))

win = GraphWin("My Circle", 500, 500)

circles = []

for x in [-1, 1]:
    for y in [-1, 1]:
        circle = Circle(Point(250, 250), 20)
        circle.setFill(rand_color())
        circle.draw(win)
        circles.append((circle, (x, y)))

for _ in range(250):
    for circle, (x, y) in circles:
        circle.move(x, y)

win.getMouse()  # Pause to view result
win.close()  # Close window when done

enter image description here