Compare two positions to steer an enemy towards the player with if … else, then meet the follow block that does it for you.
The ghosts in Pac-Man, the zombies in Plants vs. Zombies, a guard walking up and down in a stealth game: each one asks 'where is the player?' and turns that way.
Last lesson an if in on game update kept asking a question. Now the enemy will ask a question about the player: is my x less than the player's x? If yes, the player is to the right, so go right. Otherwise (else), go left.
An if … else block has two parts, and exactly one of them runs: the first when the answer is yes, the else part when it is no. Click the + at the bottom of an if block to add else.
Make your prediction first. Help opens once you've locked it in.

scene.setBackgroundColor(7)
let mouse = sprites.create(img`
. . . . d d
. d d d d f
d d d d d d
. d . d . .
`, SpriteKind.Player)
mouse.setPosition(140, 60)
controller.moveSprite(mouse)
let cat = sprites.create(img`
f . . f .
f f f f .
f 5 f 5 f
f f f f f
. f . f .
`, SpriteKind.Enemy)
cat.setPosition(20, 60)
game.onUpdate(function () {
if (cat.x < mouse.x) {
cat.vx = 30
} else {
cat.vx = -30
}
})
< means "less than". vx is the speed across: positive goes right, negative goes left.
Now move the mouse with the arrow keys. Go left, right, up and down. What does the cat follow, and what does it ignore?

scene.setBackgroundColor(7)
let mouse = sprites.create(img`
. . . . d d
. d d d d f
d d d d d d
. d . d . .
`, SpriteKind.Player)
mouse.setPosition(140, 60)
controller.moveSprite(mouse)
let cat = sprites.create(img`
f . . f .
f f f f .
f 5 f 5 f
f f f f f
. f . f .
`, SpriteKind.Enemy)
cat.setPosition(20, 60)
game.onUpdate(function () {
if (cat.x < mouse.x) {
cat.vx = 30
} else {
cat.vx = -30
}
})

scene.setBackgroundColor(7)
let mouse = sprites.create(img`
. . . . d d
. d d d d f
d d d d d d
. d . d . .
`, SpriteKind.Player)
mouse.setPosition(140, 60)
controller.moveSprite(mouse)
let cat = sprites.create(img`
f . . f .
f f f f .
f 5 f 5 f
f f f f f
. f . f .
`, SpriteKind.Enemy)
cat.setPosition(20, 60)
game.onUpdate(function () {
if (cat.x < mouse.x) {
cat.vx = 30
} else {
cat.vx = -30
}
})
Work out the cat's vx from the two positions.
Zoe's blocks (load them in the playground and run them):

game.onUpdate(function () {
if (cat.x < mouse.x) {
cat.vx = -30
} else {
cat.vx = 30
}
})
For each position, which way does the if … else set the cat's vx?
| cat x | mouse x | cat vx |
|---|---|---|
| 20 | 140 | |
| 120 | 50 | |
| 80 | 81 | |
| 80 | 80 |
Your cat only chases across. Add a second if … else for up and down, using y and vy.
Add a second if … else under the first one, inside on game update.
Ask 'cat y < mouse y'. y counts down, so if the mouse is lower, the cat needs a positive vy.
Make the game end when the cat touches the mouse.
on overlap is in Sprites › Overlaps. Choose Player and Enemy.
game over is in Game.
Load the follow version and run it. Arcade has a block (follow, in Sprites) that does your chasing in one block. Try a speed of 60.
Make the cat chase up and down too.
This ticks by itself when your blocks do what is asked.
When the cat catches the mouse, the game is over.
This ticks by itself when your blocks do what is asked.

let hero = sprites.create(img`
. 4 4 .
4 4 4 4
. 8 8 .
. 8 8 .
`, SpriteKind.Player)
controller.moveSprite(hero)
game.onUpdate(function () {
if (hero.y < 30) {
scene.setBackgroundColor(7)
}
})
Make a guard that patrols. The guard already walks right. Make it:
for ever. Use one if with an else if (the + on the if block adds more parts; the − takes one away).
Use on game update and one if block with an else if: one question for each end of the walk.
If guard x > 140, set guard vx to -40 (walk left). Else if guard x < 20, set guard vx to 40.
There is no else: in the middle of the walk, the guard keeps going the way it was going.
A guard that walks left and right between x 20 and x 140, turning at each end, in one if … else if.
This ticks by itself when your blocks do what is asked.
Here the sheep runs away from the dog (you move the dog). Make the sheep run away up and down as well, and make it a bit slower than the dog so you can herd it.
Students build an enemy that chases the player with if … else by comparing positions, then meet the follow block that does the same steering for them. About 50 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
if … else has two parts and exactly one runs: the first when the question is true, the else part when it is false. else if adds another question, asked only when the ones above were false.
The chaser asks is cat x < mouse x? If yes, the mouse is to the right of the cat, so the cat's vx (speed across) must be positive. Otherwise it is negative (go left). Up and down works the same with y and vy; y counts down, so a mouse lower on the screen has a bigger y and the cat needs a positive vy.
The planted misconception (Zoe's cat) is a swapped sign: the cat runs away. It is easy to make, because "less than" and "go right" do not sound alike. Read it aloud: "if the mouse is to my right, go right."
After students have built the chaser, they meet follow (Sprites), which does the steering in one block. This is "build it yourself, then meet the real tool": they now know what the block does inside. The Make (a patrolling guard) uses else if with no else, so the guard keeps walking in the middle.
Choosing the sign of vx from two positions. Ask them to say in words which side the player is on before choosing positive or negative.
Stretch: Make two guards that patrol at different speeds and heights, and make the game end if either touches the player. Or make a cat that only chases when the mouse is close (an if around the chase).
Support: Give students the first if … else ready-made and ask them to copy it for y. For the guard, give the if and else if with empty questions.
Next unit: projectiles and lists, so the player can fight back. Class ideas: a chase game with a cat that speeds up over time; a 'follow the leader' line of ducks using follow; discuss how ghosts in Pac-Man each chase differently.