12 Chapter 12: Turtle Graphics

TURTLE GRAPHICS

0s and 1s in a hypnotic arrangement.

 

Topics Covered:

  • Turtle graphics

Using turtle graphics is a fun way to hone your problem solving and programming skills, as well as a writing code that can generate graphics. It was part of the original Logo programming language developed in 1967.

Importing the turtle module into your Python program allows you to create simple drawings on the screen. The name turtle, or turtle graphics, is a term in computing that means “using a relative cursor to draw on a Cartesian plane”. The relative cursor is called the turtle. The Logo programming language compared the drawing capabilities to that of a turtle, with a pen attached to its tail, crawling around on the screen. If the tail was down, a line was produced, but if the tail was raised, the movement of the turtle left no visible trace.

The graphic turtle begins at (0,0) in the x-y plane. By default, it is facing to the right. To begin drawing, you must first import the turtle module and then create a turtle object. The forward command will move the turtle a given number of pixels. Let’s jump right to an example and start exploring the options and functions available.

The code to import the turtle module.
What the python turtle graphic will look like.

When you run a turtle program, it will pop up a new “Python Turtle Graphics” window for your drawing. After importing the turtle module, you should create a turtle object by calling the Turtle() method. I named my turtle “Tom”, but you can create the turtle object using any variable name.

In the sample program, the pencolor() method was used to change the turtle to blue. Next, the shape of the turtle was changed from a small arrowhead to an actual turtle. The forward() command will move the turtle straight ahead by a distance of the number of pixels passed as the argument. A pixel, short for picture element, is one of over a million little dots that make up your screen display. A blue line of 300 pixels was drawn left-to-right from the origin.

Tom the turtle turned 90 degrees to the left() and then changed its color to red. After the turn, Tom is now facing upwards and draws a vertical line of 200 pixels. The method exitonclick() will cause the window to disappear whenever the user clicks anywhere in the window.

In our next program, we will show two ways to draw a square. In the first example, Tina the turtle repeats the steps “move forward 200 pixels and turn 90 degrees left” four times. A second square, this one green, is created by using a for loop to repeat the forward() and left() methods.

Code for the turtle to change directions.

When the previous program is executed, it produces the window shown below. In order to avoid drawing a line that connected the two squares, the penup() method was used to move 100 pixels to the left of the black square. Before drawing the green square, the pendown() method was called to resume drawing. The output is shown next:

The turtle created a square with the code from the previous image.

This third turtle graphics program introduces several new turtle methods:

The code for the turtle to create different shapes and words.

First, we show how the hideturtle() will make that shape invisible. Next, we show how the write() method can be used to display a string at the current turtle position. One optional parameter allows you to change the font.

The begin_fill() and end_fill() methods can be used to change the background color of a shape created using a concave set of points. The fillcolor() method should be called to set the shape’s background color. You first use the goto(x,y) method to position the turtle to the first point of your polygon. After that, a series of goto(x,y) calls are used to create the shape. Once the end_fill() method is called, the interior color of the shape is filled.

Finally, we show how the dot() method can be used to draw a small circle on the window. With the pendown() activated, this could be used to create a line graph. With penup() active, you could use the dot() method to create a scatter plot.

The output of the code from the previous image. The turtle created a yellow triangle, the word "Hooray" in red font color, Hello in black font color, and a red line with points on it.

Common Turtle Graphics Methods

Table of Common Turtle Graphic Methods

* Note: There are hundreds of different color names that you can use. Also, there are literally millions of colors that you can create by using RGB or hex codes. The following web site provides a great way to check out many of the available Turtle colors:

https://trinket.io/docs/colors

 

INTERACTIVE – What does the turtle say?

Check out the Turtle graphics program below, but do not run it. Try to figure out what the output will look like first. Then, go ahead and run it and verify if you are the turtle master or not.

 

Chapter Review Exercises:

12.1. What is a pixel and approximately how many of them appear on a screen?

12.2. Explain what each of the following turtle graphic methods do?

  • left
  • forward
  • goto
  • write
  • penup
  • endfill
  • dot

 

 

Programming Projects:

12.1. Using Turtle Graphics, write a program that asks the user to type in the following inputs:

  • Number of sides
  • Length of each side
  • Color of the sides
  • The pen size

Your program should then draw a regular polygon with those properties.

 

12.2. Write a Python program to generate the graph below:

Example Bar Graph titled: Worldwide PC Vendor Unit Shipment Estimates for 4Q18.

License

Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

Python Textbook Copyright © 2022 by Dr. Mark Terwilliger is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book