Professional Documents
Culture Documents
COMP1021 • After completing this presentation, you are • Let’s look at some examples which use loops in
Introduction to Computer Science expected to be able to: turtle graphics programming
1. Use while loops to create shapes with turtle • Loops are very useful in turtle graphics since you
Using Loops with graphics programming often need to use a turtle to do things repeatedly
• For example, to draw a turtle.forward(10)
Turtle Graphics square with a length of 10 turtle.right(90)
you need to ask the turtle to turtle.forward(10)
Gibson Lam and David Rossiter move forward and turn four turtle.right(90)
turtle.forward(10)
times, as shown on the right: turtle.right(90)
turtle.forward(10)
COMP1021 Using Loops with Turtle Graphics Page 2 turtle.right(90)
The Program Stages Stage 0 – Get the Graphics Started Stage 1 – Create the Curved Stem
• We can create the stem of the flow
• Stage 0: get the turtle graphics started • Like many of the programs we have seen, the using the turtle.circle() command i.e.
first step is to import the turtle module and set
– import the turtle module, open the window turtle.width(20)
some initial parameters i.e.:
• Stage 1: create the curved stem turtle.color("green")
COMP1021 Using Loops with Turtle Graphics Page 8 COMP1021 Using Loops with Turtle Graphics Page 9
Stage 2 – An Example of a Nested Loop Structure Designing the Code
Drawing a Flower Using Nested Loops
while . . .condition . . . : • Let’s look at how we can draw the flower using two loops
• We use a nested loop to
draw the eight red petals . . .statement(s) . . . • Outer loop: repeat eight times for drawing eight petals
of the flower, shown here: while . . .condition . . . : • Move to the drawing position of the petal
• Inner loop: repeat five times for drawing five circles
. . .statement(s) . . .
• The inner while loop • Move to the drawing position of the circle
draws one petal • As you already know, a loop inside another loop is • Draw a circle of the appropriate size
using five circles called a nested loop (the circles get larger each time)
• Return to the starting position of the petal
• The outer while loop uses • It doesn’t matter what type of loop it is; any type of
• Return to the centre position of the flower
the inner while loop to loop inside any type of loop is called a nested loop
• Rotate the turtle by 45 degrees to draw the next petal
draw eight red petals • So far we know about while loops, soon we will see
COMP1021 Using Loops with Turtle Graphics Page 10 another type of loop • We will show the main code in the following slides
radius = 60
radius_reduction = 3 Drawing the Hiding the Turtle
while radius > 10:
Flower Centre
turtle.up()
• Sometimes the pictures in the turtle window will look
The current turtle position
turtle.forward(radius) better if the turtle is not visible inside the window
is the center of the circle, so we
turtle.left(90) • In our previous flower example, the
temporarily move the turtle to the
turtle.down()
correct position (the side of the circle) turtle is in the middle of the flower
turtle.begin_fill() before making the circle when the drawing finishes
turtle.circle(radius) Then we draw the yellow circle • We hide the turtle near the end of the
turtle.end_fill() that we want
program using this line of code:
turtle.up() The we move the turtle
turtle.right(90) back to the center position, turtle.hideturtle()
turtle.backward(radius) ready for the next loop
turtle.down()
• After running the code, the turtle is then hidden in the
We make the next circle smaller final result
radius = radius - radius_reduction Next time, the decrease
radius_reduction = radius_reduction * 2 in radius will be greater COMP1021 Using Loops with Turtle Graphics Page 17