我需要设计我的图形的帮助,而无需使用Turtle或Tkinter,但需要使用Zelle graphics.py。问题是我需要同时运行四个圆圈。这是我到目前为止的代码:
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()
我不知道如何一次移动多个对象。怎么会这样呢?
答案 0 :(得分:0)
宁可依次轮流完全移动每个圆圈,也可以将动作切碎并轮流交替,以便每个圆圈在轮循中一次移动一点。我猜这很接近您要执行的操作:
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