You are on page 1of 2

Outcomes While Loops in Turtle Graphics

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)

Drawing a Square Using a Loop Drawing a Star Shape Using Nested


• You can use a while loop to draw a square, using • The example on the previous slide uses a while Loops
the code shown below: loop to draw four sides
• Similarly you can use a loop to draw a star shape • On the right is a
side = 0 flower image
with five sides, i.e.:
while side < 4: created by a
Run the loop five times,
turtle.forward(400) side = 0 i.e. side = 0, 1, 2, 3, 4 single program
turtle.right(90) while side < 5: • It’s a good
turtle.forward(400) example of using
side = side + 1 turtle.right(144)
loops, including
Run the loop four times, i.e.
nested loops
the loop will be executed with side = side + 1
these values, side = 0, 1, 2, 3

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")

– part of a circle import turtle turtle.up() # Don't draw while we move


• Stage 2: draw the petals turtle.goto(100, -400) # Move the turtle to bottom right
– uses a nested loop turtle.speed(0) turtle.left(90) # Point the turtle upwards
turtle.down() # Start drawing from now onwards
• Stage 3: draw the central part turtle.circle(1000, 30) # Draw circle with radius 1000
– uses a loop

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

• This is the inner loop for


The Inner Loop drawing one petal, using The Outer Loop – Drawing the Flower Stage 3 - Drawing the Flower Centre
– Drawing a Petal 5 circles of different sizes petal = 0 The outer while loop uses the inner 45 degrees
petal_piece = 0 • Below we use unfilled circles while loop 8 times, for 8 petals • The middle of the flower is drawn
while petal < 8:
to illustrate the process: using a while loop, which draws a
while petal_piece < 5: turtle.up() Each petal has a different
turtle.forward(50) series of circles
turtle.up() starting point, 50 pixels
turtle.forward(petal_piece * 20) turtle.down()
away from the center • We first draw the largest circle,
turtle.left(90) The result
Starting position after the loop petal_piece = 0 then the smaller ones The reduction in
turtle.down() petal_piece=0 The inner while loop
while petal_piece < 5:
draws five circles of a • We reduce the radius each time radius each time is 3
turtle.begin_fill() ...
turtle.circle(15 + (petal_piece*5)) petal_piece petal (this part is shown • If we reduced it by 3 pixels each time,
= petal_piece + 1 in the previous slide)
turtle.end_fill() The result The result we would get the top image:
after the loop after the loop
turtle.up() petal_piece=1 petal_piece=2 turtle.up() Here we go back to the • However, let’s make a more interesting
turtle.right(90) turtle.backward(50) starting point, the center
turtle.down()
pattern like the bottom:
turtle.backward(petal_piece * 20)
Each petal is drawn using a different
turtle.down()
turtle.left(45) angle by turning the turtle 45 degrees
• We do that by increasing the size of
The result The result The reduction in
petal_piece = petal_piece + 1 after the loop after the loop petal = petal + 1 the reduction for each successive circle radius is 3, 6, 12, 24
petal_piece=3 petal_piece=4 Move on to the next petal

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

You might also like