- Learnt28 min · a Pen health bar, phase 2, a flash and a shake, two endings
- Basic25 min · a fight with an ending
- Challenge 1–327 min · bar colours, stamped hearts, a damage trail
- Extrabonus · phase 3 and its bats
Review — Boss Tower II 5 min
- The dragon reads its next move with
(item (step) of [pattern v]). - A move is a list of spots. The Marker and the Fireball both read
spots. - A fair attack always leaves a gap.
Quick-fire
Arena Duel drew its health bars with eleven costumes, one for each tenth. How many would a dragon with 37 health need?
Reveal the answer
38 — one for every value from 0 to 37. Today the Pen draws a bar of any length from one sum, so the answer becomes “none”.
Today’s Topic 3 min
- A health bar drawn with the Pen
- A second phase at half health: a new pattern, faster
- A hit flash and a screen shake
- Win and lose
Learning outcome
By the end of this lesson you will be able to:
- Draw a bar whose length follows a variable.
- Change how a sprite behaves when a number crosses a line.
- Finish a game with two different endings.
Learnt — health, anger and an ending 28 min
1 · A health bar from one sum
You drew with the Pen in Sketch Pad. A health bar is two straight lines: a grey one for the whole bar, and a red one on top for the health left.
| boss hp | −100 + 200 × hp ÷ 10 | the red line ends at |
|---|---|---|
| 10 | −100 + 200 | x 100 (full) |
| 5 | −100 + 100 | x 0 (half) |
| 3 | −100 + 60 | x −40 |
| 0 | −100 + 0 | x −100 (empty) |
define draw bar
erase all
set pen size to (12)
set pen color to [#3A3A3A]
go to x: (-100) y: (160)
pen down
go to x: (100) y: (160)
pen up
if <(boss hp) > (0)> then
set pen color to [#E0262B]
go to x: (-100) y: (160)
pen down
go to x: ((-100) + ((200) * ((boss hp) / (max hp)))) y: (160)
pen up
end
when flag clicked
hide
forever
draw bar :: custom
end
boss hp.How to use this: Click the green flag to start it. Nothing else to press — watch boss hp count down and the bar follow it.
2 · Phase 2 at half health
First, swap two numbers in the dragon’s brain for variables: rest (1.2) and wind-up (1). Then one more script can change how the whole fight feels.
glide (rest) secs to x: (pick random (-150) to (150)) y: (70)
wait (wind-up) seconds
wait (rest) seconds
when flag clicked
wait (0.1) seconds
wait until <not <(boss hp) > ((max hp) / (2))>>
delete all of [pattern v]
add [rain] to [pattern v]
add [sweep] to [pattern v]
add [rain] to [pattern v]
set [step v] to (1)
set [wind-up v] to (0.6)
set [rest v] to (0.8)
set [color v] effect to (150)
say [ROAR!] for (1) seconds
How to use this: Click the green flag to start it. Nothing else to press — this dragon starts on 6 health, so phase 2 comes in the first window.
The new pattern and the new wind-up are still fair: rain still has its gap, and 0.6 seconds is still longer than a person needs to react.
3 · A flash and a shake
A hit has to feel like a hit. When a bolt lands, it broadcasts two messages.
when I receive [hurt v]
start sound [Clang v]
set [brightness v] effect to (70)
wait (0.1) seconds
set [brightness v] effect to (0)
when I receive [shake v]
repeat (3)
change x by (6)
wait (0.03) seconds
change x by (-6)
wait (0.03) seconds
end
How to use this: Click the green flag to start it. Nothing else to press — the knight throws bolts and every landed one flashes and shakes.
4 · Two endings
A referee on the Knight waits for either number to run out, then says which ending it is.
when flag clicked
set [state v] to [fight]
wait (0.1) seconds
wait until <<(boss hp) < (1)> or <(lives) < (1)>>
if <(boss hp) < (1)> then
set [state v] to [win]
broadcast [win v]
else
set [state v] to [lose]
broadcast [lose v]
end
if <(state) = [fight]> then.Basic — a fight with an ending 25 min
Open your boss fight from last lesson.
Step 1 — the numbers
- New variables for all sprites:
boss hp,max hp,lives,rest,wind-upandstate. - On the Knight’s green flag:
set [boss hp v] to (max hp)andset [lives v] to (3). Setmax hpto 10. - On the Dragon’s green flag:
set [rest v] to (1.2)andset [wind-up v] to (1), and put the three pattern items back into the list.
Step 2 — hits and burns
- In the Bolt, a landed hit now does
change [boss hp v] by (-1),broadcast [hurt v]andbroadcast [shake v]. - In the Fireball, a burn now does
change [lives v] by (-1).
Step 3 — the bar and phase 2
- Make a Bar sprite (any costume — it stays hidden). Build the bar from section 1.
- Swap the three numbers in the brain, and add the phase 2 script, from section 2.
- Add the flash and the shake from section 3.
Step 4 — the endings
- Add the referee to the Knight, and wrap its keys in the state check.
- Dragon, on
win: stop its other scripts, fade out with the ghost effect, hide. - Knight, on
win: stop its other scripts, playWin, say “Victory!”. Onlose: playLose, say “Burnt! Try again.”
when I receive [win v]
stop [other scripts in sprite v]
switch costume to [dragon-a v]
repeat (10)
change [ghost v] effect by (10)
change y by (-4)
end
hide
when I receive [lose v]
stop [other scripts in sprite v]
switch costume to [dragon-c v]
say [Too hot for you!]
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 game ends the moment I press the green flag.
The referee saw last game’s numbers. Check the wait (0.1) seconds is before its wait until.
A red dot stays at the left of the bar.
The red line is drawn even at 0. Put it inside if <(boss hp) > (0)> then.
The bar flickers.
draw bar is not ticked Run without screen refresh, so you see it half drawn.
A bar that warns you
- Above half health, the bar is green.
- Above a quarter, it is orange.
- At a quarter or less, it is red.
How to use this: Click the green flag to start it. Nothing else to press — watch the bar change colour as it shrinks.
max hp at 10.Teacher reveal
Replace the red set pen color to [#E0262B] with:
if <(boss hp) > ((max hp) / (2))> then
set pen color to [#2EB82E]
else
if <(boss hp) > ((max hp) / (4))> then
set pen color to [#FF9F1A]
else
set pen color to [#E0262B]
end
end
Hearts, stamped
Swap the lives number for pictures.
- Give the Bar sprite the Heart costume.
- One heart per life, in a row at the top left.
- Hide the
livesmonitor — the hearts say it now.
How to use this: Click the green flag to start it. Nothing else to press — the knight stands still, so watch a heart go with each burn.
lives, and none are left when it reaches 0.Teacher reveal
At the end of draw bar:
go to x: (-210) y: (160)
repeat (lives)
show
stamp
hide
change x by (28)
end
erase all at the top of the block also wipes the old hearts, so they are redrawn fresh every frame. The show and hide are never seen: the block runs without screen refresh.
A damage trail
Show the player how much a hit took.
- When the dragon is hit, the part that was lost turns white.
- The white part then drains away smoothly, behind the red.
- It catches up in about a second.
How to use this: Click the green flag to start it. Nothing else to press — the dragon loses 2 health four times.
Teacher reveal
A second variable, shown hp, chases boss hp down. Draw a white line to shown hp between the grey and the red.
when flag clicked
hide
set [shown hp v] to (10)
forever
if <(shown hp) > (boss hp)> then
change [shown hp v] by (-0.05)
end
draw bar :: custom
end
The loop draws, so it runs once a frame: 0.05 × 30 frames is 1.5 health a second.
Phase 3: the bats
Give the fight a final phase.
- At a quarter health, the dragon calls two bats.
- The bats swoop back and forth over the knight’s head.
- A bat that touches the knight costs a life. A bolt pops a bat.
- The bats leave when the dragon is beaten.
How to use this: Click the green flag to start it. Nothing else to press — this dragon starts on 3 health and the knight keeps throwing bolts.
Teacher reveal
when I receive [bats v]
set [my no v] to (0)
repeat (2)
change [my no v] by (1)
create clone of [myself v]
end
when I start as a clone
go to x: ((((my no) * (2)) - (3)) * (180)) y: (-20)
point in direction (((3) - ((my no) * (2))) * (90))
show
forever
move (4) steps
if on edge, bounce
set y to ((-30) + ((30) * ([sin v] of ((timer) * (200)))))
if <touching [Bolt v]?> then
delete this clone
end
if <touching [Knight v]?> then
change [lives v] by (-1)
delete this clone
end
end
Bat 1 starts at x −180 heading right; bat 2 at x 180 heading left. The sine makes the swoop. The demo adds next costume in the loop, so the wings flap.
Summary 5 min
- A Pen health bar is two lines, and the second one’s end is one sum.
- A boss gets a second phase when a number crosses a line: new list, shorter wind-up.
- A flash and a shake make a hit feel like a hit.
- A referee waits for either number to run out, and picks the ending.
- Health bar
- A line whose length shows how much health is left.
- Threshold
- The number at which something changes — here, half health.
- Juice
- Small effects that make an action feel strong: flashes, shakes, sounds.
- Ending
- What the game does when it is won or lost.
Hand in
Save your boss fight. Next lesson it gets its front and back doors: a title screen, the credits, and a best time to beat.