You are currently viewing Learn to Code with Turtle: A Super Fun Adventure!
Learn to code using Turtle with Python

Learn to Code with Turtle: A Super Fun Adventure!

Welcome, Young Coders!

Ready to become a coding superhero? With Python and Turtle, we will show you how draw cool shapes, create colorful patterns, and have a blast while you learn to code. Think of Turtle as your magical paintbrush that follows your instructions to make art on the screen. Let’s jump into this fun adventure and create awesome images with Python!

Using Turtle with Python

What is Turtle?

Turtle is a special tool in Python that lets you draw pictures by moving a little “turtle” around the screen. The turtle has a pen, and when it moves, it draws lines. You can tell it where to go, what colors to use, and even how fast to draw. It is like giving directions to a robot artist!

Getting Started: Setting Up Your Turtle Adventure

Before we start drawing, we need to get Python and Turtle ready on your computer. Ask a grown-up to help with this part!
  1. Install Python:
    • Go to python.org and download the latest version of Python.
    • Follow the instructions to install it. Make sure to check “Add Python to PATH” during setup.
  2. Open a Coding Program:
    • Use a program like IDLE (it comes with Python) or a kid-friendly editor like Thonny (download at thonny.org).
    • These programs are where you will type your Turtle commands.
  3. Test your Python install works correctly:
    • In Thonny there is a window with an untitled tab, type the following command there and then click the Run icon.
print("Hello World!")
    • At the bottom of the Thonny program is the Shell window, when you execute the above command Hello World! should be printed there. The above command is known as a print statement.

Well done, that is your very first program!

Your First Turtle Adventure: Drawing a Line

Let’s make the turtle draw a straight line. Think of the turtle as a tiny robot with a pen that you can control!
  • In Thonny click on the New icon to open a fresh window. Now enter the following commands into the untitled window like we did with our previous command (the print statement):
import turtle  # Wake up the turtle!
turtle.forward(100)  # Move forward 100 steps
turtle.done()  # Tell the turtle to rest

What happened?

A new window opened called Python Turtle Graphics, and the turtle (represented by an arrowhead similar to a triangle) moved forward and drew a line, and the turtle is now sat at the end of that line. The number 100 tells the turtle how far to go.

To close the turtle window, click the Stop icon in Thonny, closing a program in this way makes sure it is closed correctly. Now, try changing 100 to 50 or 200 and click on the Run icon again to see what happens!

Drawing a basic line
Superhero Coder

Fun Challenge:

Can you make the turtle draw a super long line?

Try turtle.forward(300)

Superhero Power: Comments!

Super Coders, see those # symbols in our Turtle code? Those are comments, like secret notes for you and your friends! They start with a # and tell you what the code does, but Python ignores them.
 
Example:
import turtle
# Draw a square turtle.forward(100) # Move forward turtle.right(90) # Turn right
Why Comments Rock:
  • They explain your code, so you do not forget.
  • Comments also help friends understand your superhero plan.
  • They make fixing mistakes easier.
Mission: Add a comment to your Turtle code, like # Make a star. Keep it short and fun!
Comments are your coding sidekick, keeping your Turtle missions clear. Now, code on, hero!

Turning the Turtle: Making Shapes

The turtle can turn left or right to draw shapes like squares or triangles. Let’s make it turn!
import turtle
turtle.forward(100)  # Move forward
turtle.left(90)  # Turn left 90 degrees (like a corner)
turtle.forward(100)  # Move forward again
turtle.done()
What is going on?
  • turtle.left(90) makes the turtle turn left by 90 degrees (like turning a corner).
  • Try turtle.right(90) to turn right instead.

Super Fun Shape: Draw a Square!

A square has 4 sides, and each corner is a 90-degree turn. Here is how to draw one using your turtle:

import turtle
turtle.forward(100)  # Side 1
turtle.left(90)
turtle.forward(100)  # Side 2
turtle.left(90)
turtle.forward(100)  # Side 3
turtle.left(90)
turtle.forward(100)  # Side 4
turtle.left(90)
turtle.done()
Wow, you drew a square!
We can use less code to get the same result by using a loop to repeat the steps.
Drawing a square

Loops: Making Drawing Easier

A loop is like telling the turtle, “Do this a bunch of times!” To draw a square, we can use a for loop instead of typing the same thing over and over.
import turtle
for i in range(4):  # Do this 4 times
    turtle.forward(100)  # Move forward
    turtle.left(90)  # Turn left
turtle.done()
Let’s Break Down This Loop?
  • “for” is the type of loop we are using.
  • “i” is increment, think of it as steps. The first time through the loop i = 0, every time the loop repeats i increases by 1.
  • “range(4)” is how many steps or times we want to loop.
    It’s like saying, “Draw a side and turn, then do it again 3 more times”, for a total of 4 times through the loop.
Superhero Coder

Fun Challenge:

  • Change range(4) to range(3). What shape do you get? 
  • Try range(5) or range(6). What shapes can you make?

Adding Colors: Make It Pop!

Let’s make our drawings colorful! The turtle can use different colors for its pen and even fill shapes with color.
import turtle
turtle.pencolor("blue")  # Set pen color to blue
turtle.forward(100)
turtle.left(90)
turtle.pencolor("red")  # Change to red
turtle.forward(100)
turtle.done()
Cool Colors to Try:
  • “green”, “purple”, “yellow”, “pink”, “orange”, or any color you can think of.
Filling Shapes with Color: To fill a shape, tell the turtle to start filling, draw the shape, then stop filling.
import turtle
turtle.fillcolor("yellow")  # Fill with yellow
turtle.begin_fill()  # Start filling
for i in range(4):  # Draw a square
    turtle.forward(100)
    turtle.left(90)
turtle.end_fill()  # Stop filling
turtle.done()
Wow, a yellow square!
Try different colors for pencolor and fillcolor. Maybe a red outline with a green fill?
Drawing a square with color filled
Superhero Coder

Fun Challenge:

Draw a blue triangle and fill it with purple!

Making It Fancy: Changing Pen Size and Speed

You can make the turtle’s lines thicker or thinner and control how fast it draws.
import turtle
turtle.pensize(5)  # Make the pen thicker (5 pixels)
turtle.speed(3)  # Set speed (1 is slow, 10 is fast)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.done()
What is cool?
  • turtle.pensize(10) makes super thick lines.
  • turtle.speed(1) makes the turtle draw slowly so you can watch it.
  • turtle.speed(10) makes it zoom!
Drawing with Pen size
Superhero Coder

Fun Challenge:

Draw a thick, slow square and a thin, fast triangle!

Drawing Circles and Stars

The turtle can draw circles and even make star shapes!
Draw a Circle:
import turtle
turtle.circle(50)  # Draw a circle with radius 50
turtle.done()

What is radius?

It’s how big the circle is. Try turtle.circle(100) for a bigger circle or turtle.circle(20) for a tiny one.

Draw a Star: A star has 5 or more points. We can use a loop and turn the turtle at special angles.
import turtle
turtle.pencolor("gold")  # Golden star
for i in range(5):  # 5 points
    turtle.forward(100)
    turtle.right(144)  # Special angle for stars
turtle.done()
Wow, a golden star!
Try changing the angle (144) to 120 or 150. What happens?

Moving Without Drawing: Jumping Around

Sometimes, you want the turtle to move without drawing a line (like jumping to a new spot). Use penup and pendown.
import turtle
turtle.forward(50)  # Draw a line
turtle.penup()  # Lift the pen (no drawing)
turtle.forward(50)  # Move without drawing
turtle.pendown()  # Put the pen down
turtle.forward(50)  # Draw again
turtle.done()
Superhero Coder

Fun Challenge:

Draw two squares side by side with a gap between them. Use penup to jump between squares!

Super Cool Project: Draw a Rainbow House

Let’s combine everything to draw a colorful house with a square base, a triangle roof, and a sun in the sky!
import turtle
# Draw the square base (red)
turtle.pencolor("red")
turtle.fillcolor("pink")
turtle.begin_fill()
for i in range(4):
    turtle.forward(100)
    turtle.left(90)
turtle.end_fill()

# Move to draw the triangle roof
turtle.penup()
turtle.goto(0, 100)  # Jump to the top
turtle.pendown()
turtle.pencolor("blue")
turtle.fillcolor("purple")
turtle.begin_fill()
for i in range(3):
    turtle.forward(100)
    turtle.left(120)  # 120 degrees for a triangle
turtle.end_fill()

# Draw a sun in the sky
turtle.penup()
turtle.goto(150, 150)  # Jump to the sky
turtle.pendown()
turtle.pencolor("yellow")
turtle.fillcolor("orange")
turtle.begin_fill()
turtle.circle(30)  # Draw a sun
turtle.end_fill()

turtle.done()
You made a rainbow house!
Try adding more details, like a door (a small square) or a tree (a circle on a line).

Saving Your Image

Super Coders, want to save your Turtle masterpiece? Use your coding powers to tell Turtle to save your drawing as a special file called .eps. Then, with a magical tool like Pillow, you can turn it into a picture (like a PNG) to share with the world! Follow these steps, and your superhero art will be ready to shine!

Call in Pillow’s Powers

  • Windows Heroes: Search for “Command Prompt” in the Start menu (type cmd and hit Enter).
  • Mac and Linux Heroes: Open “Terminal” (search for it using Spotlight or find it in Applications > Utilities).
  • Why? This is like your superhero headquarters where you send commands to Python!
Type this command and press Enter:
pip install Pillow

What’s Happening? pip is your trusty sidekick that grabs Pillow from the internet and installs it into Python. You’ll see some text zooming by as Pillow gets ready—cool, right?

Tip: If you get an error, try pip3 install Pillow instead, especially on a Mac. Ask a grown-up if you need backup!

import turtle
from PIL import Image

# Set up the turtle
t = turtle.Turtle()
t.speed(5)

# Add your code below! This code will draw a square
for _ in range(4):
    t.forward(100)
    t.right(90)

# Turtle code ends here

# Save as EPS (turtle format)
canvas = turtle.getscreen().getcanvas()
canvas.postscript(file="turtle_drawing.eps", colormode='color')

# Convert EPS to .PNG image
img = Image.open("turtle_drawing.eps")
img.save("turtle_drawing.png", "PNG")

# Keep the window open
turtle.done()

Superhero Tips

No Internet? Ask a grown-up to help download Pillow on another computer and transfer it to yours.

Stuck? If the command doesn’t work, make sure Python is installed and try again. You’ve got this, hero!

Why Pillow? It’s like giving Turtle a superpower to save drawings in formats everyone can see, not just secret .eps files.

With Pillow in your arsenal, you’re ready to save your Turtle creations like a true coding superhero! Keep drawing, keep coding, and let your imagination soar!

What You Have Learned

You are now a Turtle coding superhero! You’ve learned how to:
  • Move the turtle (forward, left, and right).
  • Draw shapes like squares, triangles, and circles.
  • Use comments to explain your code.
  • Use loops to repeat steps.
  • Add colors and fill shapes.
  • Change the pen size and drawing speed.
  • Jump around without drawing (penup, pendown).

Keep Exploring!

Here are some fun ideas to try:
  1. Draw a Flower: Use circles and loops to make petals.
  2. Make a Spiral: Draw a circle but make it bigger each time.
  3. Create a Pattern: Draw lots of shapes in different colors.
  4. Write Your Name: Use turtle.write(“Your Name”) to add text!

Conclusion

Super coders, you have levelled up your Python skills with Turtle! In this heroic guide, you have harnessed Turtle’s awesome abilities to draw epic shapes, create dazzling patterns, and unleash your creativity through code. These are only your first steps to becoming a coding superhero! Keep practicing your Turtle powers, experimenting with new designs, and exploring the Python universe. With every line of code, you are building the strength to conquer bigger challenges and create your own legendary projects. So, suit up, grab your keyboard, and let Turtle lead you to your next coding adventure, save the day and create something super cool! We hope you have had fun while you learn to code with Meganano!

Luke Barber

Hey there! I’m Luke, a tech enthusiast simplifying Arduino, Python, Linux, and Ethical Hacking for beginners. With creds like CompTIA A+, Sec+, and CEH, I’m here to share my coding and tinkering adventures. Join me on Meganano for easy guides and a fun dive into tech, no genius required!