O'Reilly logo

Raspberry Pi For Kids For Dummies by Richard Wentk

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required