如何给 Python 中的乌龟添加位置条件?
how to add positional conditions to the turtle in Python?
我想添加这样一个条件,即如果比赛中的任何一只海龟越过终点线或指定位置,则循环自动在那里结束。这是一个竞赛程序,其中有 4 只海龟竞赛,轮流移动随机距离。所以我想要一个代码,它可以让我在其中一只乌龟到达最后一行时立即结束转弯循环。我尝试在 while 循环中使用它,但效果不佳;我希望有人可以修复我的代码。
这是我的代码:::
from turtle import *
from random import randint
from turtle import position
penup ()
speed (10)
goto (-140, 125)
for step in range (11) :
write (step, align = "center")
right (90)
forward (10)
pendown ()
forward (150)
penup ()
backward (160)
left (90)
forward (20)
aaa = Turtle()
aaa.color ("red")
aaa.shape ("turtle")
aaa.penup ()
aaa.goto (-150, 100)
aaa.pendown ()
bbb = Turtle()
bbb.color ("blue")
bbb.shape ("turtle")
bbb.penup ()
bbb.goto (-150, 60)
bbb.pendown ()
ccc = Turtle()
ccc.color ("yellow")
ccc.shape ("turtle")
ccc.penup ()
ccc.goto (-150, 20)
ccc.pendown ()
ddd = Turtle()
ddd.color ("green")
ddd.shape ("turtle")
ddd.penup ()
ddd.goto (-150, -20)
ddd.pendown ()
for turn in range (1) :
aaa.right(360)
bbb.left(360)
ccc.right(360)
ddd.left(360)
for turn in range (70) :
aaa.forward(randint(1, 5))
bbb.forward(randint(1, 5))
ccc.forward(randint(1, 5))
ddd.forward(randint(1, 5))
这个问题比乍一看更有趣,因为其中有一个相当微不足道的部分,还有一个更复杂的部分。
I would like a code that would allow me to end the turn loop as soon as one of the turtle hits the last line.
由于终点线是for step in range (11) :
循环中绘制的最后一条线,我们可以在绘制线时简单地存储海龟的x坐标(由.xcor()
返回)并获取最后一条线的循环后的位置:
for step in range(11):
…
finish = xcor() # remember where line has been drawn
forward(20)
其中一只海龟到达终点线意味着至少一只海龟的 x 坐标达到 finish
;在这种情况下,我们不需要假设最大圈数并且可以使用 while
而不是 for
循环:
while max(aaa.xcor(), bbb.xcor(), ccc.xcor(), ddd.xcor()) < finish:
aaa.forward(randint(1, 5))
bbb.forward(randint(1, 5))
ccc.forward(randint(1, 5))
ddd.forward(randint(1, 5))
但是现在,当我们看比赛结束的图片时,我们看到获胜的乌龟已经以身体的中心越过终点线(因为 body 中心被视为乌龟的位置),尽管乌龟应该在它的头撞到终点线时就赢了。因此,为了使其正确,我们必须调整 finish
的值以说明头部相对于中心的引导。为此,我们可以使用 .get_shapepoly()
其中 returns 当前形状多边形作为坐标对 的元组,例如。 g.
((0, 16), (-2, 14), (-1, 10), (-4, 7), (-7, 9), (-9, 8), (-6, 5), (-7, 1), (-5, -3), (-8, -6), (-6, -8), (-4, -5), (0, -7), (4, -5), (6, -8), (8, -6), (5, -3), (7, 1), (6, 5), (9, 8), (7, 9), (4, 7), (1, 10), (2, 14))
形状的中心位于 (0, 0),返回的多边形始终具有相同的方向,在海龟的情况下头部朝上,因此我们需要最大 y 坐标进行调整,以放置在 循环之前 :
# adjust the finish line position for the turtle's head rather than its center
finish -= max([y for x, y in aaa.get_shapepoly()]) # y as head points upwards
我想添加这样一个条件,即如果比赛中的任何一只海龟越过终点线或指定位置,则循环自动在那里结束。这是一个竞赛程序,其中有 4 只海龟竞赛,轮流移动随机距离。所以我想要一个代码,它可以让我在其中一只乌龟到达最后一行时立即结束转弯循环。我尝试在 while 循环中使用它,但效果不佳;我希望有人可以修复我的代码。
这是我的代码:::
from turtle import *
from random import randint
from turtle import position
penup ()
speed (10)
goto (-140, 125)
for step in range (11) :
write (step, align = "center")
right (90)
forward (10)
pendown ()
forward (150)
penup ()
backward (160)
left (90)
forward (20)
aaa = Turtle()
aaa.color ("red")
aaa.shape ("turtle")
aaa.penup ()
aaa.goto (-150, 100)
aaa.pendown ()
bbb = Turtle()
bbb.color ("blue")
bbb.shape ("turtle")
bbb.penup ()
bbb.goto (-150, 60)
bbb.pendown ()
ccc = Turtle()
ccc.color ("yellow")
ccc.shape ("turtle")
ccc.penup ()
ccc.goto (-150, 20)
ccc.pendown ()
ddd = Turtle()
ddd.color ("green")
ddd.shape ("turtle")
ddd.penup ()
ddd.goto (-150, -20)
ddd.pendown ()
for turn in range (1) :
aaa.right(360)
bbb.left(360)
ccc.right(360)
ddd.left(360)
for turn in range (70) :
aaa.forward(randint(1, 5))
bbb.forward(randint(1, 5))
ccc.forward(randint(1, 5))
ddd.forward(randint(1, 5))
这个问题比乍一看更有趣,因为其中有一个相当微不足道的部分,还有一个更复杂的部分。
I would like a code that would allow me to end the turn loop as soon as one of the turtle hits the last line.
由于终点线是for step in range (11) :
循环中绘制的最后一条线,我们可以在绘制线时简单地存储海龟的x坐标(由.xcor()
返回)并获取最后一条线的循环后的位置:
for step in range(11):
…
finish = xcor() # remember where line has been drawn
forward(20)
其中一只海龟到达终点线意味着至少一只海龟的 x 坐标达到 finish
;在这种情况下,我们不需要假设最大圈数并且可以使用 while
而不是 for
循环:
while max(aaa.xcor(), bbb.xcor(), ccc.xcor(), ddd.xcor()) < finish:
aaa.forward(randint(1, 5))
bbb.forward(randint(1, 5))
ccc.forward(randint(1, 5))
ddd.forward(randint(1, 5))
但是现在,当我们看比赛结束的图片时,我们看到获胜的乌龟已经以身体的中心越过终点线(因为 body 中心被视为乌龟的位置),尽管乌龟应该在它的头撞到终点线时就赢了。因此,为了使其正确,我们必须调整 finish
的值以说明头部相对于中心的引导。为此,我们可以使用 .get_shapepoly()
其中 returns 当前形状多边形作为坐标对 的元组,例如。 g.
((0, 16), (-2, 14), (-1, 10), (-4, 7), (-7, 9), (-9, 8), (-6, 5), (-7, 1), (-5, -3), (-8, -6), (-6, -8), (-4, -5), (0, -7), (4, -5), (6, -8), (8, -6), (5, -3), (7, 1), (6, 5), (9, 8), (7, 9), (4, 7), (1, 10), (2, 14))
形状的中心位于 (0, 0),返回的多边形始终具有相同的方向,在海龟的情况下头部朝上,因此我们需要最大 y 坐标进行调整,以放置在 循环之前 :
# adjust the finish line position for the turtle's head rather than its center
finish -= max([y for x, y in aaa.get_shapepoly()]) # y as head points upwards