I'm trying to draw an arrow using Python turtle. 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. I set the correct angle but then everything goes wrong:
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)
Output image:
The problem is you're ignoring the heading of the turtle. When you use goto()
the heading of the turtle is unchanged. When you write left(45)
, it's left 45 degrees relative to what? 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()
:
Distance = 140
# ...
brush.setheading(45)
brush.forward(Distance)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.