【问题标题】:Point a Python Turtle towards certain coordinates将 Python Turtle 指向特定坐标
【发布时间】:2018-05-12 12:03:11
【问题描述】:

有什么办法可以让海龟指向某个坐标

对此的任何帮助表示赞赏。

【问题讨论】:

  • turtle.setheading(math.degrees(math.atan2(target_pos - current_pos)))

标签: python python-3.x turtle-graphics


【解决方案1】:

您正在寻找的是turtle.towards() 方法,它返回从乌龟位置到目标的角度。可以和turtle.setheading()方法结合使用:

turtle.setheading(turtle.towards(x, y))

turtle.towards() 方法在参数方面很灵活。它可以采用单独的 xy 值、组合的 (x, y) 元组,或者它所针对的位置的另一个海龟。

这是一种经常被忽视的方法,人们重新实现了它,以及turtle.distance()

【讨论】:

    【解决方案2】:

    正如我在 cmets 中所建议的,您可以使用一些三角函数来前往特定点:

    target = (100,50)
    d = math.degrees(math.atan2(*(target - turtle.pos())))
    turtle.setheading(d)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      相关资源
      最近更新 更多