← Teacher guide

Timers

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.

Arcade Foundations · Collide and score · about 45 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 7 min · Investigate 10 min · Modify 8 min · Mix it up 3 min · Make 13 min · Rain dodger 4 min

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.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

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

What you will seeA question to askThe fix
Only one enemy ever appears.Is the create block inside the timer, or in on start?Move it into on game update every 1000 ms, so a new one is made each second.
No enemies appear.Where does each enemy start, and which way does it move?Start at x 160 and give a negative vx. With a positive vx it leaves the screen to the right at once.
Every rock comes at the same height.Where is pick random?Put it inside the timer, in the set position block, so it runs for every rock.

Answers to the checks

  1. Predict: The timer runs every 1000 ms and makes a rock each time. You watch for 5 seconds. How many rocks have appeared?
    5. The timer runs once every second, and each time it makes a new rock: 5 seconds, 5 rocks.
  2. How many?: The timer runs every 500 ms. How many rocks has it made after 10 seconds?
    20
    500 ms is half a second, so the timer runs twice a second: 20 times in 10 seconds.
  3. Ava's rocks: 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?
    Every rock comes at the same height
    pick random ran once, in on start, so y never changes. Put pick random inside the timer, so each new rock gets a new number.
  4. How often?: Match each timer with how often it runs.
    every 1000 ms → B. once a second; every 500 ms → C. twice a second; every 2000 ms → A. once every two seconds
  5. Mix it up: The player starts with 2 lives. Fill in the lives after each thing happens.
    3, 2, 1
    The heart's Player–Food event adds one life; each rock that hits takes one and is destroyed.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Faster rocks

The program as blocks
Show as code
let rock: Sprite = null
let mySprite = sprites.create(@IMG:mySprite@, SpriteKind.Player)
mySprite.setPosition(20, 60)
controller.moveSprite(mySprite)
game.onUpdateInterval(500, function () {
    rock = sprites.create(@IMG:rock@, SpriteKind.Enemy)
    rock.setPosition(160, 60)
    rock.vx = -50
})

Modify › Random heights

The program as blocks
Show as code
let rock: Sprite = null
let mySprite = sprites.create(@IMG:mySprite@, SpriteKind.Player)
mySprite.setPosition(20, 60)
controller.moveSprite(mySprite)
game.onUpdateInterval(500, function () {
    rock = sprites.create(@IMG:rock@, SpriteKind.Enemy)
    rock.setPosition(160, randint(10, 110))
    rock.vx = -50
})

Make

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

Change a rain dodger

Open task: any program that does what the step asks.

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.

Exit ticket

A timer runs every 2000 ms and makes one enemy each time. How many enemies after 10 seconds?

5. It runs once every two seconds.

What each step is for

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.