Build a falling-stars game with many turtles, and tidy it step by step: from separate variables to lists, dictionaries and classes.
Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.
Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.
Strategies for getting unstuck: Say what it should do, Test a small piece, Read the error, Trace it. A student who is stuck can try one before asking for a hint. The optional course How you learn best introduces them, and a few lesson decks have a short teach, show or nudge slide for one. (Each lesson page links to the cards from its first hint.)
Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)
Many turtles at once, then lists and dictionaries to keep track of them.
Turtle() makes another turtle. Give it a name, star = Turtle(), and put the name and a dot in front of a command to give the command to that turtle: star.forward(50). Each turtle is an object: it keeps its own position, heading and colour. A command with no name in front goes to the default turtle, the one students used in the turtle course.
Once there are many objects, the program needs a way to keep track of them:
s1, s2, s3) work for three and cost the same block of code for every new one.{"turtle": s, "speed": 5, "points": 1}, read as star["speed"]. A list of dictionaries is a table: one row per star.Lists side by side are like a class roll and a separate list of phone numbers, matched by row. They work until someone joins the class and only one list is updated. A dictionary is a contact card: the name and the number stay on the same card.
s1.range(4) to range(8) and run it."colour": "orange" to the dictionary and colour each star with star["turtle"].color(star["colour"]).Cheat sheet: Open to print (A4, with a picture of what each command draws) · Open in Print · Markdown
Make your own kind of object with class, then give it methods: one line adds another star.
A class describes a new kind of object. class Star: says what every star stores and what every star can do. Star(100) makes one star; make four and you have four separate stars from one pattern.
__init__ (say "init") runs each time a star is made. Inside it, self is the star being made, and self.speed = 5 stores a speed in that star.star.speed, like a turtle's own commands.self as its first parameter: def move(self):. star.move() runs it for that star. Methods put each kind of object's behaviour in one place.This course stops there: no inheritance, getters and setters, or class diagrams.
star.move() and rock.touches(player) say what they mean.A class is a cookie cutter and each object is a cookie: same shape, but each one can have its own icing (its own values). Methods are like a vending machine's buttons: you press "give drink" and the machine knows how.
Where it breaks: the cookie cutter does not make cookies by itself. A class does nothing until Star(...) makes an object, and its methods do nothing until they are called.
Pet("Rex", 4) to another pet and print its name.self. from self.speed = ... in Lesson 4's game and run it: read the AttributeError.print("caught") inside a new method catch(self) and call it when a star is caught.self is the name for "the star this method was called on".__init__? Python's names for special methods have them. __init__ is the one Python runs when an object is made.Cheat sheet: Open to print (A4, with a picture of what each command draws) · Open in Print · Markdown
Self-paced briefs students can pick up (after the lesson named on each, where one is named). Students tick their own milestones and claim a badge; the page lights the skills their code uses. Badges stay in the student's browser: ask them to press Show your teacher. Open the projects page
Hold a turtle race: line up some turtles, move each one a random amount every turn, and stop when one crosses the finish line. Then say who won.
Milestones:
for i in range(4):
t = Turtle()
t.shape("turtle")
t.color(colours[i])
t.penup()
t.goto(-200, 90 - i * 60)
t.pendown()
t.pensize(4)
racers.append(t)for r in racers:
r.forward(random.randint(5, 20))winner = None
while winner == None:
for r in racers:
r.forward(random.randint(5, 20))
if r.xcor() > 180 and winner == None:
winner = rBuilds on Many turtles (a turtle each) and Lists of stars (one loop over them all). The stopping rule is the hard part: students who check for a winner after the whole loop finish one turn late, which is a good conversation about where a check goes. winner == None is a recipe here; None comes properly in Deeper Python. The turtle shape always draws in the same green, so the example gives each racer a coloured trail; circles take a colour if students want coloured racers.
from turtle import *
import random
speed(0)
penup()
goto(180, 120)
pendown()
right(90)
forward(240) # the finish line
hideturtle()
colours = ["red", "blue", "green", "orange"]
racers = []
for i in range(4):
t = Turtle()
t.shape("turtle")
t.color(colours[i])
t.penup()
t.goto(-200, 90 - i * 60)
t.pendown()
t.pensize(4)
racers.append(t)
winner = None
while winner == None:
for r in racers:
r.forward(random.randint(5, 20))
if r.xcor() > 180 and winner == None:
winner = r
sign = Turtle()
sign.hideturtle()
sign.penup()
sign.goto(150, winner.ycor() + 18)
sign.write("Winner!", False, "left", ("Arial", 14, "bold"))
Fill an aquarium with fish that swim by themselves. Each fish is an object made from a class you write, with its own colour and speed and a method that moves it.
Milestones:
class Fish:
def __init__(self, colour, y):
self.turtle = Turtle()
self.turtle.shape("circle")
self.turtle.color(colour)
self.turtle.penup()
self.turtle.goto(random.randint(-200, 200), y)
self.speed = random.randint(2, 6)def swim(self):
self.turtle.forward(self.speed)
if self.turtle.xcor() > 230 or self.turtle.xcor() < -230:
self.turtle.left(180)def tick():
for f in tank:
f.swim()
ontimer(tick, 50)The same shape as Stars that move themselves: a class with __init__ and one method, a list of objects, and a timer that calls the method on each. Ask students where they'd change how every fish swims (the method) and where they'd add one more fish (the list).
from turtle import *
import random
bgcolor("lightblue")
hideturtle()
class Fish:
def __init__(self, colour, y):
self.turtle = Turtle()
self.turtle.shape("circle")
self.turtle.color(colour)
self.turtle.penup()
self.turtle.goto(random.randint(-200, 200), y)
self.speed = random.randint(2, 6)
def swim(self):
self.turtle.forward(self.speed)
if self.turtle.xcor() > 230 or self.turtle.xcor() < -230:
self.turtle.left(180)
tank = []
for i in range(5):
tank.append(Fish(random.choice(["orange", "gold", "red", "purple"]), -150 + i * 70))
def tick():
for f in tank:
f.swim()
ontimer(tick, 50)
tick()
mainloop()