我想设置海龟可以移动的最大距离。使用下面的代码,我希望第一个向前移动距离[[x的乌龟停止所有乌龟:
for i in range(130):
alex.forward(randint(5,10))
tess.forward(randint(5,10))
tim.forward(randint(5,10))
duck.forward(randint(5,10))
dog.forward(randint(5,10))
any(turtle.xcor() > 300 for turtle in turtles)
这是一个简单的示例实现:
from turtle import Screen, Turtle from random import randint COLORS = ["red", "green", "blue", "cyan", "magenta"] for index, color in enumerate(COLORS): turtle = Turtle('turtle') turtle.color(color) turtle.penup() turtle.setposition(-300, -60 + index * 30) screen = Screen() turtles = screen.turtles() while True: for turtle in turtles: turtle.forward(randint(5, 15)) if any(turtle.xcor() > 300 for turtle in turtles): break screen.mainloop()