Stage 7: Build the Game Rules
Keep both tabs open all week. Open in a new tab — don’t use the buttons in this page to leave the course.
a score, a falling object, a way to catch it, and a way to win
how rules turn a moving thing into a real game
the AI Collector Game in its first playable form
Show the room — slowly, this is the most code-heavy stage:
- Open your Stage 6 project. The cat moves but doesn't do anything.
- Make a score variable. Show how it appears on the stage.
- Add a star sprite. Show its falling script. Show the catch detection (touching the cat → +1 score, hide, fall again).
- Add a timer variable. Show it counting down. When it hits 0, show "YOU WIN" if score is high enough, else "TRY AGAIN."
- Play one round in front of the room. Score, catch, win.
The big idea
A sprite that moves is a toy. A toy plus rules is a game.
Today we add the rules: a score that goes up when the cat catches things, a falling object that the cat has to catch, and a timer that ends the game with a win or a try-again. Without rules, the cat just moves around forever. With rules, every move matters.
cat moves (AI controls)
↓
a star falls from the top
↓
cat touches star → score goes up + new star drops
↓
timer counts down to 0
↓
enough score? → YOU WIN
not enough? → TRY AGAIN
The new tool today is the variable — a name we give to a piece of game state. Score is a variable. Timer is a variable. They live outside any single sprite — the whole game can see them and change them. (Python Arcade kids: yes, this is exactly the same idea as score and lives in your Trinket.)
- variable
- a name we give to a number the game remembers
- score
- the number that goes up when you do the goal
- timer
- a number that counts down to end the round
- win condition
- the rule that says when the player wins
- touching
- Scratch's way to check if two sprites overlap
Your Stage 6 project should be open with a sprite that moves up/down/sideways based on your hand sign.
Build it
Step 1 — Make a score variable
In the Variables category, click Make a Variable. Name it:
score
Click OK. The variable appears in the palette, and a small score readout appears at the top-left of the stage. That's your scoreboard.
At the top of your cat's script (just below when 🟢 clicked, before the forever loop), drag in set score to 0. That makes sure the score starts fresh every round.
Step 2 — Add a falling star sprite
In RAISE Playground, click the Choose a Sprite button (bottom-right). Pick a Star sprite, or any object that looks like a "collectable" thing.
The star now has its own script area — separate from the cat. Add this script to the star:
when 🟢 clicked
forever
go to x: (pick random -200 to 200) y: 180
repeat until (touching cat?) or (y position < -180)
change y by -5
if (touching cat?) then
change score by 1
Read it out loud:
- Start at the top of the screen at a random X position.
- Fall down 5 pixels at a time, until you either touch the cat or fall off the bottom.
- If the cat caught you, add 1 to the score.
- Either way, jump back to the top and fall again.
Step 3 — Test catching
Click the green flag. The star should fall. Move your hand to position the cat under it. When the cat touches the star, your score readout should bump up.
If catching works for a couple of stars, you've built a game.
Step 4 — Add a timer
Make another variable, this time called:
timer
You'll need one new sprite that only manages the timer — or you can add to the cat's script. We'll add it to the cat's script.
At the top of the cat's script (right after set score to 0), add:
set timer to 30
That's a 30-second round.
Inside the cat's forever loop, after your four if-blocks, add:
wait 1 second
change timer by -1
The wait+change pair counts down one second at a time. The timer readout (since you made it a variable) appears on the stage.
Step 5 — Add a win condition
After the forever loop... wait — forever loops never end. We need to stop the loop when the timer hits zero.
Change the forever block to repeat until (a different Control block). The condition is timer = 0. So the loop now reads:
repeat until (timer = 0)
... all the if-blocks and timer countdown ...
After the repeat-until ends, add:
if (score > 5) then
say "YOU WIN" for 3 seconds
else
say "TRY AGAIN" for 3 seconds
A score of 5 in 30 seconds is a fair goal. You can tune that number after you playtest.
Click the green flag. Catch as many stars as you can in 30 seconds. When the timer ends, the cat tells you whether you won.
Understand it
A variable is just a labeled box. The label score points at a box. Inside the box is a number. We can read it ("is the score over 5?"), change it ("add 1 to score"), or reset it ("set score to 0"). The number doesn't belong to any sprite — the cat, the star, and the future sprites all share access to score.
The reason we changed forever into repeat until is the same lesson from Python Arcade Stage 7: a game needs a way to end. A forever loop is for when the game runs as long as the program does. A repeat until is for when there's a condition (timer = 0, lives = 0, level finished) that exits the loop. Same idea, different name.
The touching check is how the game knows the cat caught the star. Two sprites overlap → touching is true. The check happens many times per second inside the star's loop, so the catch feels instant.
A win condition is the most important rule in any game. Without it, players don't know what they're trying to do. "Catch 6 stars in 30 seconds" is a clear sentence — a 4-year-old could repeat it. A parent watching the demo will get it immediately. Clear goals = good design.
Try this
Try this
Three short experiments. Predict before you run, then test your guess.
Change the win condition to score > 100 and run a 30-second round. Predict: how does the game feel now? Possible to win? Fair?
Comment out the change score by 1 line (or set it to 0). The catch detection still works, but nothing happens. Is it still a game? Is it still fun?
Stage 8 lets you customize the look of your game — different sprites, different background, sounds. If you change the cat to a basketball hoop and the star to a basketball, what kind of game does the same code become?
Test your stage
- The score variable starts at 0 and goes up when the cat catches the star.
- The star falls from the top, gets caught, and restarts at a random X position.
- The timer counts down from 30 to 0.
- When the timer hits 0, the game shows "YOU WIN" or "TRY AGAIN" based on your score.
- You can play a full 30-second round without anything crashing.
- Design check. Play three rounds in a row. Is the goal (
score > 5) too easy, too hard, or about right?
If it breaks
- The star falls forever and never restarts. The
repeat until (touching cat?) or (y position < -180)line probably has a typo. The "or" is an Operators block — drag it in fresh and check both sides. - The score goes up but never stops. The
if (touching cat?)inside the star's loop runs every frame while they're touching. Add ahideand a smallwait 0.1after thechange score by 1to debounce. Or simpler: move the star to a different position immediately after the catch. - The timer never reaches 0. Three suspects. First, did you change forever to repeat-until? Second, is
change timer by -1inside the loop? Third, isset timer to 30at the start of the script, not inside the loop? - The "YOU WIN" message never appears. The if-else needs to be after the repeat-until block, not inside it. Drag it down so it sits below.
- The cat ignores the AI now. When you added the timer countdown, you may have broken the AI prediction chain. Make sure the four if-blocks for AI control are still inside the loop, and the
wait 1 secondis after them, not before.
This is the longest, most code-heavy stage of the week. Plan a snack break at the 45-minute mark — ideally right after Step 3 (when the catching first works, which is a natural high).
The single most confusing concept for kids 7–9 is variables exist outside sprites. The star changes a number that the cat can read. Reinforce: "The score is the whole room's number, not the cat's number."
Common bug: when adding the timer, campers accidentally move the AI-control if-blocks out of the loop. Walk the room before they hit Step 5 and confirm the if-blocks are still inside the repeat-until.
Hard stretch (bad objects) is the core of the AI Collector Game. If campers are running fast, push them into this stretch even if Step 5 isn't perfect — the bomb mechanic is what makes the game shine for the parent demo.
If a camper's score-target feels too hard, lower the threshold. If it feels too easy, raise it. This is Stage 9's whole job (tuning), but a quick adjustment today saves frustration tomorrow.