dark mode light mode Search Menu
Search

Who Knew Turtles Can Draw?

Nicholas Labyrinth X on Flickr

KONICA MINOLTA DIGITAL CAMERA

If you’ve ever drawn a star before — or even doodled one in your notebook — you probably realized that it’s harder than it looks. My stars always look titled and squishy. Inevitably, one of the star’s points is way too long, and another point looks like a sad little stump. Well, luckily for us technical folk, there’s an easier way to draw perfect stars: using Python and Turtle.

Pythons and Turtles

Python is a simple, flexible programming language that allows you to focus on logic instead of worrying about brackets and semi-colons. Turtle is a Python module — sort of like an extension to the main language — that makes it easy to draw things.

There are two main components in a Turtle program. There’s the ‘screen’, or the canvas on which you draw. Then there’s the turtle, who wanders around the screen with an imaginary pen, drawing the shapes we request. Of course, this is virtual turtle! Often he (or she) is shaped liked a little triangle, but we can change this if we like.

Instructions

Open up your favourite web browser and navigate to www.repl.it then type ‘Python’ in the box, but make sure you select ‘Python (with Turtle)’. Otherwise, the code won’t work!

Paste the following code in the left-hand editor:

from turtle import *
thomas = Turtle()
thomas.color("red")
for i in range(5):
    thomas.forward(50)
    thomas.right(144)

Hit the Run button! Your ‘turtle’ should draw a star on the right-hand screen. Try the embedded script here, by clicking the Run button, or scroll down to visit the repl.it site with this code.

Breakdown the Code

from turtle import *

Since Turtle is a module, it’s not automatically included in our program. This line of code fetches the module from its ‘shelf’ and brings it into the work area.

thomas = Turtle()

The second part of this line — Turtle() — is creating a turtle object. An object is simply a virtual data structure with special traits and abilities; in this case, the ability to move, draw, and change shape. Altogether, this line of code creates a specific turtle and assigns him to the variable ‘thomas’. You can imagine this as the virtual equivalent of going to a pet store, buying a turtle, and naming him Thomas.

thomas.color(“red”)

By default, Thomas draws with a black pen. This code changes the colour to red, but you could choose blue, green, pink, orange. Just change the word ‘red’ to the colour of your choice — and make sure to keep the quotation marks, or you’ll get an error!

for i in range(5):

This line of code creates a ‘for loop.’ They’re very useful in programs that have lots of repeated steps. Instead of telling the program to ‘draw one side of the star, then the second side, then the third side, etc.’ (which is long and boring to write) you can use a for loop to say ‘draw a side, then do it five times.’ ’range(5)’ controls how often the code inside the loop is repeated — in this case, 5 times. By that logic, what will happen if you change the ‘5’ to a ‘4’? Or increase it to ’10’? Try it out!

thomas.forward(50)

Simple: we’re telling our turtle, Thomas, to move forward ’50’ pixels. You can put any number larger than 0 between those two brackets. The bigger the number, the bigger the star that Thomas will draw. Make sure you start this line with a tab!

thomas.right(144)

In preparation for drawing the next side of the star, Thomas has to turn a bit (otherwise he’ll only move in a straight line). In this case, we tell Thomas to turn to the right by 144 degrees. A full circle is 360 degrees and a half circle is 180 degrees. Therefore, 144 degrees is not quite a half circle; you almost turn to face the opposite direction, but you stop before you’re all the way there. What do you think happens if you turned more than 360 degrees?

‘144’ is the magic number for creating a perfect star; otherwise, Thomas will turn too much or too little, and miss the point where the lines should meet. This isn’t necessarily a bad thing. You can create lots of neat spiral art by changing the angle, they just won’t be stars. I recommend trying out 100 and 165 degrees. You might also want to increase the number of repetitions, i.e. changing ‘range(5)’ to something like ‘range(20)’.

The Spiral Star

To finish off the activity, we’re going to draw something extra cool: the spiral star. There are two small changes we must make to the code:

  • Change ‘range(5)’ to ‘range(20)’ (if you haven’t already)
  • Change ‘thomas.forward(50)’ to ‘thomas.forward(i * 10)’

Now hit the ‘Run’ button and see what happens!

We know that loops are good when we’re repeating an action over and over again. But what if it isn’t quite the same action? Something similar, but not identical. In every for loop, there’s one value that changes at each run through – the variable ‘i’. On the first iteration, i=0. Next, i =1, then i=2 — all the way up until i=19 (the loop stops right before it gets to 20). And since the value of ‘i’ is always changing, we can use it to make Thomas draw lines of different sizes. At first, when i = 0, the line is length 0. Next, when i = 1, the line is of length 10. Then 20, 30… all the way to 190! Cool, right? Like in the previous example, try playing around with the key numbers in the code — 20, 10, and 144 — and see what happens. Have fun!

LINKS

Activities with Python and Turtle:

http://interactivepython.org/runestone/static/thinkcspy/PythonTurtle/OurFirstTurtleProgram.html

http://openbookproject.net/thinkcs/python/english3e/hello_little_turtles.html

https://michael0x2a.com/blog/turtle-examples

Learn More

A Simple Turtle Tutorial for Python

https://github.com/asweigart/simple-turtle-tutorial-for-python/blob/master/simple_turtle_tutorial.md

Easy Designs – Turtle Graphics Python

http://www.instructables.com/id/Easy-Designs-Turtle-Graphics-Python/

Notes on Using Python’s Turtle Built-In Commands

http://www.eg.bucknell.edu/~hyde/Python3/TurtleDirections.html