← Arcade Foundations

Score and lives

Give the player lives, make a hit cost one, and let the engine end the game.

Three hearts in the corner and a game that can end: that is what turns moving sprites into a game you can win or lose.

Last lesson the score went up and down, but the game never ended. Arcade's Info blocks keep a score and lives (hearts) at the top of the screen, and they end the game by themselves.

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
info.setLife(3)
let mySprite = sprites.create(img`
    . 4 4 .
    4 4 4 4
    . 8 8 .
    . 8 8 .
    `, SpriteKind.Player)
mySprite.setPosition(20, 60)
let rock = sprites.create(img`
    . e e .
    e e e e
    e e e e
    `, SpriteKind.Enemy)
rock.setPosition(160, 60)
rock.vx = -50
let rock2 = sprites.create(img`
    . e e .
    e e e e
    e e e e
    `, SpriteKind.Enemy)
rock2.setPosition(240, 60)
rock2.vx = -50
let rock3 = sprites.create(img`
    . e e .
    e e e e
    e e e e
    `, SpriteKind.Enemy)
rock3.setPosition(320, 60)
rock3.vx = -50
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    info.changeLifeBy(-1)
    otherSprite.destroy()
})

There is no game over block in this program. Lock in what you think happens.

The player has 3 lives and cannot move. Three rocks fly at it. What happens?

RunRun it and compare the result with your prediction.

Run

Watch the hearts at the top of the screen. Use the restart button under the simulator to play again.

The program as blocks
Show as code
lesson.ts
info.setLife(3)
let mySprite = sprites.create(img`
    . 4 4 .
    4 4 4 4
    . 8 8 .
    . 8 8 .
    `, SpriteKind.Player)
mySprite.setPosition(20, 60)
let rock = sprites.create(img`
    . e e .
    e e e e
    e e e e
    `, SpriteKind.Enemy)
rock.setPosition(160, 60)
rock.vx = -50
let rock2 = sprites.create(img`
    . e e .
    e e e e
    e e e e
    `, SpriteKind.Enemy)
rock2.setPosition(240, 60)
rock2.vx = -50
let rock3 = sprites.create(img`
    . e e .
    e e e e
    e e e e
    `, SpriteKind.Enemy)
rock3.setPosition(320, 60)
rock3.vx = -50
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    info.changeLifeBy(-1)
    otherSprite.destroy()
})
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

Dodge the rocks with the arrow keys and collect the heart. Lose on purpose once, too.

The program as blocks
Show as code
lesson.ts
info.setLife(3)
let mySprite = sprites.create(img`
    . 4 4 .
    4 4 4 4
    . 8 8 .
    . 8 8 .
    `, SpriteKind.Player)
mySprite.setPosition(20, 60)
controller.moveSprite(mySprite)
mySprite.setStayInScreen(true)
let heart = sprites.create(img`
    2 2 . 2 2
    2 2 2 2 2
    . 2 2 2 .
    . . 2 . .
    `, SpriteKind.Food)
heart.setPosition(80, 100)
let rock = sprites.create(img`
    . e e .
    e e e e
    e e e e
    `, SpriteKind.Enemy)
rock.setPosition(160, 30)
rock.vx = -40
let rock2 = sprites.create(img`
    . e e .
    e e e e
    e e e e
    `, SpriteKind.Enemy)
rock2.setPosition(240, 60)
rock2.vx = -40
let rock3 = sprites.create(img`
    . e e .
    e e e e
    e e e e
    `, SpriteKind.Enemy)
rock3.setPosition(320, 90)
rock3.vx = -40
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    info.changeLifeBy(-1)
    otherSprite.destroy()
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
    info.changeLifeBy(1)
    otherSprite.destroy()
})

Sam's overlap event:

The program as blocks
Show as code
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    info.changeLifeBy(-1)
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Play this game in your head. Fill in the lives after each thing happens.

what happenslives after
(start)3
a rock hits the player
a rock flies past
the player collects the heart
a rock hits the player

Sam's player touches one rock and the game is over straight away, though he started with 3 lives. Why?

Kai wants his game to end when the lives run out. What does he need to add?

ModifyChange the code on purpose and check the result.

Modify

Start the player with 5 lives.

Need a hint?

Change the 3 in set life.

Make the heart give a point as well as a life.

Need a hint?

Inside the Player–Food event, add change score by 1 (Info) next to change life by 1.

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

Start the player with 5 lives.

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

Collecting the heart also scores a point.

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

The program as blocks
Show as code
lesson.ts
let mySprite = sprites.create(img`
    . 4 4 .
    4 4 4 4
    . 8 8 .
    . 8 8 .
    `, SpriteKind.Player)
mySprite.setPosition(20, 60)
let coin = sprites.create(img`
    . 5 5 .
    5 5 4 5
    5 4 5 5
    . 5 5 .
    `, SpriteKind.Food)
coin.setPosition(150, 60)
coin.vx = -30
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
    info.changeScoreBy(1)
    otherSprite.destroy()
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    info.changeLifeBy(-1)
    otherSprite.destroy()
})
A coin of kind Food moves left towards the player at vx -30. When it reaches the player, which event runs?

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

Make

The rocks fly already. Make it a 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?

In on start: set life to 3 (Info).

Another hint

Add an on overlap for Player and Enemy. Inside: change life by -1, then destroy otherSprite.

A dodge game: 3 lives, and a rock that hits takes one life and is destroyed.

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

ModifyChange the code on purpose and check the result.

Change a dodgeball game

Balls bounce around the court; each hit costs a life. Add a fourth ball, give the player 5 lives, and make the balls a little faster.

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 give the player lives, make a hit cost one life, and see that the engine ends the game at 0 lives and that an overlap runs every frame while sprites touch. About 40 minutes.

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

Students will be able to

Before class

Info blocks keep a score and lives (hearts) at the top of the screen. When the lives reach 0, Arcade ends the game by itself. One important fact: an overlap event runs every frame (about 30 times a second) while the two sprites still touch. If the enemy is not destroyed, one touch takes life after life, and the game ends at once. Timers (a new enemy every second) are next lesson; here the rocks are made at the start and flown in with a velocity.

Running the lesson

  1. Predict: many students expect lives to go below 0, or that a game over block is needed. The engine does it. Ask the 'no game over block' group to run it and watch.
  2. Run: count the hearts aloud as they go.
  3. Investigate: Sam's dodger is the lesson's key bug: an overlap runs every frame while sprites touch.
  4. Mix it up: kinds and velocity, from earlier lessons.
  5. Make: students who forget destroy lose all three lives at once: let them find it.

The hard part (on purpose)

An overlap runs every frame while sprites touch, so one hit takes every life. Ask what happens to the enemy after it touches the player.

When students get stuck

Stretch and support

Stretch: Add a coin worth points, so there is something to chase as well as dodge.

Support: Give students the overlap event already placed, and ask them to fill in the two blocks inside.

Where it leads

Next lesson: a timer that sends a new rock every second, for as long as the game lasts.

What each step is for

Now you can
Next lesson6. TimersUse a timer to make a new enemy every second, from a random place, for as long as the game lasts.