4.3. Instances — A Herd of Turtles

Just like we can have many different integers in a program, we can have many turtles. Each of them is an independent object and we call each one an instance of the Turtle type (class). Each instance has its own attributes and methods — so alex might draw with a thin black pen and be at some position, while tess might be going in her own direction with a fat pink pen. So here is what happens when alex completes a square and tess completes her triangle:

Here are some How to think like a computer scientist observations:

Check your understanding

    turtle-2-1: True or False: You can only have one active turtle at a time. If you create a second one, you will no longer be able to access or use the first.
  • True
  • You can create and use as many turtles as you like. As long as they have different names, you can operate them independently, and make them move in any order you like. To convince yourself this is true, try interleaving the instructions for alex and tess in ActiveCode box 3.
  • False
  • You can create and use as many turtles as you like. As long as they have different names, you can operate them independently, and make them move in any order you like. If you are not totally convinced, try interleaving the instructions for alex and tess in ActiveCode box 3.

Mixed up programs

        turtle-2-2: The following program has one turtle, "jamal", draw a capital L in blue and then another, "tina", draw a line to the west in orange as shown to the left, .  The program should do all set-up, have "jamal" draw the L, and then have "tina" draw the line.   Finally, it should set the window to close when the user clicks in it.

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 wn = turtle.Screen() --- jamal = turtle.Turtle() jamal.pensize(10) jamal.color("blue") jamal.right(90) jamal.forward(150) --- jamal.left(90) jamal.forward(75) --- tina = turtle.Turtle() tina.pensize(10) tina.color("orange") tina.left(180) tina.forward(75) --- wn.exitonclick()
        turtle-2-3: The following program has one turtle, "jamal", draw a line to the north in blue and then another, "tina", draw a line to the east in orange as shown to the left, .  The program should import the turtle module, get the window to draw on, create the turtle "jamal", have it draw a line to the north, then create the turtle "tina", and have it draw a line to the east.  Finally, it should set the window to close when the user clicks in it.

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 --- wn = turtle.Screen() --- jamal = turtle.Turtle() jamal.color("blue") jamal.pensize(10) --- jamal.left(90) jamal.forward(150) --- tina = turtle.Turtle() tina.pensize(10) tina.color("orange") tina.forward(150) --- wn.exitonclick()
Next Section - 4.4. The for Loop