Point a Python Turtle towards certain coordinates

筅森魡賤 提交于 2020-12-26 09:23:05

问题


Is there any way to point the Turtle towards certain coordinates

Any help with this with be appreciated.


回答1:


What you're looking for is the turtle.towards() method, which returns the angle from the turtle's position to a target. It can be used in combination with the turtle.setheading() method:

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

The turtle.towards() method is flexible with respect to it arguments. It can take separate x and y values, a combined (x, y) tuple, or another turtle whose position it will target.

It's a commonly overlooked method, that folks reimplement, along with turtle.distance().




回答2:


As I suggested in the comments, you can use a bit of trigonometry to head to a specific point:

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


来源:https://stackoverflow.com/questions/50306024/point-a-python-turtle-towards-certain-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!