- Learnt28 min · a phase variable, a tell, a window
- Basic25 min · the first boss fight
- Challenge 1–327 min · a flash, one hit per window, a gloat
- Extrabonus · a dodge roll
Review — Castle Climb 5 min
- Each floor was a 192-letter save code in the
levelslist.unpack code :: customcut it into rows, and the Tile sprite stamped them. - The knight never touched a colour.
tile at (x) (y) :: customread the letter at a point, and four corners kept it out of the bricks. - Coins and keys vanished by rewriting one letter, a key opened a door, and a checkpoint moved
start xandstart y. - Three
lives, a “Floor cleared!” moment, and aplayingswitch: 1 while the knight can move, 0 on the title card and between floors.
Quick-fire
The guards in Castle Climb’s last Extra walked left, then right, then left again, for ever. What is the smallest thing a guard has to remember to know which way to walk next?
Reveal the answer
One variable: which way it is going now. That was dir, 1 or −1 — the guard’s state. playing had two states as well. Today’s dragon has four, and they change by themselves.
Today’s Topic 3 min
- A boss as a state machine: one
phasevariable - Four phases: idle, wind-up, attack, recover
- A tell: warning the player before an attack
- A window: the only time the boss can be hurt
Learning outcome
By the end of this lesson you will be able to:
- Drive a sprite with one variable that names what it is doing.
- Give an attack a warning a player can see and react to.
- Let a hit count only in the right phase.
Learnt — a boss in four phases 28 min
1 · A boss is a cycle
A good boss is not random. It does the same four things, in the same order, again and again. Once you learn the order, you can win.
| Phase | The dragon | The player |
|---|---|---|
idle | drifts to a new spot | gets ready |
wind-up | crouches; the floor blinks where the fire will land | moves away |
attack | breathes a fireball | is already somewhere else |
recover | sinks low and rests | attacks! |
One variable, phase, holds the word for what the dragon is doing now. That is a state machine: a few states, and rules for moving between them.
2 · One variable, one script that asks it
In Storm Flight, a button changed the state. Here nobody presses anything. Each phase does its job, then names the next phase. That is the whole trick.
when flag clicked
go to x: (0) y: (70)
set [phase v] to [idle]
forever
if <(phase) = [idle]> then
switch costume to [dragon-a v]
glide (1.2) secs to x: (pick random (-150) to (150)) y: (70)
set [phase v] to [wind-up]
end
if <(phase) = [wind-up]> then
switch costume to [dragon-b v]
set [target x v] to (knight x)
broadcast [warn v]
glide (0.3) secs to x: (target x) y: (70)
wait (0.7) seconds
set [phase v] to [attack]
end
if <(phase) = [attack]> then
switch costume to [dragon-c v]
create clone of [Fireball v]
wait (0.8) seconds
set [phase v] to [recover]
end
if <(phase) = [recover]> then
switch costume to [dragon-a v]
glide (0.4) secs to x: (x position) y: (0)
wait (1.2) seconds
glide (0.4) secs to x: (x position) y: (70)
set [phase v] to [idle]
end
end
ifs, and each one ends by naming the next phase.How to use this: Click the green flag to start it. Nothing else to press — watch the phase monitor and the dragon change together.
The knight posts its own x into knight x every turn of its loop. That is how the dragon knows where to aim.
3 · The tell
A person needs a moment to see something and react — a little under half a second. An attack with no warning beats every player, every time. That is not hard. It is unfair.
The wind-up is the tell. The dragon crouches, and a marker blinks on the floor where the fire will land. Both demos below use the same knight: it notices, waits 0.4 seconds like a person would, then steps away.
How to use this: Click the green flag to start it. Nothing else to press — watch the knight try to dodge.
How to use this: Click the green flag to start it. Nothing else to press — watch the floor blink, then the knight step away.
when I receive [warn v]
go to x: (target x) y: (-150)
repeat (5)
show
wait (0.1) seconds
hide
wait (0.1) seconds
end
4 · The window
The knight throws bolts. A bolt only hurts the dragon in the recover phase. At any other time it bounces off. So the player has to wait for the window.
when I start as a clone
go to x: (knight x) y: (-80)
show
repeat until <(y position) > (170)>
change y by (10)
if <touching [Dragon v]?> then
if <(phase) = [recover]> then
change [hits v] by (1)
else
change [bounced v] by (1)
end
delete this clone
end
end
delete this clone
How to use this: Click the green flag to start it. Nothing else to press — the knight throws a bolt every 0.3 seconds for two whole cycles.
Basic — the first boss fight 25 min
Start a new project. You will build the fight from the Learnt yourself.
Step 1 — the hall and the cast
- Choose the backdrop
Castle 3. Delete the cat. - Add the
Dragonand theKnightfrom the library. Set both to size 50. - Click the Knight’s direction and choose the left-right arrows, so it never turns upside down.
- Paint three small sprites:
Fireball(a red circle),Bolt(a small blue circle) andMarker(a red ring).
Step 2 — the variables
- For all sprites:
phase,target x,knight x,hitsandburns. - On the Knight, for this sprite only:
next shot.
Step 3 — the knight
when flag clicked
go to x: (-150) y: (-115)
set [hits v] to (0)
set [burns v] to (0)
set [next shot v] to (0)
forever
if <key (right arrow v) pressed?> then
point in direction (90)
change x by (6)
end
if <key (left arrow v) pressed?> then
point in direction (-90)
change x by (-6)
end
set [knight x v] to (x position)
if <<key (space v) pressed?> and <(timer) > (next shot)>> then
set [next shot v] to ((timer) + (0.3))
create clone of [Bolt v]
end
end
next shot.Step 4 — the dragon’s brain
Build the four-phase script from Learnt section 2 on the Dragon.
Step 5 — fire, marker, bolt
when flag clicked
hide
when I start as a clone
go to x: (target x) y: (40)
show
repeat until <(y position) < (-150)>
change y by (-8)
if <touching [Knight v]?> then
change [burns v] by (1)
delete this clone
end
end
delete this clone
when I start as a clone
go to x: (knight x) y: (-80)
show
repeat until <(y position) > (170)>
change y by (10)
if <touching [Dragon v]?> then
if <(phase) = [recover]> then
change [hits v] by (1)
end
delete this clone
end
end
delete this clone
The Marker is the blinking script from Learnt section 3, with a hide on the green flag.
How to use this: Click the green flag, then click the stage once and hold ← or → to move the Knight. Press the space bar to throw a bolt.
The dragon stays in one phase for ever.
That phase’s if is missing its last block. Every phase ends by setting the next one.
The fire always lands on the left of the hall.
knight x is never set, so it stays 0 or its old value. Check the set [knight x v] to (x position) is inside the knight’s forever.
Every bolt counts, even when the dragon is high up.
The if <(phase) = [recover]> then is missing round the change [hits v] by (1).
A tell you cannot miss
The marker is on the floor. A player watching the dragon might miss it.
- During the wind-up, the dragon flashes bright four times.
- The wind-up still lasts one second.
- The dragon looks normal again when it attacks.
How to use this: Click the green flag, then click the stage once and hold ← or → to move the Knight. Press the space bar to throw a bolt.
Teacher reveal
Replace the wait (0.7) seconds in the wind-up with:
repeat (4)
set [brightness v] effect to (60)
wait (0.1) seconds
set [brightness v] effect to (0)
wait (0.075) seconds
end
Four times 0.175 seconds is 0.7, so the tell keeps its length.
One hit per window
Holding space in the window lands four bolts at once. That makes the fight too easy.
- The first bolt that lands ends the window straight away.
- Later bolts bounce off, even though the dragon is still low.
- The dragon’s own script is not changed at all.
How to use this: Click the green flag to start it. Nothing else to press — the knight throws a bolt every 0.3 seconds.
Teacher reveal
if <(phase) = [recover]> then
change [hits v] by (1)
set [phase v] to [idle]
end
A phase can be changed from outside the dragon. The dragon finishes its rest, but the window is already shut — every bolt now sees idle.
The dragon gloats
Make getting burnt cost something more than one point.
- If the fireball burns the knight, the dragon says “Ha!” and skips its rest.
- If the fireball misses, the dragon rests as usual.
- So a burn also means no window this time.
How to use this: Click the green flag to start it. Nothing else to press — the knight stands still for the first attack and steps away from the second.
Teacher reveal
if <(phase) = [attack]> then
switch costume to [dragon-c v]
set [scorched v] to (0)
create clone of [Fireball v]
wait (0.8) seconds
if <(scorched) = (1)> then
say [Ha!] for (0.8) seconds
set [phase v] to [idle]
else
set [phase v] to [recover]
end
end
The Fireball sets scorched to 1 when it burns the knight. One phase, two ways out — the next phase depends on what happened.
A dodge roll
The knight gets a state machine of its own.
- A
rollvariable:ready,rollingortired. - Press ↓ when ready: the knight rolls for 0.7 seconds and fire cannot hurt it.
- Then it is tired for 0.8 seconds and cannot roll again.
- The knight looks see-through while it rolls.
How to use this: Click the green flag to watch the knight roll under each fireball. Then click the stage once and press ↓ to roll yourself.
Teacher reveal
when [down arrow v] key pressed
if <(roll) = [ready]> then
set [roll v] to [rolling]
set [ghost v] effect to (60)
wait (0.7) seconds
set [roll v] to [tired]
set [ghost v] effect to (0)
wait (0.8) seconds
set [roll v] to [ready]
end
And in the Fireball:
if <<touching [Knight v]?> and <not <(roll) = [rolling]>>> then
change [burns v] by (1)
delete this clone
end
Summary 5 min
- A boss is a state machine: one
phasevariable, and one script that asks it. - Each phase does its job, then names the next one.
- A wind-up warns the player. An attack with no warning is unfair, not hard.
- A hit counts only in the recover window, because the bolt checks the phase.
- State machine
- A few states, and the rules for moving from one to the next.
- Phase
- The state a boss is in right now.
- Tell
- A warning that shows an attack is coming.
- Window
- The short time when the boss can be hurt.
Hand in
Save your boss fight. Next lesson the dragon stops doing the same attack every time: it reads its moves from a list.