← Arcade advanced skills

True or false

A variable that holds true or false remembers whether a power-up is on, so an overlap can ask about it.

A mushroom in Mario, a power pellet in Pac-Man, a shield in a space shooter: each one is a variable that is true for a while.

A variable can hold true or false (in Logic). In this game shield starts false. Catching a yellow star sets it to true. When a rock hits, if shield asks whether it is true right now: true smashes the rock for 5 points, false costs a life.

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
lesson.ts
let rock: Sprite = null
let star: Sprite = null
let shield = false
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    otherSprite.destroy()
    if (shield) {
        info.changeScoreBy(5)
    } else {
        info.changeLifeBy(-1)
    }
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
    otherSprite.destroy()
    shield = true
    scene.setBackgroundColor(8)
})
scene.setBackgroundColor(15)
let ship = sprites.create(img`
    8 8 . . . . . .
    8 9 9 9 9 . . .
    8 9 1 1 9 9 9 9
    8 9 9 9 9 . . .
    8 8 . . . . . .
    `, SpriteKind.Player)
ship.setPosition(20, 60)
controller.moveSprite(ship, 0, 100)
ship.setStayInScreen(true)
info.setLife(3)
game.onUpdateInterval(1000, function () {
    rock = sprites.create(img`
        . e e e .
        e e d e e
        e d e e e
        . e e e .
        `, SpriteKind.Enemy)
    rock.setPosition(160, randint(10, 110))
    rock.vx = -50
    info.changeScoreBy(1)
})
game.onUpdateInterval(5000, function () {
    star = sprites.create(img`
        . . 5 . .
        5 5 5 5 5
        . 5 5 5 .
        . 5 . 5 .
        `, SpriteKind.Food)
    star.setPosition(160, randint(10, 110))
    star.vx = -30
})

Read the two overlap events. The background turns dark blue while the shield is on.

You catch one star. How long does the shield last?

RunRun it and compare the result with your prediction.

Run

Catch a star (one floats past every 5 seconds), then fly into the rocks. Is this power-up fair?

The program as blocks
Show as code
lesson.ts
let rock: Sprite = null
let star: Sprite = null
let shield = false
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    otherSprite.destroy()
    if (shield) {
        info.changeScoreBy(5)
    } else {
        info.changeLifeBy(-1)
    }
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
    otherSprite.destroy()
    shield = true
    scene.setBackgroundColor(8)
})
scene.setBackgroundColor(15)
let ship = sprites.create(img`
    8 8 . . . . . .
    8 9 9 9 9 . . .
    8 9 1 1 9 9 9 9
    8 9 9 9 9 . . .
    8 8 . . . . . .
    `, SpriteKind.Player)
ship.setPosition(20, 60)
controller.moveSprite(ship, 0, 100)
ship.setStayInScreen(true)
info.setLife(3)
game.onUpdateInterval(1000, function () {
    rock = sprites.create(img`
        . e e e .
        e e d e e
        e d e e e
        . e e e .
        `, SpriteKind.Enemy)
    rock.setPosition(160, randint(10, 110))
    rock.vx = -50
    info.changeScoreBy(1)
})
game.onUpdateInterval(5000, function () {
    star = sprites.create(img`
        . . 5 . .
        5 5 5 5 5
        . 5 5 5 .
        . 5 . 5 .
        `, SpriteKind.Food)
    star.setPosition(160, randint(10, 110))
    star.vx = -30
})
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 program as blocks
Show as code
lesson.ts
let rock: Sprite = null
let star: Sprite = null
let shield = false
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    otherSprite.destroy()
    if (shield) {
        info.changeScoreBy(5)
    } else {
        info.changeLifeBy(-1)
    }
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
    otherSprite.destroy()
    shield = true
    scene.setBackgroundColor(8)
})
scene.setBackgroundColor(15)
let ship = sprites.create(img`
    8 8 . . . . . .
    8 9 9 9 9 . . .
    8 9 1 1 9 9 9 9
    8 9 9 9 9 . . .
    8 8 . . . . . .
    `, SpriteKind.Player)
ship.setPosition(20, 60)
controller.moveSprite(ship, 0, 100)
ship.setStayInScreen(true)
info.setLife(3)
game.onUpdateInterval(1000, function () {
    rock = sprites.create(img`
        . e e e .
        e e d e e
        e d e e e
        . e e e .
        `, SpriteKind.Enemy)
    rock.setPosition(160, randint(10, 110))
    rock.vx = -50
    info.changeScoreBy(1)
})
game.onUpdateInterval(5000, function () {
    star = sprites.create(img`
        . . 5 . .
        5 5 5 5 5
        . 5 5 5 .
        . 5 . 5 .
        `, SpriteKind.Food)
    star.setPosition(160, randint(10, 110))
    star.vx = -30
})

Trace a rock hitting the ship.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Fill in what happens when a rock hits the ship.

shieldlives beforelives afterscore change
false3
true3
false1

Which needs true or false, and which needs a number?

Is the shield on?
How many lives are left?
Has the player found the key?
How many shots are left?

Tom's shield smashes every rock for the rest of the game after one star. What is missing?

ModifyChange the code on purpose and check the result.

Modify

Make the shield break after it smashes one rock.

Need a hint?

The shield should break when it smashes a rock: inside if shield, after the points.

Another hint

set shield to false, and set the background back to 15 (black).

If the shield is already on, a star is worth 3 points.

Need a hint?

In the star overlap, before shield is set to true: if shield, change score by 3.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

The shield breaks after one smash.

This ticks by itself when your blocks do what is asked.

A star caught while the shield is already on is worth 3 points.

This ticks by itself when your blocks do what is asked.

InvestigateTrace how each line of code connects to what you see.

Mix it up

colours is 9, 4, 2, 7 and level is 2. What number does colours[level] give?

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

A ghost chases you. A power pellet appears every 6 seconds. Make it a power-up:

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 new variable, hunting, set to false in on start.

Another hint

The pellet overlap: destroy the pellet, set hunting to true, and change the background.

Another hint

The ghost overlap: if hunting, score 5, set hunting to false and put the background back; else lose a life. Either way, send the ghost to the corner.

A power pellet: catch it and the next ghost you touch is worth 5 points, not a life.

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Change a key and a door

Find the key, then reach the door. Right now the door opens with no key. Use hasKey so the door only opens if you have the key, and if you don't, splash "Locked!" and move the explorer back a step (or the splash comes back every frame).

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

Students use a true or false variable to remember whether a shield is on, see that it stays true until a block sets it back, and build a power pellet. About 45 minutes.

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

Students will be able to

Before class

A variable can hold true or false (Logic). A power-up is three parts: a variable that remembers it (shield), an event that changes it (catching a star sets it to true) and a rule that uses it (if shield in the rock overlap).

The misconception: a true or false variable stays as it is until a block changes it, so a shield set once lasts for ever. Modify fixes it the simplest way (break after one smash); next lesson makes it run out with a countdown.

Running the lesson

  1. Predict and Run: the shield never ends. Ask: is this fair? What would make it fair?
  2. Investigate: the match separates yes-or-no from how many.
  3. Modify: one smash needs set shield to false inside the if.
  4. Mix it up: lists from last lesson.
  5. Make: ask students to name the variable, the event and the rule before they start.

The hard part (on purpose)

Setting it back. When a power-up never ends, ask which block sets it to false.

When students get stuck

Stretch and support

Stretch: Two pellets in a row: count the ghosts eaten while hunting, and give 5, then 10.

Support: Give students the hunting variable and the pellet overlap, so they write only the if in the ghost overlap.

Where it leads

Next: run out, a number that counts down so a power-up lasts a set time. Skillmap: Save the Galaxy (arcade.makecode.com/skillmaps) adds power-ups to a shooter.

What each step is for

Now you can
Next lesson9. Run outA number that counts down limits a power-up, and the event checks it first, so it never goes below zero.