简体   繁体   English

Python龟箭头没有面向正确的方向

[英]Python turtle arrowheads not facing in correct direction

I'm trying to draw an arrow using Python turtle. 我正在尝试使用Python龟绘制箭头。 But when it gets to the head of the arrow, I get the turtle heading and add 45 degrees to draw half of the arrow, then get back to the same position to draw the other part. 但是当它到达箭头时,我会看到乌龟朝向并加上45度来绘制箭头的一半,然后回到相同位置以绘制另一部分。 I set the correct angle but then everything goes wrong: 我设置了正确的角度,但一切都出错了:

Initialization 初始化

StartPointX=0
StartPointY=0
MaxX=100
MaxY=100

Drawing The arrow line 绘制箭头线

Brush.goto(StartPointX,StartPointY)
Brush.goto(MaxX,MaxY)

Drawing arrow head 绘制箭头

Brush.left(45)
Brush.backward(20)
Brush.forward(20)
Brush.right(90)
Brush.backward(20)

Output image: 输出图像:

输出图像在这里

The problem is you're ignoring the heading of the turtle. 问题是你忽略了乌龟的标题 When you use goto() the heading of the turtle is unchanged. 当你使用goto()时,乌龟的标题不变。 When you write left(45) , it's left 45 degrees relative to what? 当你left(45)left(45) ,它相对于什么左转45度? Left of the current heading, which hasn't been set: 当前标题的左侧,尚未设置:

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()

Another way to approach this: 另一种方法:

MaxX = 100
MaxY = 100

# ...

brush.setheading(brush.towards(MaxX, MaxY))
brush.goto(MaxX, MaxY)

Is to control the heading and use forward() instead of goto() : 是控制标题并使用forward()而不是goto()

Distance = 140

# ...

brush.setheading(45)
brush.forward(Distance)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM