dark mode light mode Search Menu
Search

Create Turtles with Python

Tony Alter on Flickr

Turtles make great pets. They’re small, slow, and clean. Plus, who can resist their cute, wrinkled faces peeping out of their shells?

Now, you can create your own pet turtle. All you need is a programming language called Python, and an extension called Turtle.

Instructions

1. Open up your favourite web browser. Navigate to www.repl.it

2. Type ‘Python’ in the box. Make sure you select ‘Python (with Turtle)’. Otherwise, the code won’t work!

3. Type this code in the left-hand editor:

from turtle import *

maurice = Turtle()
maurice.shape("turtle")
screen = Screen()
screen.onscreenclick(maurice.goto)

4. Hit the run button.

See the turtle icon that appeared in the right-hand screen? If you click inside the screen, your turtle comes running over. Have fun playing with your new buddy!

Explanation

from turtle import *

Turtle is a Python ‘module’. Picture a module as a shop full of nifty gadgets. These gadgets aren’t included in a programming language by default. You have to explicitly tell the Python interpreter that you want to ‘borrow’ these gadgets and use them in your code. Once you do, you can do all kinds of cool things!

The purpose of Turtle is to make it easy to draw shapes and lines. And to create adorable turtles.

maurice = Turtle()

In most programming languages, if a word starts with a capital letters and ends with brackets then it’s invoking a constructor. A constructor builds an object. There are predefined objects, like number generators, and web sockets, and turtles. You can also create your own objects.

Here, we’re creating our pet turtle. I’ve named mine Maurice. because he’s a sophisticated reptile. If you want to name your turtle something else (Frankie, Tubs, Slowpoke), replace ‘maurice’ with the name of your choice. But make sure you keep the same name through all the code!

maurice.shape(“turtle”)

By default, Maurice is shaped like a little triangle. This code makes the Python interpreter use a turtle icon instead. Want to check this?

Code can be confusing to read, especially when there’s a lot of it. That’s why programmers use comments to explain pieces of their code to other programmers. Comments are ignored by the computer when running a program.

There’s another use for comments. The easiest way to understand a single line of code is to ‘comment it out’. In other words, to, ‘transform the line into a comment’. It has the same effect as deleting a line of code, except that you can still see the text. By ‘deleting’ this line, you can then watch how the program changes.

To comment out a line, put a ‘#’ symbol in front of it. See how the text turned green in the editor? Now hit the ‘Run’ button and see what happens!

screen = Screen()

This line creates the screen object. It’s where Maurice is going to play.

screen.onscreenclick(maurice.goto)

The first part of this line — screen.onscreenclick(…) — registers a ‘screen click’ event handler. The screen now pays attention to mouse clicks. It remembers the coordinates where the mouse click happened. They second part — maurice.goto — tells Maurice to go to a pair of coordinates. Together, the ‘screen click’ event handler grabs the coordinates of the mouse click, then passes them to Maurice, who dashes over.

More Fun Things to Do

Here’s more code you can add in between lines 4 and 5.

Change the colour of your turtle’s pen:

maurice.color(“blue”)

You can choose any colour you want: green, pink, yellow, orange. Make sure your colour is written in between quotation marks!

Change your turtle’s speed:

maurice.speed(10)

The number inside the brackets is called the speed variable. It can be any number between 0 and 10. 0 is a slow, plodding turtle and 10 is a zooming, speedy one. Experiment with a couple different numbers!

Learn More

More about comments

https://kidscodecs.com/comments/
http://www.pythonforbeginners.com/comments/comments-in-python

About Objects

https://www.tutorialspoint.com/python/python_classes_objects.htm

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