Section 13.4 The Turtle Object
Let’s try a couple of lines of Python code to create a new turtle and start drawing a simple figure like a rectangle. We will refer to our first turtle using the variable name alex
(there’s a tradition of using people’s names for turtles, so I’ll go with it), but remember that you can choose any name you wish as long as you follow the naming rules for Python variables.
The program as shown will only draw the first two sides of the rectangle. After line 4 you will have a straight line going from the center of the drawing canvas towards the right. After line 6, you will have a canvas with a turtle and a half drawn rectangle. Press the run button to try it and see.
Here are a couple of things you’ll need to understand about this program.
The first line tells Python to load a module named turtle
. That module brings us two new types that we can use: the Turtle
type, and the Screen
type. The dot notation turtle.Turtle
means “The Turtle
type that is defined within the turtle
module”. (Remember that Python is case sensitive, so the module name, turtle
, with a lowercase t, is different from the type Turtle
because of the uppercase T.)
We then create and open what the turtle module calls a screen (we would prefer to call it a window), which we assign to variable window. Every window contains a canvas, which is the area inside the window on which we can draw.
In line 3 we create a turtle. The variable alex
is made to refer to this turtle. These first three lines set us up so that we are ready to do some drawing. When you use the name of the type to create an object, that is called using a constructor.
In lines 4-6, we instruct the object alex
to move and to turn. We do this by invoking or activating alex
’s methods — these are the instructions that all turtles know how to respond to. Here the dot indicates that the methods invoked belong to and refer to the object alex
. Unlike the math
module, where angles are measured in radians, the turtle
module measures angles in degrees.
The key thing to notice here is that the turtle is the focus of our attention. In our object orientation, alex.forward(75)
is telling the turtle to move forward 75 pixels, unlike a procedural program which would say forward(alex, 75)
where the action is the focus rather than the actor.
Checkpoint 13.4.2.
The following program uses a turtle to draw a capital L as shown in the picture to the left of this text,
But the lines are mixed up. The program should do all necessary set-up: import the turtle module, get the window to draw on, and create the turtle. Remember that the turtle starts off facing east when it is created. The turtle should turn to face south and draw a line that is 150 pixels long and then turn to face east and draw a line that is 75 pixels long. We have added a compass to the picture to indicate the directions north, south, west, and east.
Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.
import turtle
window = turtle.Screen()
ella = turtle.Turtle()
---
ella.right(90)
ella.forward(150)
---
ella.left(90)
ella.forward(75)
Checkpoint 13.4.3.
Consider the following code:
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(150)
alex.left(90)
alex.forward(75)
What does the line “import turtle” do?
It creates a new turtle object that can be used for drawing.
- The line "alex = turtle.Turtle()" is what actually creates the turtle object.
It defines the module turtle which will allow you to create a Turtle object and draw with it.
- This line imports the module called turtle, which has all the built in functions for drawing on the screen with the Turtle object.
It makes the turtle draw half of a rectangle on the screen.
- This functionality is performed with the lines: "alex.forward(150)", "lex.left(90)", and "alex.forward(75)"
Nothing, it is unnecessary.
- If we leave it out, Python will give an error saying that it does not know about the name "turtle" when it reaches the line "wn = turtle.Screen()"
Checkpoint 13.4.4.
Why do we type turtle.Turtle()
to get a new Turtle object?
This is simply for clarity. It would also work to just type "Turtle()" instead of "turtle.Turtle()".
- We must specify the name of the module where Python can find the Turtle object.
The period (.) is what tells Python that we want to invoke a new object.
- The period separates the module name from the object name. The parentheses at the end are what tell Python to invoke a new object.
The first "turtle" (before the period) tells Python that we are referring to the turtle module, which is where the object "Turtle" is found.
- Yes, the Turtle type is defined in the module turtle. Remember that Python is case sensitive and Turtle is different from turtle.