我正在尝试使用Python乌龟绘制箭头。但是当到达箭头的头部时,我得到了乌龟的箭头,并加了45度以绘制箭头的一半,然后回到相同的位置以绘制另一部分。我设置了正确的角度,但随后一切都出错了:
StartPointX=0
StartPointY=0
MaxX=100
MaxY=100
Brush.goto(StartPointX,StartPointY)
Brush.goto(MaxX,MaxY)
Brush.left(45)
Brush.backward(20)
Brush.forward(20)
Brush.right(90)
Brush.backward(20)
输出图像:
答案 0 :(得分:0)
问题是您忽略了乌龟的标题。使用goto()
时,乌龟的标题不变。当您写left(45)
时,它相对于什么成45度角?当前标题的左侧,尚未设置:
from turtle import Screen, Turtle
StartPointX = 0
StartPointY = 0
MaxX = 100
MaxY = 100
screen = Screen()
brush = Turtle()
# Drawing The arrow line
brush.penup()
brush.goto(StartPointX, StartPointY)
brush.pendown()
brush.setheading(brush.towards(MaxX, MaxY))
brush.goto(MaxX, MaxY)
# Drawing arrow head
brush.left(45)
brush.backward(20)
brush.forward(20)
brush.right(90)
brush.backward(20)
brush.hideturtle()
screen.exitonclick()
另一种解决方法:
MaxX = 100
MaxY = 100
# ...
brush.setheading(brush.towards(MaxX, MaxY))
brush.goto(MaxX, MaxY)
用于控制标题,并使用forward()
代替goto()
:
Distance = 140
# ...
brush.setheading(45)
brush.forward(Distance)