我使用 python 龟模块做了一条线。代码如下:
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)
,它会继续绘制线。
我从用户那里得到线的坐标,那么我如何将海龟的方向与线对齐?
谢谢!
答案 0 :(得分:0)
您可以简单地将海龟的 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()