4.2. Our First Turtle Program

Let’s try a couple of lines of Python code to draw a simple figure like a rectangle.

The program as shown will only draw the first two sides of the rectangle. After line 2 you will have a straight line going from the center of the drawing canvas towards the right. After line 4, you will have a canvas with a turtle and a half drawn rectangle. Press the run button to try it and see.

Here are a couple of things you’ll need to understand about this program.

The first line tells Python to load a module named turtle. That module allows you to use code someone else has written to create drawings. The import line must come before all other lines. With the first line set up, we are ready to do some drawing.

In lines 2-4, we instruct the turtle to move and to turn. We do this by invoking or activating the turtle functions — these are the instructions that all turtles know how to respond to. Here the dot indicates that the functions invoked belong to and refer to the turtle module.

Complete the rectangle …

Modify the program by adding the commands necessary to have the turtle complete the rectangle.

Check your understanding

    turtle-2-1: Which direction does the Turtle face when it is created?

  • North
  • Some turtle systems start with the turtle facing north, but not this one.
  • South
  • No, look at the first example with a turtle. Which direction does the turtle move?
  • East
  • Yes, the turtle starts out facing east.
  • West
  • No, look at the first example with a turtle. Which direction does the turtle move?

Mixed up programs

        turtle-2-2: The following program uses a turtle to draw a capital L as shown in the picture to the left of this text,  but the lines are mixed up.  The program should import the turtle module.  Remember that the turtle starts off facing east when it is created.  The turtle should turn to face south and draw a line that is 150 pixels long and then turn to face east and draw a line that is 75 pixels long.  We have added a compass to the picture to indicate the directions north, south, west, and east.  

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 --- turtle.right(90.0) turtle.forward(150.0) --- turtle.left(90.0) turtle.forward(75.0)
        turtle-2-3: The following program uses a turtle to draw a checkmark as shown to the left,  but the lines are mixed up.  The program should import the turtle module.  The turtle should turn to face southeast, draw a line that is 75 pixels long, then turn to face northeast, and draw a line that is 150 pixels long.  We have added a compass to the picture to indicate the directions north, south, west, and east.  Northeast is between north and east. Southeast is between south and east. 

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 --- turtle.right(45.0) turtle.forward(75.0) --- turtle.left(90.0) turtle.forward(150.0)
        turtle-2-4: The following program uses a turtle to draw a single line to the west as shown to the left,  but the program lines are mixed up.  The program should import the turtle module.  The turtle should then turn to face west and draw a line that is 75 pixels long.

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 turtle.left(180.0) turtle.forward(75.0)

Turtles also have attributes — (sometimes called properties). For example, the turtle has a color attribute. The function invocation turtle.pencolor(“red”) will make the turtle red and the line that it draws will be red too.

The color of the turtle, the width of its pen(tail), the position of the turtle within the window, which way it is facing, and so on are all part of its current state.

Quite a number of methods exist that allow us to modify the turtle. In the example below, we show just show a couple and have only commented those lines that are different from the previous example.

Extend this program …

  1. Modify this program so that it prompts the user to enter the desired pen color. It should store the user’s responses in a variable, and modify the color of the window according to the user’s wishes. (Hint: you can find a list of permitted color names at https://www.w3schools.com/colors/colors_names.asp. It includes some quite unusual ones, like “PeachPuff” and “HotPink”.)

  2. Do similar changes to allow the user, at runtime, to set tess’ color.

  3. Do the same for the width of tess’ pen. Hint: your dialog with the user will return a string, but tess’ pensize method expects its argument to be an int. That means you need to convert the string to an int before you pass it to pensize.

Check your understanding

    turtle-2-5: Consider the following code:

    import turtle
    turtle.forward(150.0)
    turtle.left(90.0)
    turtle.forward(75.0)
    

    What does the line “import turtle” do?

  • It creates a new turtle object that can be used for drawing.
  • The line "alex = turtle.Turtle()" is what actually creates the turtle object.
  • It defines the module turtle which will allow you to create a Turtle object and draw with it.
  • This line imports the module called turtle, which has all the built in functions for drawing on the screen with the Turtle object.
  • It makes the turtle draw half of a rectangle on the screen.
  • This functionality is performed with the lines: "alex.forward(150)", "lex.left(90)", and "alex.forward(75)"
  • Nothing, it is unnecessary.
  • If we leave it out, Python will give an error saying that it does not know about the name "turtle" when it reaches the line "wn = turtle.Screen()"

Mixed up programs

        turtle-2-6: The following program uses a turtle to draw a blue capital L but the lines are mixed up.  The program should do all necessary set-up and set the pen size to 10.  The turtle should then turn to face south, draw a line that is 150 pixels long, turn to face east, and draw a line that is 75 pixels long.

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 --- turtle.pencolor("blue") turtle.pensize(10.0) --- turtle.right(90.0) turtle.forward(150.0) --- turtle.left(90.0) turtle.forward(75.0)
        turtle-2-7: The following program uses a turtle to draw a green capital T, but the lines are mixed up.  The program should do all necessary set-up and set the pen size to 10.  After that the turtle should turn to face north, draw a line that is 150 pixels long, turn to face west, and draw a line that is 50 pixels long.  Next, the turtle should turn 180 degrees and draw a line that is 100 pixels long.

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 turtle.pencolor("white") turtle.pensize(10.0) --- turtle.left(90.0) turtle.forward(150.0) --- turtle.left(90.0) turtle.forward(50.0) --- turtle.right(180.0) turtle.forward(100.0)
You have attempted of activities on this page
4.1. Hello Little Turtles!"> 4.3. The for Loop">Next Section - 4.3. The for Loop