- Learnt28 min · lives, a floor-cleared moment, a title, shipping it
- Basic25 min · the finished Castle Climb
- Challenge 1–327 min · hearts, a score, a best time
- Extrabonus · guards that read the grid
Review — Castle Climb III 5 min
clear tile :: customrebuilds a row with a dot where the knight is, then the map is drawn again.- Coins and keys are counted and cleared. A door opens if you carry a key.
- A checkpoint changes
start xandstart y.
Quick-fire
Row 11 is #S.*..^...#.*.E#. The knight takes the coin in column 4. What is row 11 now?
Reveal the answer
#S....^...#.*.E# — only letter 4 changed. The coin in column 13 is still there.
Today’s Topic 3 min
- Lives, and game over
- A moment when each floor is cleared
- A title card, and a
playingswitch that stops and starts the game - Shipping it: instructions, notes and credits, the cold test
Learning outcome
By the end of this lesson you will be able to:
- Give a game lives, a game over and a win.
- Pause the game for a moment without stopping the project.
- Get a game ready for someone who has never seen your code.
Learnt — a climb you can win 28 min
1 · Lives
Up to now, spikes only sent the knight back. Now they cost a life. A lose a life :: custom My Block takes the place of the go to in check tiles :: custom.
define lose a life
change [lives v] by (-1)
start sound [Oops v]
if <(lives) = (0)> then
set [playing v] to (0)
say [Game over! Click the green flag to try again.] for (3) seconds
stop [all v]
else
go to x: (start x) y: (start y)
set [y velocity v] to (0)
end
How to use this: Click the green flag to start it. Nothing else to press — the knight walks into the spikes three times.
2 · A floor-cleared moment
Last lesson the next floor appeared the instant you touched the exit. A player needs a moment to feel they did it.
- Stop the game: set
playingto 0. - Play a sound and say which floor was cleared, for two seconds.
- Then the next floor — or, after the last one, the win.
define floor cleared
set [playing v] to (0)
start sound [Tada v]
say (join [Floor ] (join (level number) [ cleared!])) for (2) seconds
change [level number v] by (1)
if <(level number) > (length of [levels v])> then
say (join [You reached the top with ] (join (coins) [ coins!]))
else
start level :: custom
set [playing v] to (1)
end
next level :: custom.How to use this: Click the green flag to start it. Nothing else to press — the knight climbs floor 1 on its own.
3 · A title card and a switch
playing is the game’s on-off switch. The forever loop only moves the knight while it is 1. The title card sets it to 0, waits for the space bar, then sets it to 1.
when flag clicked
set rotation style [left-right v]
set size to (18) %
point in direction (90)
set [playing v] to (0)
set [lives v] to (3)
set [coins v] to (0)
set [level number v] to (1)
start level :: custom
say [CASTLE CLIMB. Arrows walk, space jumps. Press the space bar to start.]
wait until <key (space v) pressed?>
say []
set [playing v] to (1)
forever
if <(playing) = (1)> then
if <key (right arrow v) pressed?> then
point in direction (90)
walk (4) :: custom
end
if <key (left arrow v) pressed?> then
point in direction (-90)
walk (-4) :: custom
end
if <<key (space v) pressed?> and <(on ground) = (1)>> then
set [y velocity v] to (12)
end
fall :: custom
check tiles :: custom
end
end
How to use this: Click the green flag, then click the stage once and press the space bar to start. Hold ← or → to walk the knight and press the space bar to jump.
4 · Shipping it
Your project is a game when someone else can play it without you. Check four things before you share it.
| Check | What it means |
|---|---|
| Instructions | The title card and the Instructions box both say: arrows walk, space jumps, reach the exit. |
| Notes and Credits | Name what you borrowed: the Knight and the sounds are from the Scratch library; say who drew the tiles. |
| No dead ends | Every floor can be finished. Every door has a key before it. |
| The cold test | Save, do something else for ten minutes, then play it with every script shut. |
Basic — the finished Castle Climb 25 min
Carry on from your key level. Today it becomes three floors with everything in them.
levels 1 #################..............##..............##..............##..............##..............##..............##..............##.........*....##.........#....##S.*..^...#.*.E################# 2 #################............####............DE##...........#####........K.....##.......###....##...C*.........##...###........##..............##.##...........##S##...^^^^^^^.################# 3 #################E.............######..........##......**......##.....####..*..##..........###.##.......C......##......###.....##..............##...##.........##S..##^^^^^^^^.#################
Step 1 — the floors
- Replace the codes in
levelswith these three, or with three of your own. - Make variables for all sprites:
livesandplaying. - Add the Oops and Tada sounds to the knight.
Step 2 — lives and the cleared moment
- Make
lose a life :: customandfloor cleared :: custom, both unticked. - In
check tiles :: custom, spikes now calllose a life :: custom, and the exit callsfloor cleared :: custom.
Step 3 — the title and the switch
- Build the green-flag script from Learnt part 3.
- Play the whole climb. Lose on purpose once.
define check tiles
tile at (x position) ((y position) - (6)) :: custom
if <<(tile) = [^]> or <(y position) < (-170)>> then
lose a life :: custom
end
tile at (x position) (y position) :: custom
pick up :: custom
if <(tile) = [E]> then
floor cleared :: custom
end
How to use this: Click the green flag, then click the stage once and press the space bar to start. Hold ← or → to walk the knight and press the space bar to jump.
After game over, the next game starts with no lives.
set [lives v] to (3) is missing from the green-flag script, or it comes after the forever.
The knight keeps walking during “Floor 1 cleared!”
The movement is not inside <(playing) = (1)>, or floor cleared :: custom does not set playing to 0 first.
The game freezes on the title.
Click the stage once before pressing space: the keys go to the Stage only after it has been clicked.
Hearts
Show lives as hearts, not as a number.
- Three hearts in the top-left corner.
- Lose a life and one heart disappears.
How to use this: Click the green flag to start it. Nothing else to press — the knight walks into the spikes three times.
Teacher reveal
when flag clicked
hide
set [slot v] to (1)
repeat (3)
create clone of [myself v]
change [slot v] by (1)
end
when I start as a clone
go to x: ((-238) + ((24) * (slot))) y: (165)
forever
if <(lives) < (slot)> then
hide
else
show
end
end
A Heart sprite at size 16, with slot for this sprite only. Stamping hearts does not work here: the next erase all would wipe them with the map.
A score
Give the win a number to beat.
- Every coin is worth 10. Every life you still have is worth 50.
- The win screen shows the sum, and how it was made.
How to use this: Click the green flag to start it. Nothing else to press — the knight runs three short floors and wins.
Teacher reveal
set [score v] to (((coins) * (10)) + ((lives) * (50)))
say (join [You win! Score: ] (score))
In place of the win line in floor cleared :: custom. The demo prints the whole sum with three more joins.
A best time
A climb you have finished is worth climbing again — faster.
- The clock starts when you press space on the title card.
- The win screen shows your time in whole seconds.
- It also shows the best time, which is kept from one climb to the next.
How to use this: Click the green flag to start it. Nothing else to press — the knight runs three short floors, then shows its time.
Teacher reveal
set [time v] to (round ((timer) - (climb start)))
if <<(best) = (0)> or <(time) < (best)>> then
set [best v] to (time)
end
say (join (join [Time: ] (time)) (join [ s. Best: ] (join (best) [ s.])))
set [climb start v] to (timer) goes after the wait until <key (space v) pressed?>. best must not be reset by the green flag — that is the whole point. A best of 0 means “no climb yet”.
Guards that read the grid
Add guards that patrol a floor. They use the same map maths as the knight.
- A guard walks along a floor or a shelf.
- It turns round at a wall, and at the edge of a drop — it never falls off.
- Touching a guard costs a life.
How to use this: Click the green flag to start it. Nothing else to press — two guards patrol, and the knight jumps down to meet one.
Teacher reveal
define patrol
change x by ((2) * (dir))
tile at ((x position) + ((10) * (dir))) (y position) :: custom
set [ahead v] to (tile)
tile at ((x position) + ((10) * (dir))) ((y position) - (20)) :: custom
if <<(ahead) = [#]> or <not <(tile) = [#]>>> then
set [dir v] to ((0) - (dir))
point in direction ((90) * (dir))
end
dir is 1 or −1, for this sprite only, so a clone patrols on its own. The knight checks <touching [Guard v]?> in its loop, then waits half a second, so one touch is one life.
Summary 5 min
- Lives turn a mistake into a cost; game over says it clearly before it stops.
- A
playingswitch pauses the game while the project keeps running. - A floor-cleared moment, a title and a win are what make it feel like a game.
- Shipped means someone else can play it from the screen alone.
- Lives
- How many mistakes the player can make before the game ends.
- Game state
- What the game is doing right now: title, playing, paused or finished.
- Title card
- The first screen: the game’s name and how to play.
- Credits
- Naming the people and things your project borrowed from.
Hand in
Share your Castle Climb — the arc is finished. Next lesson you reach the top of the castle: a dragon waits there, and it fights in phases you can learn to read.