Who Knew Turtles Can Draw?
Image by Nicholas Labyrinth X on Flickr
Meet Thomas, a turtle who can help you draw stars with Python (not the snake!).
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
Also In The August 2017 Issue

A substitution cipher is an easy way to begin learning about how to use and make secret codes.

What are the odds two people in your classroom share a birthday? Much higher than you think!

Scratch is a fun block-based programming language that's easy to learn once you understand the basics.

The micro:bit is a not too expensive board that lets you easily build projects to learn about computing.

Meet Thomas, a turtle who can help you draw stars with Python (not the snake!).

The humble sewing machine can be a great first step to fun maker projects. Here's how to get started!

There's lots you can do make your online experiences enjoyable AND safe.

Minecraft is a fun game to play and a way to learn about games and programming. But first you have to learn the basics.

Have you ever put books in alphabetical order? What do you think the best method of alphabetizing would be?

With an EV3 robotics set, you can build all kinds of robots!

Some ideas how to engage young women in computing and STEAM based on recent research.

These three dimensional objects are 3D printed and cast images when light shines through them.

How do computers predict what text you want to write next? Here's how to create predictive stories.

This tutorial shows how to create a chat bot that plays hangman.

In this installment, learn about how programming languages are designed.

Links from the bottom of all the August 2017 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for February 2017.