The turtle module in Python allows you to create images and shapes by drawing on a canvas. Turtles are often used to introduce beginners to basic programming concepts related to graphics.

You can draw many kinds of shapes using turtles, including lines and other patterns. You can do this by specifying the direction that the turtle moves in to create the shape, and by changing the color and pen size.

What Are Turtles in Python?

On the canvas, a turtle represents a point that you can move around, similar to how you would move a pen around on a piece of paper.

Turtle moving in different directions

When you spawn a turtle on a canvas, you can move the turtle left, right, up, or down. The turtle then draws a line in the direction it is moving in. You can draw different shapes by making the turtle move a certain way.

Creating graphics in a JES application is another way to draw shapes on a canvas, using a Python-based language.

How to Add Turtles to a Canvas

You will need to use the turtle module to create the turtle object and canvas.

  1. Create a new file called shapes.py, and open it using any Python IDE like PyCharm or VS Code.
  2. Inside the file, import the turtle module:
            import turtle
        
  3. Use the turtle module to create a new turtle object. When you create a new turtle object, a canvas is automatically created as well. The canvas will open when you run the program.
            my_turtle1 = turtle.Turtle()
        
  4. Set the shape of the turtle. This is the point or "pen tip" that you are using to draw the shapes with.
            my_turtle1.shape("turtle")
        
  5. Set the color of the line that you will draw the shapes in:
            my_turtle1.color("green")
        
  6. Use the exitonclick() function to keep the canvas open once created. This means that the canvas window will only close when you click on it:
            turtle.exitonclick()
        
  7. Open the command line and navigate to the folder where you stored your Python script. For example, if you stored it on the Desktop, your command would look similar to this:
            cd C:\Users\Sharl\Desktop
        
  8. Use the python command to run the file:
            python shapes.py
        
  9. Wait for the canvas to open. By default, the turtle is in the center of the canvas and facing to the right.
    Python turtle default location

How to Make the Turtle Move in Any Direction

Use the right(), left(), backward(), and forward() functions to move the turtle around the canvas. The direction the turtle moves in is relative to its current position. For example, if the turtle is facing in the "right" direction, then turning right again will make the turtle go down the canvas.

  1. After setting the color of the turtle, and before using the exitonclick() function, move the turtle forward. Since the turtle is facing "right" by default, moving it forward will draw a line towards the right of the screen. The number represents the distance moved in pixels:
            my_turtle1.forward(100)
        
    Python turtle moving right
  2. Instead of moving forward, you can make the turtle move backward. This will cause it to draw a line towards the left of the screen:
            my_turtle1.backward(100)
        
    Python turtle moving left
  3. You are only able to use either the forward() or backward() functions to move the turtle. If you want to move up, down, or diagonally, you will have to change the direction the turtle is facing. From the turtle's starting point, make it turn left by 90 degrees. Once the turtle is facing up, move it forward:
            my_turtle1.left(90)
    my_turtle1.forward(100)
    Python turtle moving up
  4. Alternatively, from the turtle's starting position, make it turn 90 degrees to its right. Move the turtle forward to draw a line going downwards:
            my_turtle1.right(90)
    my_turtle1.forward(100)
    Python turtle moving down
  5. Use a combination of different directions to draw your desired lines or shapes. You can also rotate the turtle any number of degrees to make it move diagonally:
            my_turtle1.forward(100)
    my_turtle1.left(90)
    my_turtle1.forward(100)
    my_turtle1.right(45)
    my_turtle1.forward(100)
    my_turtle1.left(135)
    my_turtle1.forward(300)
    my_turtle1.left(90)
    my_turtle1.forward(50)
  6. In the command line, use the python shapes.py command to re-open the canvas to view any lines and shapes created. You can view all of the above examples for the turtle's movement in a GitHub repo.
    Turtle moving in different directions

Learn Through Visual Programming

Now you understand how to create turtles in Python, and how to move them in different directions. Continue practicing by using turtles to create actual shapes, such as squares, triangles, or rectangles. You can also explore other Python modules to make the most out of Python.