【发布时间】:2018-05-12 12:03:11
【问题描述】:
有什么办法可以让海龟指向某个坐标
对此的任何帮助表示赞赏。
【问题讨论】:
-
turtle.setheading(math.degrees(math.atan2(target_pos - current_pos)))
标签: python python-3.x turtle-graphics
有什么办法可以让海龟指向某个坐标
对此的任何帮助表示赞赏。
【问题讨论】:
标签: python python-3.x turtle-graphics
您正在寻找的是turtle.towards() 方法,它返回从乌龟位置到目标的角度。可以和turtle.setheading()方法结合使用:
turtle.setheading(turtle.towards(x, y))
turtle.towards() 方法在参数方面很灵活。它可以采用单独的 x 和 y 值、组合的 (x, y) 元组,或者它所针对的位置的另一个海龟。
这是一种经常被忽视的方法,人们重新实现了它,以及turtle.distance()。
【讨论】:
正如我在 cmets 中所建议的,您可以使用一些三角函数来前往特定点:
target = (100,50)
d = math.degrees(math.atan2(*(target - turtle.pos())))
turtle.setheading(d)
【讨论】: