Previous Lecture lect07 Next Lecture

Resources from lecture

Resources from lecture

Learning Goals

#Turtle Graphics: Formal introduction to modules

from module_name import function_name
import turtle

Start by creating a new Turtle object using the special function “Turtle”

jane = turtle.Turtle()

So far we have worked with some basic data types

From here on you can customize what jane looks like and command it to move around on your screen. You can do this by calling the functions that the turtle module provides on our turtle jane using the dot notation.

For example, you can make jane look like an actual turtle using the shape function

jane.shape("turtle")

Or change jane’s color

jane.color("red")

You can query jane’s location on your canvas

jane.xcor()
jane.ycor()

And finally you can command jane to move forwrad backward and turn left or right

jane.forward(100)
jane.backward(100)
jane.left(90)
jane.right(90)
jane.up()          # Lift the turtle's tail to avoid leaving a trail
jane.goto(100, 20) # Go to location (100,20)
jane.down()     # Put the tail down to start drawing
jane.forward(100)

To learn about all the turtle functions that you can use type

dir(turtle) or dir(jane)

To learn more about the usage of each function use the “help” function:

help(jane.forward)

Notice the concept of “abstraction” at work. As the user of the function forward() we don’t have to know the details of how it was implemented, we only have to know what the function does, what its parameters are and how to use it. This information is exactly what “help” displays.

Coding example: