Final Project Guide: Turtle

by Elliott Hauser

17 Nov 2022

Turtle Methods

I’m going to offer you some unique takes on turtle methods, based on my almost decade long experience using it to teach. These are my takes, so you won’t see most of the categories I’m gong to use in the documentation. But I think they’ll help you anyways.

.forward, .backward, .left, and .right: Turtle-relative commands

Proprioception is your sense of your physical body. One of the reasons Turtle supports commands from the turtle’s perpective is to engage this intuitive sense to help you connect the result of your code to the commands you’re typing.

It is less efficient to program using these kinds of commands, but that’s OK! Much like typing something out instead of copy/pasting, sometimes the slow way is the best for giving your brain what it needs to learn.

It is totally OK to use these methods that key off of the Turtle’s current state in this class. If you feel that telling a turtle what to do from its perspective helps you get fluent with code, keep at it. What you lose in terms of efficiency in drawing a house or a tree you’ll make up for with a more intuitive sense of code.

.goto, .setheading, .sety. .setx etc.: Geometric commands

Turtle also lets you directly modify state, using these commands.

What’s good for what?

The turtle-relative methods like .forward are actually quite convenient if you need Turtle objects to behave like real living things. Since in a game or animation you won’t always know the state of a turtle, telling it to .forward(10) is much simpler than calculating what .goto arguments will produce the same effect.

Geometry-based methods are very efficient and precise. They’re great for when you need to draw complex shapes, or move a Turtle to a specific location. They’re also flexible to use within loops, since you can manipulate the x and y parameters you pass to .goto in a way that might be difficult to do relative to each turtle.

Think of it like this: With turtle-relative commands you modify the turtle’s state based upon its current state. With geometric commands, you’re setting that state. The effect of calling .forward(10) depends on the Turtle’s starting position and heading, whereas .goto(10,10) will always result in the turtle ending up at the position 10,10.

Turtle Design Philosophy: Flexibility of Use & Backwards Compatibility

You’ll see that a lot of turtle methods have synonyms. .seth and .setheading do the same thing, for instance. Some of that is an attempt to accomodate the history of Logo. seth was a logo command, and one of the ways you can use turtle is to from turtle import * and write code very similar to Logo and it’ll work great.

BUT Python is different, and has more space for long command names. It also values explicitness. So the .setheading method, called on a Turtle object, is the more Pythonic way of doing this. That said, supporting older ways of doing things is called backwards compatibility and is rather common in software. If you ever see something strange in a programming language, or support for a weird way of doing something, it might be an instance of backwards compatibility. This is similar to the reason why English has so many common irregular verbs (go => went, etc.). Programming languages and technologies of all kinds are inevitably shaped by historical inertia.

The historical details of things like this are interesting to nerds like me but I hope that a glimpse into why Turtle is the way it is, where there are often many ways to do things and many supported syntaxes, keeps that unanswered question from getting in your way.

Turtle’s flecibility of use, where for instance the .color method can take a color name OR a Red, Green, and Blue value, is great for learning but isn’t something you’ll see in most modules. This is because it’s much simpler to maintain code that expects one and only one kind of input. But we’re glad Turtle is like this, because it accomodates a range of learners, AND learning different patterns of using code.

API is a User Interface

If you think about it, the turtle module is software someone else wrote. The syntax we have to use to get turtles to do things is a user interface for us, the programmer-users of the software.

Since we can use Turtle to make basic user interfaces, this really helps illustrate how technologies are layered interfaces!

The portion of someone else’s source code you can interact with as a programmer is called the API, or Application Programming Interface. You’re probably familiar with Web-based APIs, which let us interact with web-based services. Python can both call and even provide web-based APIs, but in this class when we talk about an API we’re talking about things like the names of functions, their parameters and expected data types, classes and their methods, etc.

Using Images in Turtle

Turtle can use images as an input to Turtles’ .shape method (turning the turtle into that image), or as a background image via Screen’s .bgpic method.

For a turtle shape the image needs to 1) visible to Python and 2) “registered” to a Screen object.

For a background picture, it can be set directly with .bgpic as long as it’s visible to Python.

Get an image visible to Python on Trinket by clicking the image tab in the upper right of the code area and uploading the image. It will then be visibile to Python as its filename. This is the equivalent of being in the root folder of your project.

Elliott Hauser is an Assistant Professor at the UT Austin iSchool. He's hacking education as one of the cofounders of Trinket.io. Find Elliott Hauser on Twitter, Github, and on the web.