← Arcade Foundations

Timers

Use a timer to make a new enemy every second, from a random place, for as long as the game lasts.

Enemies that keep on coming, raindrops that never stop: a game that runs for minutes is made by a few blocks that run again and again.

Last lesson three rocks flew in, and then the game ran out of rocks. A timer fixes that. on game update every 1000 ms (in Game) is an event that runs its blocks again every so many milliseconds (1000 ms is one second), for as long as the game lasts.

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 mySprite = sprites.create(@IMG:mySprite@, SpriteKind.Player)
mySprite.setPosition(20, 60)
controller.moveSprite(mySprite)
game.onUpdateInterval(1000, function () {
    rock = sprites.create(@IMG:rock@, SpriteKind.Enemy)
    rock.setPosition(160, 60)
    rock.vx = -50
})

The rock is made inside the timer, not in on start.

The timer runs every 1000 ms and makes a rock each time. You watch for 5 seconds. How many rocks have appeared?

RunRun it and compare the result with your prediction.

Run

Count the rocks as they come.

The program as blocks
Show as code
lesson.ts
let rock: Sprite = null
let mySprite = sprites.create(@IMG:mySprite@, SpriteKind.Player)
mySprite.setPosition(20, 60)
controller.moveSprite(mySprite)
game.onUpdateInterval(1000, function () {
    rock = sprites.create(@IMG:rock@, SpriteKind.Enemy)
    rock.setPosition(160, 60)
    rock.vx = -50
})
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.

The program as blocks
Show as code
lesson.ts
let rock: Sprite = null
info.setLife(3)
let mySprite = sprites.create(@IMG:mySprite@, SpriteKind.Player)
mySprite.setPosition(20, 60)
controller.moveSprite(mySprite)
mySprite.setStayInScreen(true)
game.onUpdateInterval(1000, function () {
    rock = sprites.create(@IMG:rock@, SpriteKind.Enemy)
    rock.setPosition(160, randint(10, 110))
    rock.vx = -50
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    info.changeLifeBy(-1)
    otherSprite.destroy()
})

Ava's program:

The program as blocks
Show as code
let y = randint(10, 110)
game.onUpdateInterval(1000, function () {
    rock = sprites.create(img`
        . c c .
        c b b c
        c b b c
        . c c .
        `, SpriteKind.Enemy)
    rock.setPosition(160, y)
    rock.vx = -50
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

The timer runs every 500 ms. How many rocks has it made after 10 seconds?

Ava picks a random height once, in on start, and stores it in y. Every rock in her timer uses y. What does she see?

Match each timer with how often it runs.

every 1000 ms
every 500 ms
every 2000 ms

ModifyChange the code on purpose and check the result.

Modify

Make a rock come every half a second.

Need a hint?

Change 1000 in on game update every. Half a second is 500 ms.

Give each rock a random height, so they don't all come through the middle.

Need a hint?

pick random is in Math.

Another hint

Drag it into the y of set rock position, inside the timer: pick random 10 to 110.

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

Make a rock come every half a second.

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

Each rock starts at a random height.

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
info.setLife(2)
let mySprite = sprites.create(@IMG:mySprite@, SpriteKind.Player)
controller.moveSprite(mySprite)
let heart = sprites.create(@IMG:heart@, SpriteKind.Food)
heart.setPosition(120, 30)
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
    info.changeLifeBy(1)
    otherSprite.destroy()
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    info.changeLifeBy(-1)
    otherSprite.destroy()
})

The player starts with 2 lives. Fill in the lives after each thing happens.

what happenslives after
(start)2
collects a heart
a rock hits
a rock hits

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

Make

Make a 30-second dodge game (bats in a cave, asteroids, traffic):

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 and start countdown 30 (both in Info).

Another hint

Timer: on game update every 1000 ms (Game). Inside, create an Enemy, set its position to x 160 and y pick random 10 to 110, and set its vx to a negative number.

Another hint

Overlap Player with Enemy: change life by -1, and destroy otherSprite so one touch takes only one life.

A 30-second dodge game: 3 lives, a countdown of 30, an enemy every second from a random height on the right, and a touch takes one life.

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

ModifyChange the code on purpose and check the result.

Change a rain dodger

Someone is walking home in the rain without an umbrella. Make the rain heavier (more drops, falling faster), then give the walker 5 lives and a 40-second walk home.

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 timer event to make a new enemy every second, from a random height, and see that both the timer and pick random run again each time. About 45 minutes.

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

Students will be able to

Before class

on game update every 1000 ms (Game) is a timer event: it runs its blocks again every so many milliseconds (1000 ms = 1 second) for as long as the game lasts. Here it makes a new enemy each time, at x 160 (the right edge), with a negative vx so it flies left. pick random (Math) gives a new whole number each time it runs, so inside the timer each rock gets its own height. start countdown (Info) ends the game after so many seconds. Try the Make yourself: about 15 blocks.

Running the lesson

  1. Predict: some students expect one rock, because the create block appears once. It runs once per timer run. Ask the '1' group how often the timer runs.
  2. Investigate: Ava's rocks are the random-once mistake: a random number picked once is just a number.
  3. Modify: a smaller timer number means more often.
  4. Mix it up: lives, from last lesson.
  5. Make: x 160 is the right edge, negative vx flies left. Students who forget destroy lose all three lives at once.

The hard part (on purpose)

Seeing that the blocks inside a timer run again and again. Ask how many times the create block has run after five seconds.

When students get stuck

Stretch and support

Stretch: Make the game a win if you survive: add on countdown end (Info) with game over (win).

Support: Give students the timer block with the enemy already inside, and ask them to add lives, the countdown and the overlap event.

Where it leads

Next unit the game decides things every frame: if a sprite leaves the screen, wrap it round. Class idea: vote on the fairest number of lives and timer speed.

What each step is for

Now you can
Next lesson7. If, every framePut an if inside on game update, so the game keeps checking a question while it runs.