Drawing Geometric Shapes with Turtles

You can use turtles to draw geometric shapes like squares, triangles, and rectangles.


(a_6)




Here are some things to notice about this program:

Check your understanding

trl-1: True or False: If all of a turtles turns add up to a multiple of 360 it will be facing the same way it started.



The for Loop

When sarah drew a square, it was quite tedious. She had to move then turn, move then turn, etc. etc. four times. A basic concept in computing is the ablity to repeat some code. In computer science, we refer to this as looping or iteration.

To draw a square we’d like to do the same thing four times — move the turtle forward some distance and turn 90 degrees. We previously used 8 lines of Python code to have sarah draw the four sides of a square. This next program does exactly the same thing but, with the help of the for statement, uses just three lines (not including the setup code). The code [0,1,2,3] creates a list with 4 items in it. The for statement will repeat the forward and left four times, one time for each item in the list. A list contains items in an order. Both the forward and left statements are in the body of the loop. The body of the loop is all of the statements that are indented further to the right than the for statement. A for statement must end with a colon: ‘:’ as shown in the code below.


(a_7)




While writing less lines of code might be nice, it is not as important than the fact that we found a pattern here that can be repeated. If we want to change the number of times this pattern repeats we can just change the number of items in the list in the for statement.

In the previous example, there were four integers in the list. But, we could use any list that had 4 items. In the following example, each time through the loop the value of aColor will change to the next color in the list. We can use the value of aColor in the program to change the turtle’s pen color.


(colorList)




Click on the forward button below to see how the loop changes the value of aColor each time through the loop. The print() statement prints the value in aColor. Notice that we used “wn” which is short for “window” to name our screen object this time instead of “canvas”. It doesn’t really matter what we name the screen, but it is useful to know what kind of thing it is, so using “canvas”, “window”, or “wn” makes sense.

(list1)

Check your understanding

trl-2: How does Python know which lines are in the body of the loop?




trl-3: How many items are in the following list: [0,1,2]




trl-4: Does the for statement have to end with a colon?



Mixed up program

trl-5: The following program uses a turtle to draw a triangle as shown to the left, but the lines are mixed up. The program should do all necessary set-up and create the turtle. After that, iterate (loop) 3 times, and each time through the loop the turtle should go forward 175 pixels, and then turn left 120 degrees. After the loop, 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 with the correct indention. Click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order or are incorrectly indented.



Mixed up program

trl-6: The following program uses a turtle to draw a rectangle as shown to the left, but the lines are mixed up. The program should do all necessary set-up and create the turtle. After that, iterate (loop) 2 times, and each time through the loop the turtle should go forward 175 pixels, turn right 90 degrees, go forward 150 pixels, and turn right 90 degrees. After the loop, 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 with the correct indention. Click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order or are incorrectly indented.



Time to Try Stuff Here is the program that draws a square again from above. Try to modify it to draw an octagon. How many times will you need to repeat the body of the loop? How much do you need to turn by each time? Remember that the total amount the turtle turns will equal 360 for any polygon.


(a_8)