python - 设置python海龟的朝向线的方向
问题描述
我使用 python turtle 模块做了一条线。这是代码:
import turtle
t = turtle.Turtle()
def line(x1, y1, x2, y2):
t.penup()
t.setpos(x1, y1)
t.pendown()
t.setpos(x2, y2)
以下是输出的样子:(使用line(0, 0, 100, 100)
)
海龟的航向是 0.0。我需要将它设置在画线的方向上,这样如果我这样做t.fd(50)
,它就会继续画线。
我从用户那里获得了线的坐标,那么如何将海龟的方向与线对齐?
谢谢!
解决方案
除了三角函数,你可以简单地使用turtle的towards()
方法setheading()
来指向目标,然后再移动到它:
from turtle import Screen, Turtle
def line(x1, y1, x2, y2):
turtle.penup()
turtle.setpos(x1, y1)
turtle.pendown()
turtle.setheading(turtle.towards(x2, y2))
turtle.setpos(x2, y2)
screen = Screen()
turtle = Turtle()
line(0, 0, 100, 100)
screen.exitonclick()
推荐阅读
- pyomo - 需要帮助:约束和结果(Pyomo)
- java - 如何将 launch4j 与 Java 自定义运行时映像一起使用?
- android - 如何查找特定 SSID 的网络 ID?
- c# - 如何从给定数字开始实现此字符串生成行为?
- c# - 正则表达式仅提取具有特定单词的 a href
- python - 如何解决 pyodbc.OperationalError: ('HYT00', u'[HYT00] [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0)
- full-text-search - Redisearch:提升查询短语匹配
- amazon-web-services - 隐藏通过对 EC2 上运行的服务的休息调用发送的密码的最佳做法是什么
- python - 如何以编程方式关闭 dumpmaster (mitmproxy)?
- html - 当我从 IE5 模拟到 IE7 或更高版本的 Web 应用程序时,为什么我的 offsetheight 和 offsetwidth 值会有所不同?