Chapter 12

Throw Shapes with Turtles

Python includes a turtle. It’s not a real turtle, because you’d have to feed it and look after it — which would be cute, but probably wouldn’t teach you much about computers.

image

Meet the Python Turtle

The Python turtle is a drawing bot. You can steer it around the screen and make it draw shapes. With only a little programming magic, you can make awesome pictures that will make your friends believe you’re a computer genius.

On the Raspberry Pi, the turtle is built right into Python. To use it, add a single line to the start of your code, like this:

import turtle

That’s all you need. You can now write commands for the turtle. When you run your code, the turtle appears in a special window. It draws a line as it moves. It’s that simple!

Figure 12-1 shows the turtle drawing a complicated shape using a few simple commands.

Get started with turtle commands

Turtle programming is really, really easy because there are only three kinds of commands:

  • The first kind turns the turtle.
  • The second kind moves it and draws a line.
  • The third kind turns drawing on and off, changes the pen color, and does other useful stuff, like clear the screen.

Most turtle programs have a block of starting code to set up the turtle and maybe define useful numbers ...

Get Raspberry Pi For Kids For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.