When working with the turtle module in Python, to change the speed of a turtle, you can use the Python turtle speed() function.
import turtle
t = turtle.Turtle()
t.speed(5)
The turtle module in Python allows us to create graphics easily in our Python code.
When working with our turtle, sometimes it makes sense to want to change the speed of the turtle.
We can change the speed of our turtles with the turtle speed() function.
The turtle speed() function takes in an integer between 0 and 10, with 0 being instantaneous drawing, 1 being the slowest movement and 10 being the fastest movement.
Below are some examples of how to use the speed() function to change the speed of a turtle in Python.
import turtle
t = turtle.Turtle()
#Change turtle speed to 5, average speed drawing
t.speed(5)
#Change turtle speed to 0, instantaneous drawing
t.speed(0)
#Change turtle speed to 1, slowest speed drawing
t.speed(1)
#Change turtle speed to 10, fastest speed drawing.
t.speed(10)
Changing the Speed of a Turtle While Drawing
You can change the speed of a turtle while drawing a shape in Python easily. You can either speed up a turtle or slow down a turtle depending on what you would like.
In a loop, we just need to change the speed using the index of the loop.
Below is the Python code for creating a spiral with after each loop, we speed up the turtle, and the output of the spiral with the turtle module.
import turtle
t = turtle.Turtle()
def draw_spiral(starting_radius, speed_direction):
for i in range(1, 10):
if speed_direction == "up":
t.speed(i)
else:
t.speed(11-i)
t.circle(starting_radius + i, 60)
draw_spiral(10, "up")
Hopefully this article has been useful for you to learn how to use the speed() function to change the speed of a turtle in Python.