How To Change Python Turtle Direction

How To Change Python Turtle Direction

We saw how to make the turtle move by using the forward() function. If you do not specify otherwise, the turtle starts its journey by pointing to the right, and the turtle can only move in the direction that it is facing. If you want the forward() function to move the turtle in a different direction, then you first have to specify the direction the turtle should be facing. In this tutorial, we’ll see how to aim the turtle in any direction we like before making the turtle move.


right() and left() functions

To change the direction the turtle is facing, you can use either the right() or left() function. These functions only work when you pass in a number value that specifies the number of degrees to turn. Let’s see a few examples of how to move the turtle up, down, left, and right using the right() and left() functions.


up using right()

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('circle')
right(270)
forward(75)

done()

python turtle right up


down using right()

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('circle')
right(90)
forward(75)

done()

python turtle down right


left using right()

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('circle')
right(180)
forward(75)

done()

python turtle left right


Putting It All Together

By making multiple calls to the right() and forward() functions, we can draw a shape. The code below produces a square.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('circle')
right(90)
forward(150)
right(90)
forward(150)
right(90)
forward(150)
right(90)
forward(150)

done()

Python_Turtle_Graphics_square

We can draw the same shape using the left() function, but the placement of the square will be different.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('circle')
left(90)
forward(150)
left(90)
forward(150)
left(90)
forward(150)
left(90)
forward(150)

done()

Python_Turtle_Graphics_left function

Todo: Experiment with using different number values passed in to both the right(), left(), and forward() functions to see what kinds of shapes you can create.