Skip to Content
Course content

86: Turtle Colors, Speed, and Pen Control

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

Imagine you're standing in front of a massive canvas with a paintbrush in your hand. You wouldn't just start painting in one color at one speed, dragging the brush across every inch of the canvas to get from the top left corner to the bottom right. You'd switch your paint to a deep blue for the sky, move your hand quickly to sketch out the horizon, and—most importantly—you'd lift your brush off the canvas when you wanted to move to a different section without leaving a messy streak of paint behind you.

In Python's Turtle module, we do the exact same thing. The "Turtle" isn't just a pointer; it's your brush. Controlling the color, the speed, and whether the brush is actually touching the paper is what turns a basic line drawing into actual art.

Switching Your Palette

Up until now, you've probably been seeing that default black line. To change that, we use color(). Now, you can use basic strings like "red" or "blue", but as a software engineer, I'll let you in on a secret: if you want your work to look professional, use Hex codes. These are the six-digit codes (like "#FF5733") you see in web design. It gives you millions of options instead of just the basic rainbow.

import turtle
t = turtle.Turtle()

t.color("forestgreen") # A standard named color
t.forward(100)

t.color("#8A2BE2") # A specific BlueViolet hex code
t.forward(100)

You can also separate the outline color from the fill color using pencolor() and fillcolor(). I find this useful when I want a bold black border but a soft yellow interior for a shape.

Adjusting the Pace

I've spent way too many hours of my life watching a turtle slowly crawl across the screen while I waited for my code to finish. You can fix this with speed(). This method takes an integer from 0 to 10. Now, here is the weird part: 1 is the slowest, 10 is fast, but 0 is actually the absolute fastest because it turns off the animation entirely.

t.speed(1) # Great for debugging so you can see exactly where it turns
t.speed(0) # Use this when you've perfected your logic and just want the result

The Art of Lifting the Brush

This is the most critical part of pen control. If you want to draw two separate circles on a page, you can't just goto() the second position, or you'll leave a "connecting" line between them. You have to tell the turtle to stop drawing.

We use penup() to lift the brush and pendown() to put it back down. Think of it as the "invisible move."

# Draw a square
t.pendown()
for _ in range(4):
    t.forward(50)
    t.left(90)

# Move to a new spot without leaving a trail
t.penup()
t.goto(-100, -100)
t.pendown()

# Draw another square in a different color
t.color("orange")
for _ in range(4):
    t.forward(50)
    t.left(90)

If you forget pendown() after a move, you'll be wondering why your turtle is moving but nothing is appearing on the screen. It happens to the best of us.




📋 Practical Task

Project: The Neon Constellation

Your task is to create a "star map" using the Turtle module. You need to draw three distinct stars (they can be simple small squares or crosses) in three different locations on the screen.

  • Requirement 1: Each star must be a different color. At least one must use a Hex code (e.g., "#00FFFF").
  • Requirement 2: The turtle must move between the stars without leaving any lines connecting them.
  • Requirement 3: Set the speed to 0 so the constellation appears instantly.
  • Requirement 4: Use penup() and pendown() correctly to navigate the canvas.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.