Build a game that uses everything so far. The turtle hops along a path; catch it before it hides.
Every game is a few rules, some data and a loop. You already know all three.
The turtle hops around a path. You will turn it into a game: catch the turtle before it hides, score as many as you can in 30 seconds. You will build it in three milestones, with less help each time.
Behind the code: the turtle is both the target and the pen. penup() stops it drawing lines, and speed(0) makes every move instant.
Make your prediction first. Help opens once you've locked it in.
from turtle import *
speed(0)
penup()
xs = [-120, 0, 120, 120, 0, -120]
ys = [60, 110, 60, -50, -100, -50]
place = 0
def hop():
global place
goto(xs[place], ys[place])
place = (place + 1) % len(xs)
ontimer(hop, 1000)
hop()
mainloop()
hop() moves the turtle to the next spot in the two lists, then asks the timer to run hop again in 1000 milliseconds.
Run it and watch a full lap. Press Stop when you are done.
from turtle import *
speed(0)
penup()
xs = [-120, 0, 120, 120, 0, -120]
ys = [60, 110, 60, -50, -100, -50]
place = 0
def hop():
global place
goto(xs[place], ys[place])
place = (place + 1) % len(xs)
ontimer(hop, 1000)
hop()
mainloop()
from turtle import *
speed(0)
penup()
xs = [-120, 0, 120, 120, 0, -120]
ys = [60, 110, 60, -50, -100, -50]
place = 0
def hop():
global place
goto(xs[place], ys[place])
place = (place + 1) % len(xs)
ontimer(hop, 1000)
hop()
mainloop()
Follow place through a lap.
How would you add a spot?
The score from Events, with one line missing.
Fill in place after each hop. The lists have 6 spots.
| hop | place used | place after |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 1 | |
| 6 | 5 | |
| 7 | 1 |
Every click should add a point, but the first click stops with an error. Click the line where it goes wrong.
The turtle now remembers where it is in tx and ty. Fill in clicked so that a click on the turtle adds a point. Run it and try to catch the turtle.
Use the same check as Build a button: if inside(x, y, tx - 20, ty - 20, 40, 40):
Inside the if: score = score + 1, then show_score().
Click the turtle to score.
Run your program. This ticks when your code does all of the above.
Too easy? Now the turtle only shows for 0.7 seconds. showing is True while it is there and False when it is gone. Follow the three comments that start with Milestone 2.
In hop: showturtle(), then showing = True, then ontimer(hide, 700).
In hide: if showing: then misses = misses + 1, showing = False and hideturtle().
In clicked: add showing to the if (if showing and inside(...)), and inside it set showing = False and hideturtle().
The turtle blinks: it shows, then hides, and a missed turtle counts.
Run your program. This ticks when your code does all of the above.
Make it a real game: a Start button, a 30 second timer, and a score that resets each game. The comment at the top lists the five parts. If you get stuck, the hints show one part at a time.
Start with time_left = 30 and playing = False. Every function that changes them needs them in its global line. Show the time like the score, at goto(150, 160).
In clicked: if playing == False and inside(x, y, -60, -180, 120, 40): start(). Draw the Start button only when playing == False.
tick(): if playing: take 1 from time_left; if it reaches 0 set playing = False, else ontimer(tick, 1000).
A Start button, a 30-second timer, and the game stops at 0.
Run your program. This ticks when your code does all of the above.
Play a few games. Do you start moving to the next spot before the turtle arrives? Then try random spots instead. random is a module someone else wrote, just like your ui.py. random.randint(-150, 150) picks a whole number from -150 up to 150, a new one each time it runs, so the turtle lands somewhere new on every hop. The first number is the lowest it can pick and the second is the highest. Swap the first two lines of hop() for the two lines above. Copy import random to the top and random. before randint as they are for now; a later lesson explains how they work. Then change the numbers so the turtle only appears in the top half of the screen.
import random
# Random spots instead of the path: replace the first two lines of hop() with these.
tx = random.randint(-150, 150)
ty = random.randint(-120, 120)
The final challenge. Students build a game in three milestones, each with less help, using everything from the course; the turtle hops along a path and players catch it before it hides. About 60 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Play the finished game (the Milestone 3 solution below) for a minute, then read the three milestone starters. Milestone 1 gives most of the code, milestone 2 an outline, milestone 3 only the goals: that fading support is deliberate. The two new words are True and False: a variable like showing remembers whether the turtle is on screen right now. Finishing this lesson and Power-ups marks the course "Challenge complete".
Every variable a function changes needs global, and % wraps the path. When the score doesn't change, ask which variables the function changes.
Stretch: Make the path draw a picture (each spot a corner of a shape) and reveal it with the pen down at game over.
Support: Pair students for Milestone 3, one reading the goals and the other typing, swapping after each goal.
Power-ups (next) adds lives, a status bar, a gold turtle and an upgrade. Then two optional lessons: Control panel (towards Gradio and web pages) and Game levels (towards MakeCode Arcade and microStudio). Creative option: students design their own path, such as their initials.