The turtle

0. Learning objectives


1. The turtle module


It's time to write the first graphics program! We will now introduce to the turtle module.

The very first turtle robot, was designed in the 40s!!! It was a little robot designed in the shape of a turtle with a pen. You could write simple procedures to control the robot and create drawings. Python developers decided to create a set of function that mimic the behavior of this robot.

Just like the math module we used before, you need to import the turtle whenever you want to use it by adding a importturtle at the beginning of your code. Since the turtle functions are all contained within this module, you will also need to use "dot notation" to call the turtle drawing functions.

Remember Lightbot? The turtle is kind of like the robot in Lightbot. We will issue commands to the turtle, like going straight, turning left/right, and the turtle will execute those commands and, in the process, it will draw some things to a new window! With just a few basic commands + some of the programming techniques we have seen so far, you can create some pretty neat drawings. Here are a few useful commands:

  • forward(distance): moves the turtle forward by some distance (in pixels),
  • backward(distance): moves the turtle backwards by some distance (in pixels),
  • left(angle): turns the turtle left by some angle (in degrees),
  • right(angle): turns the turtle right by some angle (in degrees),
  • up(): lifts the turtle so that any call to forward() or backward() does not draw anything until we put the turtle back down,
  • down(): puts the turtle back down so that we can draw stuff with calls to forward() or backward()
  • done(): this tells the turtle that we're done drawing, so we can interact with the pop-up window properly.

To get a feel for the turtle click on the buttons on the right to call the forward() or backward() functions passing the distance (you specify) as parameter, or to call the left() or right() functions passing the angle (you specify) as parameter. Try to draw a triangle, or some other shape (or letter).

   



Examples

Example 1: drawing a square

Let's do some examples, so you can see how the turtle module works. We'll start off by drawing a square which has a side length of length (on each of the four sides).

Challenge: Can you use the function above to draw two cascading squares of the same size? With cascading we mean that the top-left corner of the second square should be a little to the bottom and to the right of the top-left corner of the first square.

We can draw cascading squares by using our square() function twice: call square(), lift the pen, move a little bit down-right, put the pen back down, and call the square()function again. Notice that the every time the turtle turns, it turns with respect to the direction it is facing, not some absolute reference.

Copy or type these lines in the script above.

Example 2: nested squares

Using the turtle is useful to understand recursion.

Write a recursive function called nested_squares(length, level) which draws level nested squares with the largest one side length.

Think about how you would break the problem in base and recursive parts.

You could start by deciding that in the base case we do nothing and that the base case happens when level == 0. Then, for the recursive case you need to solve a small part of the problem (draw a square of side length) and then combine it with a smaller version of the problem. The combination, in this case, is to lift the pen and move a little inside of the square you just draw, and the smaller part of the problem is to call nested_squares() with a smaller size and a smaller level.

Note: if you don't want to see the turtle "tracing" animation, you can set turtle.tracer(False) before doing any drawing commands, which will do the drawing without showing you the animation. This will be useful when you are performing a lot of drawing commands and don't want to wait for the animation to run.

Note2:In the base case we used the keyword pass instead of return. This keyword tells the interpreter to do nothing and go to the next statement in the code. It is like a no operations command. In Python every block of code after a : has to have at least one statement. If we don't want to do anything, we can use pass. The difference between pass and return, is that return will yank you out of the function while pass will simply do nothing but keep staying in the function. As you write more programs, you will notice that sometime you might want to use one and sometimes the other depending on your algorithm. In this case, we could have used either! (Try it out).

Example 3: spiral

Let's do another recursive example with the turtle. This is the the graphic version of the go_back() example we have seen in the recursion chapter. This time, we'll draw a spiral. Our spiral() function will take in length that we would like to draw for the current level (same concept as the previous example). The base case will be if the current "level" is equal to zero, in which case we return (back through all the recursive function calls). Otherwise, in the recursive step, we will go forward by some length, then turn left by an angle of 30 degrees, and then keep recursing by calling spiral() with a smaller length (95% of the current length) and decrement the level by 1. After drawing a part of the spiral and performing the recursive call, we would also like the turtle to return to it's starting position, so we need to re-orient the turtle by turning right by 30 degrees, and then moving backwards (undoing the forward call) by the same length. Write it up, then check the code!