Students make extra turtles with Turtle(), give each one commands by name, and play a catch-the-stars game built from three separate star variables, so they can count what each star costs in code.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 15 min
Read the unit primer first. a = Turtle() makes a new turtle; a.forward(100) moves only a. Commands with no name in front (forward, goto, xcor()) go to the default turtle, the one from the turtle course. As in real Python, the default turtle only appears once such a command uses it, which is why the Predict program shows two turtles, not three. In the game the student is the default turtle; the stars are s1, s2 and s3; pen is a hidden turtle that writes the score. Keys need the picture clicked first.
Seeing that every command needs the turtle's name and a dot. When the wrong turtle moves, ask whose name is in front of that line.
| What you will see | A question to ask | The fix |
|---|---|---|
| An extra turtle appears in the middle, or the player moves when a star should. | Does every command for a star have the star's name and a dot in front? | Add the name: s4.forward(5), not forward(5). |
| The arrow keys do nothing. | Did you click the picture after pressing Run? | Click the picture once, then use the arrow keys. |
| NameError: name 's4' is not defined. | Is s4 made (s4 = Turtle()) before tick uses it? | Put the lines that make s4 with the other stars, above the functions. |
One possible solution for each task. Other correct programs are fine.
from turtle import *
import random
speed(0)
penup()
goto(0, -180)
left(90)
score = 0
pen = Turtle()
pen.hideturtle()
pen.penup()
pen.goto(-220, 200)
s1 = Turtle()
s1.shape("circle")
s1.color("gold")
s1.penup()
s1.goto(-120, 200)
s1.setheading(270)
s2 = Turtle()
s2.shape("circle")
s2.color("blue")
s2.penup()
s2.goto(0, 200)
s2.setheading(270)
s3 = Turtle()
s3.shape("circle")
s3.color("gold")
s3.penup()
s3.goto(120, 200)
s3.setheading(270)
def show_score():
pen.clear()
pen.write("Score: " + str(score), False, "left", ("Arial", 16, "normal"))
def go_left():
goto(xcor() - 25, ycor())
def go_right():
goto(xcor() + 25, ycor())
def tick():
global score
s1.forward(4)
if s1.distance(xcor(), ycor()) < 25:
score += 1
s1.goto(random.randint(-200, 200), 200)
if s1.ycor() < -220:
s1.goto(random.randint(-200, 200), 200)
s2.forward(9)
if s2.distance(xcor(), ycor()) < 25:
score += 1
s2.goto(random.randint(-200, 200), 200)
if s2.ycor() < -220:
s2.goto(random.randint(-200, 200), 200)
s3.forward(3)
if s3.distance(xcor(), ycor()) < 25:
score += 1
s3.goto(random.randint(-200, 200), 200)
if s3.ycor() < -220:
s3.goto(random.randint(-200, 200), 200)
show_score()
ontimer(tick, 50)
onkey(go_left, "Left")
onkey(go_right, "Right")
listen()
show_score()
tick()
mainloop()
from turtle import *
import random
speed(0)
penup()
goto(0, -180)
left(90)
score = 0
pen = Turtle()
pen.hideturtle()
pen.penup()
pen.goto(-220, 200)
s1 = Turtle()
s1.shape("circle")
s1.color("gold")
s1.penup()
s1.goto(-120, 200)
s1.setheading(270)
s2 = Turtle()
s2.shape("circle")
s2.color("gold")
s2.penup()
s2.goto(0, 200)
s2.setheading(270)
s3 = Turtle()
s3.shape("circle")
s3.color("gold")
s3.penup()
s3.goto(120, 200)
s3.setheading(270)
s4 = Turtle()
s4.shape("circle")
s4.color("gold")
s4.penup()
s4.goto(60, 200)
s4.setheading(270)
def show_score():
pen.clear()
pen.write("Score: " + str(score), False, "left", ("Arial", 16, "normal"))
def go_left():
goto(xcor() - 25, ycor())
def go_right():
goto(xcor() + 25, ycor())
def tick():
global score
s1.forward(4)
if s1.distance(xcor(), ycor()) < 25:
score += 1
s1.goto(random.randint(-200, 200), 200)
if s1.ycor() < -220:
s1.goto(random.randint(-200, 200), 200)
s2.forward(6)
if s2.distance(xcor(), ycor()) < 25:
score += 1
s2.goto(random.randint(-200, 200), 200)
if s2.ycor() < -220:
s2.goto(random.randint(-200, 200), 200)
s3.forward(3)
if s3.distance(xcor(), ycor()) < 25:
score += 1
s3.goto(random.randint(-200, 200), 200)
if s3.ycor() < -220:
s3.goto(random.randint(-200, 200), 200)
s4.forward(5)
if s4.distance(xcor(), ycor()) < 25:
score += 1
s4.goto(random.randint(-200, 200), 200)
if s4.ycor() < -220:
s4.goto(random.randint(-200, 200), 200)
show_score()
ontimer(tick, 50)
onkey(go_left, "Left")
onkey(go_right, "Right")
listen()
show_score()
tick()
mainloop()
from turtle import *
import random
speed(0)
penup()
goto(0, -180)
left(90)
score = 0
pen = Turtle()
pen.hideturtle()
pen.penup()
pen.goto(-220, 200)
s1 = Turtle()
s1.shape("circle")
s1.color("gold")
s1.penup()
s1.goto(-120, 200)
s1.setheading(270)
s2 = Turtle()
s2.shape("circle")
s2.color("gold")
s2.penup()
s2.goto(0, 200)
s2.setheading(270)
s3 = Turtle()
s3.shape("circle")
s3.color("gold")
s3.penup()
s3.goto(120, 200)
s3.setheading(270)
rock = Turtle()
rock.shape("square")
rock.color("gray")
rock.penup()
rock.goto(-60, 200)
rock.setheading(270)
def show_score():
pen.clear()
pen.write("Score: " + str(score), False, "left", ("Arial", 16, "normal"))
def go_left():
goto(xcor() - 25, ycor())
def go_right():
goto(xcor() + 25, ycor())
def tick():
global score
s1.forward(4)
if s1.distance(xcor(), ycor()) < 25:
score += 1
s1.goto(random.randint(-200, 200), 200)
if s1.ycor() < -220:
s1.goto(random.randint(-200, 200), 200)
s2.forward(6)
if s2.distance(xcor(), ycor()) < 25:
score += 1
s2.goto(random.randint(-200, 200), 200)
if s2.ycor() < -220:
s2.goto(random.randint(-200, 200), 200)
s3.forward(3)
if s3.distance(xcor(), ycor()) < 25:
score += 1
s3.goto(random.randint(-200, 200), 200)
if s3.ycor() < -220:
s3.goto(random.randint(-200, 200), 200)
rock.forward(5)
if rock.distance(xcor(), ycor()) < 25:
score -= 3
rock.goto(random.randint(-200, 200), 200)
if rock.ycor() < -220:
rock.goto(random.randint(-200, 200), 200)
show_score()
ontimer(tick, 50)
onkey(go_left, "Left")
onkey(go_right, "Right")
listen()
show_score()
tick()
mainloop()
Stretch: Give the rock its own speed that increases a little every time it is sent back to the top.
Support: Give students the six lines that make the rock, and ask them to add only its lines in tick.
a = Turtle(), then forward(50). Which turtle moves?
The default turtle, not a: forward(50) has no name in front. a.forward(50) would move a.
Next lesson keeps the stars in lists, so one loop moves them all and adding a star is one number. Real games (Arcade sprites, microStudio, every game engine) are many objects like these, each with its own position and behaviour.