To check whether you have the
turtle module, open the Python interpreter and type
>>> import turtle >>> bob = turtle.Turtle()
When you run this code, it should create a new window with small arrow that represents the turtle. Close the window.
Create a file named
mypolygon.py and type in the following code:
import turtle bob = turtle.Turtle() print(bob) turtle.mainloop()
turtle module (with a lowercase ’t’) provides a function called
Turtle (with an uppercase ’T’) that creates a Turtle object, which we assign to a variable named
bob displays something like:
<turtle.Turtle object at 0xb7bfbf4c>
This means that
bob refers to an object with type
Turtle as defined in module
mainloop tells the window to wait for the user to do something, although in this case there’s not much for the user to do except close the window.
Once you create a Turtle, you can call a method to move it around the window. A method is similar to a function, but it uses slightly different syntax. For example, to move the turtle forward:
fd, is associated with the turtle object we’re calling
bob. Calling a method is like making a request: you are asking
bob to move forward.
The argument of
fd is a distance in pixels, so the actual size depends on your display.
Other methods you can call on a Turtle are
bk to move backward,
lt for left turn, and
rt right turn. The argument for
rt is an angle in degrees.
Also, each Turtle is holding a pen, which is either down or up; if the pen is down, the Turtle leaves a trail when it moves. The methods
pd stand for “pen up” and “pen down”.
To draw a right angle, add these lines to the program (after creating
bob and before calling
bob.fd(100) bob.lt(90) bob.fd(100)
When you run this program, you should see bob move east and then north, leaving two line segments behind.
Now modify the program to draw a square. Don’t go on until you’ve got it working!