The Basics of Turtle

by Elliott Hauser

12 Jan 2016

Remix your first Trinket

We’ll be using trinket.io for many of the exercises in this class. Trinket is an online Python interpreter designed for education. With it, you can run, remix, and share code from your browser.

First, click Run to see what the code does. Then, click Remix and create a Trinket account to save a copy of this Trinket to your account:

You’ll now be able to save your updates right here.

Here’s a chat box for dropping share links:

What can Turtles do?

Turtles were deisgned by programmers (all programs are, of course) and these programmers thought of some cool things they thought would be useful to include in their code. Later in the course we’ll learn how to add our own cool abilities to turtles. First, let’s see some of the things they can do.

All of the examples below assume you’ve already created a turtle named tina.

Shape

Turtles can be all sorts of shapes, the coolest of which is turtle shape.

tina.shape("turtle")

circle and arrow are two others. We’ll learn later how to make our own.

Forward, Backward, Left and Right

Turtles are great for learning because you can treat them like little robot animals. Tell them to go forward or backward a certain number of pixels.

tina.forward(100)
tina.backward(100)

Now, a square:

tina.forward(100)
tina.left(90)
tina.forward(100)
tina.left(90)
tina.forward(100)
tina.left(90)
tina.forward(100)

The number passed to left and right should be a number of degrees. See what happens if you pass in a number bigger than 360

Ah, the pen

Our Turtles are actually mutant (though not ninja) turtles. You may have noticed that they draw anywhere they move. We can control that behavior with .penup():

# No line:
tina.penup()
tina.forward(100)

# Yes line:
tina.pendown()
tina.backward(100)

Color!

Turtles can change into all kinds of awesome colors.

# Red!
tina.color("red")
tina.forward(100)

# No, Blue!
tina.color("blue")
tina.backward(100)

Stamp

Turtles can make a stamp of their shape that stays there even if they leave:

tina.forward(100)
tina.stamp()
tina.backward(100)

Circle

You guessed it:

tina.forward(100)
tina.circle(10)
tina.backward(100)

Fill

Turtles have a fill mode that will fill in a shape.

tina.fill(True)
tina.forward(100)
tina.left(90)
tina.forward(100)
tina.left(90)
tina.forward(100)
tina.left(90)
tina.forward(100)
tina.fill(False)

Goto

Know exactly where you want your turtle to go? Make here go there:

tina.goto(100,100)

See if you can figure out the extent of the coordinate system.

Setx

Set the x coordinate.

tina.setx(100)

Sety

Set the y coordinate.

tina.sety(100)

Hide

Peek-a-boo turtles.

tina.hideturtle()

Write

Turtles are literate.

tina.write("Heck Yeah!", None, "center", "16pt bold")

The list of arguments that write takes is an example of something that you’ll need documentation to really understand. Here are the docs

The Screen object

When you make a Turtle, there’s an invisible Screen object that the turtle moves on. You can explictly create this object and then do things like change its color.

Try out this code:

myscreen = turtle.Screen()

myscreen.bgcolor("blue")

There are several other cool things Screens can do but we won’t get to them til later. If you’re interested, look them up! You’ll be able to use them in your first homework.

Elliott Hauser is a PhD Student in information science at UNC Chapel Hill. He's hacking education as one of the cofounders of Trinket.io. Find Elliott Hauser on Twitter, Github, and on the web.