我如何让一个海龟对象与另一个对象面对相同的方向?
How would I make a turtle object face the same direction as another?
因此,就上下文而言,我正在使用 python 乌龟图形开发一款射击游戏。我试图找到可以多次尝试的东西,但无济于事。比如我试过
def direction():
wn.onkey(lambda: shuttle.setheading(90), 'Up')
wn.onkey(lambda: shuttle.setheading(180), 'Left')
wn.onkey(lambda: shuttle.setheading(0), 'Right')
wn.onkey(lambda: shuttle.setheading(270), 'Down')
direction()
bullet.setheading(direction())
和
if bullet.distance(shuttle) < 15:
28 - if shuttle.setheading == 90:
29 - bullet.setheading(90)
30 - if shuttle.setheading == 180:
31 - bullet.setheading(180)
32 - if shuttle.setheading == 0:
33 - bullet.setheading(0)
34 - if shuttle.setheading == 270:
35 - bullet.setheading(270)
对于行号,我深表歉意,但删除它们似乎太麻烦了。
假设您有 2 只乌龟:turtle1
和 turtle2
。要将 turtle2
转向 turtle1
的方向,请使用此代码:
turtle2.setheading(turtle1.heading())
仅供参考:在您的第一个代码片段中,direction
returns None
,因为它只添加事件,所以它不起作用。
注意:我建议在游戏中使用 tkinter,因为 turtle 是为教育用途而创建的,而且速度非常慢。
因此,就上下文而言,我正在使用 python 乌龟图形开发一款射击游戏。我试图找到可以多次尝试的东西,但无济于事。比如我试过
def direction():
wn.onkey(lambda: shuttle.setheading(90), 'Up')
wn.onkey(lambda: shuttle.setheading(180), 'Left')
wn.onkey(lambda: shuttle.setheading(0), 'Right')
wn.onkey(lambda: shuttle.setheading(270), 'Down')
direction()
bullet.setheading(direction())
和
if bullet.distance(shuttle) < 15:
28 - if shuttle.setheading == 90:
29 - bullet.setheading(90)
30 - if shuttle.setheading == 180:
31 - bullet.setheading(180)
32 - if shuttle.setheading == 0:
33 - bullet.setheading(0)
34 - if shuttle.setheading == 270:
35 - bullet.setheading(270)
对于行号,我深表歉意,但删除它们似乎太麻烦了。
假设您有 2 只乌龟:turtle1
和 turtle2
。要将 turtle2
转向 turtle1
的方向,请使用此代码:
turtle2.setheading(turtle1.heading())
仅供参考:在您的第一个代码片段中,direction
returns None
,因为它只添加事件,所以它不起作用。
注意:我建议在游戏中使用 tkinter,因为 turtle 是为教育用途而创建的,而且速度非常慢。