← micro:bit: talk and play

Fake-outs and levels

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:

  1. Fake-outs: sometimes a cross appears instead of the heart. Do not press!
  2. Levels: each level has a target time to beat, kept in a list, and the targets get tighter.
  3. Lives: three mistakes and it is game over.
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

The program as blocks
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)
    }
})
Over many rounds, how often does a cross appear instead of the heart?

RunRun it and compare the result with your prediction.

Run

Press B about ten times. Count the crosses.

The program as blocks
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.

The program as blocks
Show as code
levels.ts
let level = 0
let 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?

leveltarget to beat (ms)
0
2
4

There are 5 targets. You beat level 4. What must the game do next?

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:

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.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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

  1. Predict: about one in three. Ask why 0 to 2 gives a third.
  2. Investigate: the list holds the rules of the game. The last question previews Milestone 3's win condition.
  3. Milestone 1: else if decoy. Read A aloud: 'if waiting, time it; else if a cross is showing, caught out; else too early'.
  4. Milestone 2: targets[level] is the key idea: a variable chooses the place in the list.
  5. Milestone 3: goals only. Encourage students to plan on paper first: when do lives go down, when does the game end?
  6. 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

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

Now you can
Next lesson10. TournamentTake the Radio duel further. A scoreboard kept in a list, rounds that start themselves, and a champion at three wins.