Reputation: 65
How can I tell a turtle to face a direction in turtle graphics? I would like the turtle to turn and face a direction no matter its original position, how can I achieve this?
Upvotes: 3
Views: 21785
Reputation: 19
If you're planning on moving your turtle to an (x, y) point somewhere and you want to point your turtle towards that point first, you can use:
turtle.setheading(turtle.towards(x, y))
Upvotes: 1
Reputation: 46
We can of course change the heading by turtle.setheading()
/ turtle.seth()
I'm referring to this documentation here.
Upvotes: 1
Reputation: 950
This is what I used for my game:
#Functions
def go_up():
head.direction="up"
def go_down():
head.direction="down"
def go_left():
head.direction="left"
def go_right():
head.direction="right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)
if head.direction == "down":
y = head.ycor()
head.sety(y - 20)
if head.direction == "left":
x = head.xcor()
head.setx(x - 20)
if head.direction == "right":
x = head.xcor()
head.setx(x + 20)
# Keyboard
win.listen()
win.onkeypress(go_up, "Up")
win.onkeypress(go_down, "Down")
win.onkeypress(go_left, "Left")
win.onkeypress(go_right, "Right")
Upvotes: 3
Reputation: 188
You can use:
turtle.right(angle)
or:
turtle.left(angle)
.
Hope this helps!
Upvotes: 0
Reputation: 59
The setheading command would be the way to do that.
turtle.setheading(<degrees/radians>)
is how you would use it. Without changing settings you will be in standard mode, so
turtle.setheading(0)
would face the turtle right,
turtle.setheading(90)
would face the turtle up,
turtle.setheading(180)
would face the turtle left,and
turtle.setheading(270)
would face the turtle down. Hope this helps!
Upvotes: 0
Reputation: 2918
I think turtle.setheading()
AKA seth()
is the function you're looking for, so if you want it to point North:
turtle.setheading(0)
or
turtle.setheading(90)
depending on whether you're in 'standard' or 'logo' mode.
As pointed out in the comments, you can find this info here.
Upvotes: 6