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.
Make your prediction first. Help opens once you've locked it in.

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.
Count the rocks as they come.

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
})
Dodge the rocks with the arrow keys.

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:

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
})
The timer runs every 500 ms. How many rocks has it made after 10 seconds?
Match each timer with how often it runs.
Make a rock come every half a second.
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.
pick random is in Math.
Drag it into the y of set rock position, inside the timer: pick random 10 to 110.
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.

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 happens | lives after |
|---|---|
| (start) | 2 |
| collects a heart | |
| a rock hits | |
| a rock hits |
Make a 30-second dodge game (bats in a cave, asteroids, traffic):
In on start: set life to 3 and start countdown 30 (both in Info).
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.
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.
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.
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.
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.
Seeing that the blocks inside a timer run again and again. Ask how many times the create block has run after five seconds.
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.
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.