Skip to Content
Course content

85: Drawing Shapes with Turtle

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Turtle graphics is one of those things that feels like a toy, but it's actually a fantastic way to visualize how loops and geometry interact in your code. Instead of staring at text in a console, you get to see your logic literally draw itself on the screen. Today, we're going to build a colorful, expanding geometric spiral.

Getting our canvas ready

First, we need to bring in the module and set up our "turtle"—which is really just a cursor that moves based on X and Y coordinates. I like to name my turtle t just to keep the code concise.

import turtle

t = turtle.Turtle()
t.speed(0) # This makes the turtle move as fast as possible
screen = turtle.Screen()
screen.bgcolor("black")
t.color("cyan")

The struggle with triangle angles

I want this spiral to be based on a triangle. My first instinct is usually to think, "A triangle has 60-degree angles, so I'll just turn 60 degrees." Let's see what happens when I try that in a loop.

# My first attempt (The Mistake)
for i in range(3):
    t.forward(100)
    t.right(60)

If you run that, you'll notice it doesn't actually make a triangle; it just makes a weird open zig-zag. I forgot a fundamental rule of Turtle: the turtle turns based on the exterior angle, not the interior one. To get a 60-degree interior angle, I actually need to turn 120 degrees (180 - 60 = 120). It's a common trip-up, even for experienced devs when they come back to Turtle after a few years.

Here is the corrected logic for a proper triangle:

for i in range(3):
    t.forward(100)
    t.right(120)

Turning a shape into a spiral

A simple triangle is boring. To make it a spiral, we need to do two things: increase the distance the turtle moves every time it turns, and run the loop many more times than just three. I'll use a variable for the distance and increment it inside the loop.

distance = 5
for i in range(100):
    t.forward(distance)
    t.right(120)
    distance += 3 # Every side gets slightly longer

Now we have a growing triangular spiral. It looks clean, but it's a bit monochromatic. Let's spice it up.

Adding a splash of color

To make this look professional, I'll define a list of colors and use the modulo operator (%) to cycle through them. This ensures that no matter how many times the loop runs, we never go out of bounds of our color list.

colors = ["red", "purple", "blue", "green", "orange", "yellow"]
distance = 5

for i in range(150):
    t.color(colors[i % 6]) # Cycle through the 6 colors
    t.forward(distance)
    t.right(120)
    distance += 3

turtle.done()

By changing the angle from 120 to something like 121 or 119, you'll notice the whole shape starts to rotate slightly, creating a much more complex "vortex" effect. That's the beauty of this—small tweaks to the math create massive changes in the art.




📋 Practical Task

Exercise: Building a Nested Square Mandala

Your task is to create a geometric mandala using a square as the base shape. Modify the spiral logic from the lesson to achieve the following:

  • Use a square as the base (think about what the turn angle should be).
  • Create a list of at least four different colors and cycle through them.
  • Set the loop to run 200 times.
  • Slightly offset the angle (e.g., use 91 degrees instead of 90) to create a rotating, nested effect.
  • Ensure the background color is set to "black" and the turtle speed is set to 0.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.