3-D Turtles

The purpose of this lab is to give you one more chance to extend your drawing system. You should have a reasonable understanding of how all the pieces fit together, so now we're going to swap out the standard 2D turtle and put in a 3D turtle. All of your 2D turtle programs should continue to work just fine, but now you can make 3D shapes as well. In addition, you'll be able to rotate your completed drawings with the mouse.


Tasks

The lab consists of three parts.

The goal is to be able to make 3D shapes and scenes as easily as 2D. You should be able to create a Cube class, for example, as well as 3D trees and other L-system shapes that work exactly the same way as their 2D counterparts.

  1. Create a new working folder. Copy your lsystem.py, turtle_interpreter.py, shapes.py, and tree.py files from last week. Label them as version 5. Then download the 3D turtle file (which is documented at the bottom of this page).
  2. One difference between the standard turtle and the 3D turtle is that the latter is implemented as a class. Therefore, you need to create a single instance of the turtle object that will be used by all TurtleInterpreter objects.

    At the top of turtle_interpreter.py, import turtleTk3D instead of turtle, and create a global variable called turtle and initialize it to None.

    Second, if you have a call to turtle.setup(), delete it.

    Third, before calling any other turtle functions in your __init__ method, but after the test of TurtleInterpreter.initialized, put the following two lines.

    global turtle
    turtle = turtleTk3D.Turtle3D(dx, dy)
    

    The first line tells the function to use the turtle variable in the global symbol table, and the second line creates a new turtleTk3D object. By putting the 3D turtle object in a variable called turtle, expressions like turtle.left(angle) or turtle.forward(distance) still work as expected.

  3. Make the following additional changes to turtle_interpreter.py.
  4. Make the following changes to shapes.py.

  5. Run test function two. It is a simple example of how to begin building a 3D scene.
  6. Update your tree.py file. Add roll, pitch, and zpos to the parameter list of the Tree class draw method, giving them all default values of 0. Then pass the three parameters on to the parent draw function. Then run the test function 3 using one of the L-systems below.

Appendix: Turtle3D Documentation

The Turtle3D class implements a 3D turtle abstraction using the Tkinter package.

The Turtle3D class includes the following methods for public use.