Make the reaction game harder. Decoys you must not press, a list of target times that get tighter each level, and three lives.
Arcade games keep you playing by getting harder as you improve, and by trying to trick you. Both are a few rules on top of the game you already built.
This lesson goes further than the Reaction timer, for one player. You will add three rules, one milestone each:
Fake-outs: sometimes a cross appears instead of the heart. Do not press!
Levels: each level has a target time to beat, kept in a list, and the targets get tighter.
Lives: three mistakes and it is game over.
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Predict
Show as code
levels.ts
input.onButtonPressed(Button.B, function () {
basic.clearScreen()
basic.pause(randint(1000, 3000))
if (randint(0, 2) == 0) {
basic.showIcon(IconNames.No)
} else {
basic.showIcon(IconNames.Heart)
}
})
RunRun it and compare the result with your prediction.
Run
Press B about ten times. Count the crosses.
Show as code
levels.ts
input.onButtonPressed(Button.B, function () {
basic.clearScreen()
basic.pause(randint(1000, 3000))
if (randint(0, 2) == 0) {
basic.showIcon(IconNames.No)
} else {
basic.showIcon(IconNames.Heart)
}
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
InvestigateTrace how each line of code connects to what you see.
Investigate
The game keeps one target per level in a list. A+B shows the target for the current level.
Show as code
levels.ts
let level = 0let targets = [800, 600, 450, 350, 300]
input.onButtonPressed(Button.AB, function () {
basic.showString("Beat")
basic.showNumber(targets[level])
})
Look up the target for each level.
What happens when the list runs out?
targets is 800, 600, 450, 350, 300. What must you beat at each level?
level
target to beat (ms)
0
2
4
ModifyChange the code on purpose and check the result.
Milestone 1: fake-outs
decoy is true while the cross is showing. Make A catch you out when you press during a cross: add else if decoy and show a skull.
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?
decoy is already set to true when the cross shows. In A, add else if decoy.
Another hint
Inside it: set decoy to false and show a skull (caught out).
Pressing A while the cross shows catches you out.
This ticks by itself when your blocks do what is asked.
ModifyChange the code on purpose and check the result.
Milestone 2: levels
Each good press must beat targets[level] to go up a level. Show the new level when you go up, and a sad face when you are too slow.
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?
After working out reaction: if reaction < get value at level from targets.
Another hint
If so: change level by 1 and show it. Else: show a sad face.
Beat the target for your level to go up a level.
This ticks by itself when your blocks do what is asked.
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
Milestone 3: lives
Finish the game:
You start with 3 lives. Pressing on a cross or being too slow loses one.
At 0 lives: show Game over and your level, then start again.
Beat the last target: you are the champion.
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?
A variable lives starts at 3. Change it by -1 for a press on a cross, and for a press that is too slow.
Another hint
At the end of A: if lives = 0, show Game over and the level, then reset level and lives. When level reaches the length of targets, show Champion.
Three lives, game over at zero, and a win after the last level.
This ticks by itself when your blocks do what is asked.
ModifyChange the code on purpose and check the result.
Make it yours
Change the rules to make it your game. Ideas: more levels, a different chance of a fake-out, a shorter wait at higher levels, or a sound for each level.
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
For teachers
An optional, harder extension of the Reaction timer. Students add decoy signals, levels with target times kept in a list, and lives, managing several pieces of state at once. About 60 minutes.
I can use two true/false variables to tell different moments apart.
I can use a variable as the place in a list, so a rule gets harder.
I can manage lives, levels and an end to the game.
Before class
This lesson is optional: it is for students who finished the Reaction timer and Radio duel and want a harder challenge. It needs nothing new, only more of the same ideas at once. A decoy (a cross instead of the heart) appears about one round in three (pick random 0 to 2 = 0); a second true/false variable, decoy, remembers that moment. Levels use a list of target times, targets: the current level is the place in the list, so reaction < targets[level] gets harder as the level rises. When the level reaches the length of the list, the player has won. Lives count mistakes.
Running the lesson
Predict: about one in three. Ask why 0 to 2 gives a third.
Investigate: the list holds the rules of the game. The last question previews Milestone 3's win condition.
Milestone 1: else if decoy. Read A aloud: 'if waiting, time it; else if a cross is showing, caught out; else too early'.
Milestone 2: targets[level] is the key idea: a variable chooses the place in the list.
Milestone 3: goals only. Encourage students to plan on paper first: when do lives go down, when does the game end?
Make it yours: let students swap games and play each other's.
The hard part (on purpose)
Which check comes first in the A button, and a list of targets by level. Ask them to say the order of the questions before testing.
When students get stuck
Pressing on a cross shows the too-early cross, not the skull. Ask: Which is checked first in A: waiting, decoy, or else? Fix: Order: if waiting … else if decoy … else.
The level goes up even when slow. Ask: Which way round is the comparison? Fix: Use reaction < targets[level]: faster than the target.
Something goes wrong after the last level. Ask: Which places exist in targets? Fix: When level equals the length of targets, the player has won: show Champion and start again.
Stretch and support
Stretch: Make the decoy chance grow with the level: pick random 0 to (4 − level), so higher levels trick you more often.
Support: Students who find this heavy can stop after Milestone 2; the levels alone make a good game.
Where it leads
The other optional lesson, Tournament, takes the Radio duel further with a scoreboard list. Class ideas: a class high-level board; students design a sixth level and argue what its target should be.
What each step is for
Investigate: A list can hold the rules of a game: one target per level, found by using the level number as the place.
Milestone 1: fake-outs: A second true/false variable remembers a second kind of moment, and else if tells the moments apart.
Milestone 2: levels: Comparing with a list item, chosen by a variable, lets the same rule get harder as the variable grows.
Milestone 3: lives: Writing the end of the game from goals alone shows whether a student can manage several pieces of state at once.
Now you can
Use two true/false variables to tell different moments apart.
Use a variable as the place in a list, so a rule gets harder.