设置龟可以走的最大距离

问题描述 投票:0回答:1

我想设置海龟可以移动的最大距离。使用下面的代码,我希望第一个向前移动距离[[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))
python python-3.x turtle-graphics
1个回答
0
投票
您只需要比当前更多的基础架构。为了简化操作,我们需要使用乌龟列表中的各个元素,而不是每个乌龟的单个变量。然后,我们可以执行以下操作来测试乌龟是否已越过终点线:

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

© www.soinside.com 2019 - 2024. All rights reserved.