Having Fun with Turtles in Python.
Programming has its challenges, but like any other skill, it starts with the first step. Recently I was invited to speak at an after school program about what I do. I struggled to find a topic to talk about, as there are so many areas within programming, I didn’t know where to start. I needed to demonstrate a language that was simple, yet powerful, so the kids stayed engaged during my presentation. I’ve been learning Python for the past several months now, and it was very easy for me to pick up and use. So I thought, why not demonstrate the power of drawing on the screen with Python? To do so, I used Python’s turtle module. For my friends on Windows machines, you may have to install Python first in order to follow the steps in this tutorial.
Getting started with the turtle module is easy. To spin up a Python environment, all you have to do is hold ⌘ (Command), then press the spacebar and type in “IDLE” (case-insensitive). That will spin up a version of the Integrated Development and Learning Environment for Python.
With IDLE, you’re all setup to write the code shown in this article. So please, feel free to follow along, I encourage you to do so. Without further ado, let’s get to drawing!
The first thing you need to do is tell Python to import the turtle module. After that, tell the turtle to draw a line moving forward by a distance of 100.
You’ll see a window come up, and a pointer will draw a line to the right.
That’s nice, but it would be even nicer if the cursor actually looked like a turtle. Fortunately, that’s easy. First, reset the turtle so it’s in it’s original position. Then, tell the turtle to change its shape into.. well, a turtle.
With that, your turtle should look much better. Now, let’s get to drawing some shapes. First, a circle. It’s as simple as just telling the turtle to draw a circle, and putting in what you want the radius to be.
Next, let’s draw a square, then a triangle, and finish up with some more interesting shapes. Here’s how to draw a square.
Now that’s great and all, but we see some repetition here. That can be cleaned up using a for loop. We just set the counter to the number of sides in a square, and tell the turtle to move forward and to the left.
numberOfSquareSides = 4
for i in range(numberOfSquareSides):
Similarly, a triangle can be drawn with little effort.
numberOfTriangleSides = 3
for i in range(numberOfTriangleSides):
These shapes are cool, but with the turtle module, you can draw some pretty complicated shapes with just a bit of code. Here’s how to draw a star.
if abs(turtle.pos()) < 1:
There’s a couple of newer things here. One is that you can set the color of the turtle’s pen with the turtle’s color() method. The turtle’s begin_fill() and end_fill() methods tell the turtle when to fill in the current shape it’s drawing. The while loop tells the turtle to draw as long as the indented conditions are met. Here, as long as the turtle is far enough from where it started, it will draw the star in a circle by moving forward and then to the left. With the drawStar() method defined, call it by typing “drawStar()”, hit enter and watch the turtle draw away!
Lastly, let’s roll up our sleeves a bit, and draw the Olympic logo. Sounds pretty complicated, but it’s just a series of circles interleaved together. The trick here is to tell the turtle to move its pen up and down, so it ends drawing at one spot and picks up at the other. That way, we avoid the situation where the turtle draws all of the circles on the same line.
There are two things we need to figure out. One is how to draw the circle, the other is where to draw the circle.
def drawOlympicCircle(x, y,
The method drawOlympicCircle() takes as arguments the x position, y position, color, and radius to draw the circle at.
[(60, 0, 'blue'),
(-60, 0, 'purple'),
(120, 60, 'red'),
(0, 60, 'yellow'),
(-120, 60, 'green')]
for position in positions:
On the other hand, drawOlympicLogo() takes as an argument the radius, builds a list of tuples, called positions, and passes them to the drawOlympicCircle() method to be drawn. The result is pretty awesome!
I think that’s pretty cool! The best part is, this is just the tip of the iceberg. Check out the turtle module and experiment for yourself. Feel free to share anything you draw up!
That’s it for today! As always, feedback is greatly appreciated. For convenience, a Python file containing all of the shapes drawn can be downloaded from Github here.