7. Turtle star progressions
7.1. Sequencing: steps to draw a star

t.seth(0)
t.goto(20, 30)
.t.pu()
and t.pd()
are used either side of it to avoid line drawing when repositioning the turtle.t.fd(120)
.t.lt(144)
.Code Completion: star steps
Complete the code to draw a 5 pointed star with diagonals of 120 at (20, 30), by replacing the XXXs.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("star")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
XXX(20, 30)
t.pd()
t.seth(0)
t.fd(120)
t.lt(144)
t.fd(XXX)
t.lt(XXX)
t.fd(XXX)
t.lt(XXX)
t.fd(XXX)
t.lt(XXX)
t.fd(XXX)
t.lt(XXX)
s.exitonclick()
Completed code to draw a 5 pointed star with diagonals of 120 at (20, 30).
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("star")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
t.goto(20, 30)
t.pd()
t.seth(0)
t.fd(120)
t.lt(144)
t.fd(120)
t.lt(144)
t.fd(120)
t.lt(144)
t.fd(120)
t.lt(144)
t.fd(120)
t.lt(144)
s.exitonclick()
Tasks
From the code above, list the 10 lines that do the actual drawing.
From the 10 lines, list the simplest amount of code that is repeated.
From the code above, list the lines that do the actual drawing.
t.fd(120)
t.lt(144)
t.fd(120)
t.lt(144)
t.fd(120)
t.lt(144)
t.fd(120)
t.lt(144)
t.fd(120)
t.lt(144)
From the 10 lines, list the simplest amount of code that is repeated.
t.fd(120)
t.lt(144)
7.2. Iteration: using a for-loop to draw a star
Code Completion: star for loops
Complete the code to draw a star with diagonals of 120 at (20, 30), by replacing the XXXs.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("star")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
XXX(20, 30)
t.pd()
t.seth(0)
for _ in range(XXX):
t.fd(XXX)
t.lt(XXX)
s.exitonclick()
Completed code to draw a 5 pointed star with diagonals of 120 at (20, 30).
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("star")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
t.goto(20, 30)
t.pd()
t.seth(0)
for _ in range(5):
t.fd(120)
t.lt(144)
s.exitonclick()
Tasks
Modify the code above to draw a 9 pointed star with diagonals of 120 at (20, 30), by turning 160 degrees at the points of the stars.
Modify the code above to draw a 9 pointed star with diagonals of 120 at (20, 30), by turning 160 degrees at the points of the stars.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("star")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
t.goto(20, 30)
t.pd()
t.seth(0)
for _ in range(9):
t.fd(120)
t.lt(160)
s.exitonclick()
7.3. Definitions: using a def block to draw a star
- star(t, length=50, points=5, start_pos=(0, 0), start_h=0)
- t - the turtle object to draw the starlength - diagonal length; default 50points - number of points in the star, odd integer >=5; default 5start_pos - start position; default (0, 0)start_h - start heading; default 0 degrees
Code Completion: star definition
Complete the star
definition by replacing the “XXX”s.
def star(t, length=50, points=5, start_pos=(0, 0), start_h=0):
ang = (360 * ((points-1)/2))/points
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
for _ in range(points):
t.fd(length)
t.lt(ang)
Completed star
definition.
def star(t, length=50, points=5, start_pos=(0, 0), start_h=0):
ang = (360 * ((points-1)/2))/points
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
for _ in range(points):
t.fd(length)
t.lt(ang)
Tasks
Use the code above to draw a star with 7 points and diagonal length of 300, at (-100, 0).
Use the code above to draw a star with 7 points and diagonal length of 300, at (0, 0).
import turtle
def star(t, length=50, points=5, start_pos=(0, 0), start_h=0):
ang = (360 * ((points-1)/2))/points
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
for _ in range(points):
t.fd(length)
t.lt(ang)
s = turtle.Screen()
s.bgcolor("white")
s.title("star")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(9)
star(t, length=300, start_pos=(-100, 0), start_h=0, points=7)
t.ht()
s.exitonclick()
7.4. Adding pen colour and fill colour parameters
- star(t, length=50, points=5, start_pos=(0, 0), start_h=0, penw=1, penc='black', fillc=None)
- t - the turtle object to draw the starlength - diagonal length; default 50points - number of points in the star, odd integer >=5; default 5start_pos - start position; default (0, 0)start_h - start heading; default 0 degreespenw - pensize; default 1penc - pencolor; ; default “black”fillc - fillcolor; default None

star(t, length=300, points=7, start_pos=(-100, 0), start_h=0, penw=3, penc="grey90", fillc="yellow")
draws a 9 pointed star at (x=-100, y=0) with a grey90 pencolor, a yellow fillcolor, using a pensize of 3.Code Completion: star definition
Complete the code to draw a 9 pointed star at (x=-100, y=0) with a grey90 pencolor, a yellow fillcolor, using a pensize of 3, by replacing the XXXs.
import turtle
def star(t, length=50, points=5, start_pos=(0, 0), start_h=0, penw=1, penc="black", fillc=None):
ang = (360 * ((points-1)/2))/points
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
t.pensize(penw)
t.pencolor(penc)
if fillc is not None:
t.fillcolor(fillc)
t.begin_fill()
for _ in range(points):
t.fd(length)
t.lt(ang)
if fillc is not None:
t.end_fill()
s = turtle.Screen()
s.bgcolor("white")
s.title("star")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(9)
star(t, length=XXX, points=XXX, start_pos=(XXX), start_h=0, penw=XXX, penc="XXX", fillc="XXX")
t.ht()
s.exitonclick()
Completed code to draw a 9 pointed star at (x=-100, y=0) with a grey90 pencolor, a yellow fillcolor, using a pensize of 3.
import turtle
def star(t, length=50, points=5, start_pos=(0, 0), start_h=0, penw=1, penc="black", fillc=None):
ang = (360 * ((points-1)/2))/points
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
t.pensize(penw)
t.pencolor(penc)
if fillc is not None:
t.fillcolor(fillc)
t.begin_fill()
for _ in range(points):
t.fd(length)
t.lt(ang)
if fillc is not None:
t.end_fill()
s = turtle.Screen()
s.bgcolor("white")
s.title("star")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(9)
star(t, length=300, points=7, start_pos=(-100, 0), start_h=0, penw=3, penc="grey90", fillc="yellow")
t.ht()
s.exitonclick()
7.5. Practice Questions
Exercises
Draw a 5 pointed star with an orange fill colour.
Draw a 7 pointed star with a red pen and gold2 fill colour.
Draw a random number, between 10 and 20, of 5 pointed stars, of random size, between 10 and 100, at random positions.