How To Move The Python Turtle

How To Move The Python Turtle

In this tutorial, we’re going to see how to move the Turtle in a Turtle Python program. The turtle represents the invisible pen or marker, that is used to draw all of the lines, shapes, and colors on the canvas. In order to move the turtle around, you first need to make sure you have launched a blank canvas. See the last tutorial for the steps to take to create a turtle canvas if you have not already.


Turtle forward() Function

To make the turtle move in Python, we can use the forward() function. In the code snippet below, we have added a call to the forward() function while passing in an integer value of 75. This tells the turtle to move 75 steps beginning from the middle of the canvas. A step is equivalent to a pixel. By default, the turtle is actually an arrow shape, and it moves to the right unless you change turtle’s direction. The fd() function can be used as a shorthand if preferred.

from turtle import *

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

forward(75)

done()

python turtle forward


Changing The Turtle

We can change the pen from an arrow to something else. This is the turtle program, after all, so let’s make the pen an actual turtle. To do this, we can use the shape() function. The shape() function can change the default arrow into something else like a square, circle, or even a turtle! Let’s try a few different variations here.


turtle

from turtle import *

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

shape('turtle')
forward(75)

done()

python shape turtle


square

from turtle import *

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

shape('square')
forward(75)

done()

python turtle shape square


circle

from turtle import *

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

shape('circle')
forward(75)

done()

python turtle shape circle


Turtle backward() Function

The backward() function works just like the forward() function but in reverse. Picture yourself walking forward, or walking backward. It’s the same thing for your Python Turtle. There are two shorthand versions of backward() which are bk() and back().

from turtle import *

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

shape('circle')
backward(75)

done()

python turtle backward function


Four Quadrants

The Python Turtle screen is divided into four quadrants by an x and y axis, and the turtle always begins at 0,0 which is the exact center of the canvas.

python turtle screen quadrants

We can draw the four quadrants on a turtle canvas with this code. When calling the home() function, the turtle is moved back to 0,0 which is the center of the canvas.

from turtle import *

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

forward(250)
back(500)
home()
left(90)
forward(250)
back(500)
home()

done()

python turtle quadrants


Turtle goto() Function

Once you understand how the x and y coordinates exist on the canvas, you can use the goto() function to move to a specific spot on the screen. We just divided up the screen into four quadrants, and now the following program will move the turtle to the exact center of each quadrant one by one resulting in a square. Each corner of the square is located at exactly the center of quadrants 1, 2, 3, and 4. The setpos() and setposition() functions have the same effect as goto().

from turtle import *

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

forward(250)
back(500)
home()
left(90)
forward(250)
back(500)
home()

goto(125, 125)
goto(-125, 125)
goto(-125, -125)
goto(125, -125)
goto(125, 125)

done()

turtle center of each quadrant


Turtle setx() Function

Moves the turtle to the provided location on the x-axis.

from turtle import *

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

setx(100)

done()

turtle setx function


Turtle sety() Function

Moves the turtle to the provided location on the y-axis.

from turtle import *

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

sety(100)

done()

turtle sety function


Turtle speed() Function

To make the turtle draw faster or slower, you can use the speed() function. The default value is 3, and the possible values are 1 through 10 with 10 being the fastest. The speed is passed as an integer value. You can also use the strings of slowest, slow, normal, fast, and fastest to control speed.

The shape() function is of course part of the turtle module itself. The argument we pass to it is a python string that describes the shape of the turtle. Excellent work! We now know how to make the turtle move and also change the appearance of the turtle. We can use the forward() function to make the turtle move forward, or the backward() function to make the turtle move backward. These commands also have shortcuts of fd() for forward(), or bk() for backward().