How to Use Python Turtle to Draw Graphics and Animations

Python Turtle is a module that allows you to create graphics and animations using a turtle-like object that moves on the screen. You can control the turtle’s position, direction, color, shape, and speed using simple commands. You can also use loops, functions, and variables to make your turtle draw complex shapes and patterns.

In this blog, you will learn about the basics of Python Turtle, the common commands and methods of the turtle object, and how to use them to draw some examples of graphics and animations.

The Basics of Python Turtle

Python Turtle is a built-in module that you can import into your Python program using the import turtle statement. This will create a turtle object that you can access using the turtle name. You can also create multiple turtle objects using the turtle.Turtle() constructor and assign them to different variables.

To use Python Turtle, you need to have a screen where the turtle can move and draw. You can create a screen using the turtle.Screen() method and assign it to a variable. You can also customize the screen’s size, title, background color, etc. using various methods and attributes.

To make the turtle move and draw on the screen, you need to use commands that tell the turtle what to do. For example, you can use the turtle.forward() command to make the turtle move forward by a certain distance, or the turtle.left() command to make the turtle turn left by a certain angle. You can also use the turtle.penup() and turtle.pendown() commands to lift or lower the turtle’s pen, which determines whether the turtle leaves a trail behind or not.

To change the appearance of the turtle and its trail, you need to use methods that modify the turtle’s attributes. For example, you can use the turtle.color() method to change the turtle’s pen color or fill color, or the turtle.shape() method to
change the turtle’s shape from a classic arrow to a circle, square, triangle, etc. You can also use the turtle.speed() method to change the turtle’s speed from 0 (fastest) to 10 (slowest).

To clear the screen or reset the turtle’s position and direction, you need to use methods that affect the screen or the turtle’s state. For example, you can use the turtle.clear() method to erase all the drawings on the screen without moving the turtle, or the turtle.reset() method to erase all the drawings and restore the turtle to its initial state.

To exit the turtle graphics window, you need to use the turtle.bye() method or click on the window’s close button.

Here is an example of a simple Python Turtle program that draws a square:

# Import the turtle module
import turtle
# Create a screen
screen = turtle.Screen()
screen.title("Python Turtle Example")
screen.bgcolor("lightblue")
# Create a turtle
t = turtle.Turtle()
t.color("red")
t.shape("turtle")
t.speed(5)
# Draw a square
for i in range(4):
 t.forward(100)
 t.left(90)
# Exit the turtle graphics window
turtle. Bye()

The Common Commands and Methods of the Turtle Object

  • Movement commands: These commands make the turtle move on the screen by changing its position or direction
    • turtle.forward(distance): Moves the turtle forward by a given distance.
    • turtle.backward(distance): Moves the turtle backward by a given
      distance.
    • turtle.right(angle): Turns the turtle right by a given angle in degrees.
    • turtle.left(angle): Turns the turtle left by a given angle in degrees.
    • turtle.goto(x,y): Moves the turtle to a given coordinate (x,y) on the screen.
    • turtle.setx(x): Moves the turtle to a given x coordinate on the screen.
    • turtle.sety(y): Moves the turtle to a given y coordinate on the screen.
    • turtle.setheading(angle): Sets the turtle’s heading (direction) to a given angle in degrees.
    • turtle.home(): Moves the turtle to its original position (0,0) and sets its heading to 0 degrees (east).
  • Pen control commands: These commands control whether the turtle leaves a trail behind or not when it moves
    • turtle.pendown(): Lowers the turtle’s pen, so that it draws a line when it moves.
    • turtle.penup(): Lifts the turtle’s pen, so that it does not draw a line when it moves.
    • turtle.isdown(): Returns True if the turtle’s pen is down, False otherwise.
  • Pen attribute methods: These methods change the appearance of the turtle’s pen, such as its color, width, or style.
    • turtle.color(color): Sets the turtle’s pen color to a given color. The color can be a string (such as “red” or “green”) or a tuple of three numbers (such as (0,0,0) for black or (255,255,255) for white).
    • turtle.pencolor(color): Sets the turtle’s pen color to a given color. This is equivalent to turtle.color(color).
    • turtle.fillcolor(color): Sets the turtle’s fill color to a given color. This is the color that the turtle uses to fill shapes when it starts and ends a fill operation using the turtle.begin_fill() and turtle.end_fill() methods.
    • turtle.pensize(width): Sets the turtle’s pen width to a given width in pixels. The default width is 1 pixel.
    • turtle.pen(pen): Sets the turtle’s pen attributes to a given dictionary of values. The dictionary can have keys such as “color”, “pencolor”, “fillcolor”, “pensize”, “speed”, etc. and corresponding values. For example, turtle.pen({“color”:”blue”, “pensize”:3}) will set the turtle’s pen color to blue and its pen size to 3 pixels.
  • Turtle attribute methods: These methods change the appearance of the turtle itself, such as its shape, size, or visibility.
    • turtle.shape(shape): Sets the turtle’s shape to a given shape. The shape can be one of the following strings: “arrow”, “turtle”, “circle”, “square”, “triangle”, or “classic”. The default shape is “classic”.
    • turtle.shapesize(stretch_wid, stretch_len, outline): Sets the turtle’s shape size to a given width, length, and outline. The width and length are relative to the original size of the shape, and the outline is the width of the shape’s border in pixels. The default values are 1, 1, and 1 respectively.
    • turtle.resizemode(rmode): Sets the turtle’s resize mode to a given mode. The mode can be one of the following strings: “auto”, “user”, or “noresize”. The default mode is “noresize”. In “auto” mode, the turtle will automatically resize according to its pen size. In “user” mode, the turtle will resize according to its shapesize. In “noresize” mode, the turtle will not resize at all.
    • turtle.turtlesize(stretch_wid, stretch_len, outline): Sets the turtle’s shape size to a given width, length, and outline. This is equivalent to turtle.shapesize(stretch_wid, stretch_len, outline).
    • turtle.showturtle(): Makes the turtle visible on the screen. This is equivalent to turtle.st().
    • turtle.hideturtle(): Makes the turtle invisible on the screen. This is equivalent to turtle.ht().
    • turtle.isvisible(): Returns True if the turtle is visible on the screen, False otherwise.
  • Drawing state methods: These methods affect the drawing state of the turtle, such as its position, direction, or speed.
    • turtle.position(): Returns the current position of the turtle as a tuple of two numbers (x,y). This is equivalent to turtle.pos().
    • turtle.xcor(): Returns the current x coordinate of the turtle as a number.
    • turtle.ycor(): Returns the current y coordinate of the turtle as a number.
    • turtle.heading(): Returns the current heading of the turtle in degrees as a number.
    • turtle.towards(x,y): Returns the angle in degrees from the turtle’s position to a given point (x,y) on the screen as a number.
    • turtle.distance(x,y): Returns the distance in pixels from the turtle’s position to a given point (x,y) on the screen as a number.
    • turtle.speed(speed): Sets or returns the turtle’s speed as a number from 0 (fastest) to 10 (slowest). If no argument is given, it returns the current speed. If an argument is given, it sets the speed to that value. The default speed is 3.
  • Drawing methods: These methods make the turtle draw shapes or patterns on the screen using its pen and fill color.
    • turtle.dot(size, color): Draws a dot of a given size and color at the turtle’s position. The size is in pixels and the color can be a string or a tuple of three numbers. If no arguments are given, the dot will have the same size and color as the turtle’s pen.
    • turtle.stamp(): Stamps a copy of the turtle’s shape onto the screen at the turtle’s position. The stamp will have the same color and size as the turtle. The method returns a stamp id that can be used to delete the stamp later using
      the turtle.clearstamp() or turtle.clearstamps() methods.
    • turtle.circle(radius, extent, steps): Draws a circle or an arc of a circle with a given radius around the turtle’s position. The radius is in pixels and can be positive or negative. If positive, the circle will be drawn counterclockwise. If negative, the circle will be drawn clockwise. The extent is an optional argument that specifies the angle of the arc in degrees. If not given, a full circle will be drawn. The steps is an optional argument that specifies the number of steps to draw the circle or arc. If not given, it will be calculated automatically based on the radius and extent. The turtle’s position and heading will not change after drawing the circle or arc.
    • turtle.begin_fill(): Starts filling the shape that the turtle will draw until the turtle.end_fill() method is called. The fill color will be the same as the turtle’s fill color.
    • turtle.end_fill(): Fills the shape that the turtle has drawn since the last call to turtle.begin_fill() with the turtle’s fill color. The turtle’s position and heading will not change after filling the shape.
    • turtle.write(arg, move, align, font): Writes a given text or value at the turtle’s position using the turtle’s pen color. The arg can be any type of object that can be converted to a string, such as a number, a list, or a function. The move is an optional boolean argument that specifies whether to move the turtle after writing or not. If True, the
      turtle will move to the end of the written text. If False, the turtle will stay at its position. The default value is False. The align is an optional string argument that specifies how to align the text relative to the turtle’s position. It can be one of “left”, “center”, or “right”. The default value is “left”. The font is an optional tuple of three values that specifies the font family, size, and style of the text. For example, (“Arial”, 12, “bold”) will use Arial font with size 12 and bold style.

How to Use Python Turtle to Draw Some Examples of Graphics and Animations

Let’s see some examples of graphics and animations that you can create with it.

Example : Drawing a Star

To draw a star with Python Turtle, you can use a loop to repeat five times the following steps:

  • Move forward by a certain distance
  • Turn right by 144 degrees

Here is an example code that draws a star:

# Import the turtle module
import turtle
# Create a screen
screen = turtle.Screen()
screen.title("Python Turtle Example")
screen.bgcolor("lightblue")
# Create a turtle
t = turtle.Turtle()
t.color("yellow")
t.shape("turtle")
t.speed(5)
# Draw a star
for i in range(5):
 t.forward(100)
 t.right(144)
# Exit the turtle graphics window
turtle. Bye()

This is how you can draw a star with Python Turtle.

Links of more graphic example:

Leave a Reply