- Learnt30 min · levels as save codes, find the S, the exit, spikes
- Basic25 min · a three-floor climb
- Challenge 1–327 min · a floor banner, a fourth floor, a level select
- Extrabonus · a time for every floor
Review — Castle Climb I 5 min
tile at (x) (y) :: customturns a point intocol,rowand the letter intile.- Four corners say if the knight is inside a brick.
- Landing puts the knight 12 above the top of its row. The top of row
ris210 − 30 × r.
Quick-fire
The knight’s feet land in row 9. What y does the knight stand at?
Reveal the answer
y −48. The top of row 9 is 210 − 270 = −60. Twelve above that is −48 — the ledge from last lesson.
Today’s Topic 3 min
- Three levels, kept as three save codes in one list
unpack code :: custom— Tile Maker’s, now run by the knightfind start :: custom— search the map for the SEloads the next level.^sends you back to the start.
Learning outcome
By the end of this lesson you will be able to:
- Keep several levels in one list and load any of them by its number.
- Find where a letter is by scanning the whole map.
- Make tiles that do something when the knight reaches them.
Learnt — three floors 30 min
1 · Three levels, one list
A save code is a whole level in one line: the 12 rows joined, 192 letters. So three levels fit in a list of three items, called levels. A variable, level number, says which one we are on.
levels 1 #################..............##..............##..............##..............##..............##..............##..............##..............##.........#....##S....^...#...E################# 2 #################..............##.............E##...........#####..............##.......###....##..............##...###........##..............##.##...........##S##...^^^^^^^.################# 3 #################E.............######..........##..............##.....####.....##..........###.##..............##......###.....##..............##...##.........##S..##^^^^^^^^.#################
- Make a list for all sprites called
levels. - In its monitor on the Stage, click + three times.
- Paste one code into each item. Scroll the box above to copy them, or use your own: in Tile Maker press C, and copy the code out of the
save codelist.
2 · Unpacking a code
The game still reads the level list, one row at a time. So a code has to be cut back into 12 rows of 16 letters — and you already wrote the block that does it, in Tile Maker IV. Give the knight its own copy, with the counters called r and c (the knight’s col and row belong to tile at (x) (y) :: custom).
define unpack code
delete all of [level v]
set [r v] to (1)
repeat (12)
set [new row v] to []
set [c v] to (1)
repeat (16)
set [new row v] to (join (new row) (letter ((((r) - (1)) * (16)) + (c)) of (code)))
change [c v] by (1)
end
add (new row) to [level v]
change [r v] by (1)
end
code, so the level’s code goes in there first.3 · Finding the start
Every map has one S, but not in the same place. Read every cell. When the letter is S, keep that cell’s centre in start x and start y.
define find start
set [r v] to (1)
repeat (12)
set [c v] to (1)
repeat (16)
if <(letter (c) of (item (r) of [level v])) = [S]> then
set [start x v] to ((-225) + ((30) * ((c) - (1))))
set [start y v] to ((165) - ((30) * ((r) - (1))))
end
change [c v] by (1)
end
change [r v] by (1)
end
define start level
set [code v] to (item (level number) of [levels v])
unpack code :: custom
find start :: custom
go to x: (start x) y: (start y)
set [y velocity v] to (0)
broadcast [redraw v]
redraw, not on the green flag.r and c on the knight are for this sprite only. Tile has its own pair, so the two sprites’ loops can never mix up each other’s count.
How to use this: Click the green flag to start it. Nothing else to press — each floor is unpacked and drawn in turn.
4 · The exit
After every fall, ask what is at the knight’s middle. If it is E, move on to the next level — unless that was the last one.
define check tiles
tile at (x position) ((y position) - (6)) :: custom
if <<(tile) = [^]> or <(y position) < (-170)>> then
go to x: (start x) y: (start y)
set [y velocity v] to (0)
end
tile at (x position) (y position) :: custom
if <(tile) = [E]> then
next level :: custom
end
define next level
change [level number v] by (1)
if <(level number) > (length of [levels v])> then
say [You climbed the castle!] for (3) seconds
stop [all v]
end
start level :: custom
(length of [levels v]) knows how many floors there are.How to use this: Click the green flag to start it. Nothing else to press — the knight climbs floor 1 on its own.
5 · Spikes
The spike art fills the bottom of its cell, so the check is 6 below the knight’s middle. Falling off the bottom of the Stage counts too: <(y position) < (-170)>.
How to use this: Click the green flag to start it. Nothing else to press — the knight walks into the spikes once, then jumps them.
Basic — a three-floor climb 25 min
Carry on from your Castle Climb I project.
Step 1 — the codes
- Make the
levelslist and paste in the three codes (or three of your own). - Make variables for all sprites:
level number,start x,start y,code,new row. - On the knight, make
randcfor this sprite only.
Step 2 — Tile draws on request
- Take
load level :: customanddraw level :: customout of the Tile green-flag script — the knight loads levels now. Leavehidethere. - Add
when I receive [redraw v]withdraw level :: customunder it.
Step 3 — the knight’s new My Blocks
- Make
unpack code :: custom,find start :: customandstart level :: custom. - Make
check tiles :: customandnext level :: custom. Leave these two unticked: one of them waits three seconds.
Step 4 — the green flag
- Change the knight’s hat to
when flag clicked. - Replace the go to with level 1 and start level.
- Add
check tiles :: customafterfall :: custom, inside the forever. - Climb all three floors.
when flag clicked
set rotation style [left-right v]
set size to (18) %
point in direction (90)
set [level number v] to (1)
start level :: custom
forever
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
How to use this: Click the green flag, then click the stage once. Hold ← or → to walk the knight and press the space bar to jump.
The map is drawn, but the knight falls through it.
The knight is reading level before it is loaded. start level :: custom must come before the forever.
The new floor is drawn in the wrong place, or rows are slanted.
A code is not exactly 192 letters. Each row takes the next 16, so one missing letter slants every row below it. Paste the code again.
The knight starts in the wrong place on floor 2.
find start :: custom is running before unpack code :: custom, and found the S of the old floor.
A floor banner
Tell the player which floor they have reached.
- As each floor starts, the knight says
Floor 1,Floor 2orFloor 3for two seconds. - The player can walk while the banner shows.
How to use this: Click the green flag, then click the stage once. Hold ← or → to walk the knight and press the space bar to jump.
Teacher reveal
when I receive [banner v]
say (join [Floor ] (level number)) for (2) seconds
Plus broadcast [banner v] at the end of start level :: custom. Saying it inside the game loop instead freezes the knight for two seconds — the point of the broadcast is a second script.
A fourth floor
Add a level of your own. The game must notice without being told.
- Add a fourth code to
levels: one from your Tile Maker, or this one. - The banner says
Floor 1 of 4, and so on. - The game ends after the last floor. No number in the code says 4.
#################..............##..............##..............##..............##..............##..............##..............##.........##...##.....##..##...##S.^..##..##.^E#################
How to use this: Click the green flag, then click the stage once. Hold ← or → to walk the knight and press the space bar to jump.
Teacher reveal
when I receive [banner v]
say (join (join [Floor ] (level number)) (join [ of ] (length of [levels v]))) for (2) seconds
next level :: custom already compares with (length of [levels v]), so the ending needs nothing new. Pupils who typed 3 there last time find out now.
A level select for testing
Testing floor 3 should not mean climbing floors 1 and 2 every time.
- Press 1, 2 or 3 to jump straight to that floor, drawn, with the knight on its S.
- Press N to skip to the next floor.
- The knight is never left falling, or stuck in a wall, after a jump.
How to use this: Click the green flag, then click the stage once. Press 1, 2 or 3 to jump to a floor, or N to skip one. Hold ← or → to walk and press the space bar to jump.
Teacher reveal
when [3 v] key pressed
set [level number v] to (3)
start level :: custom
when [n v] key pressed
next level :: custom
The mid-jump case works because start level :: custom sets y velocity to 0. Without that line the knight arrives on floor 3 still rising.
A time for every floor
Speed-runners want to know which floor slows them down.
- A list called
times, empty at the green flag. - Each time a floor is finished, its time in whole seconds is added to the list.
- A floor’s time starts when that floor starts, not at the green flag.
How to use this: Click the green flag to start it. Nothing else to press — the knight climbs all three floors on its own.
Teacher reveal
define next level
add (round ((timer) - (floor start))) to [times v]
set [floor start v] to (timer)
change [level number v] by (1)
if <(level number) > (length of [levels v])> then
say [Three floors, three times.] for (3) seconds
stop [all v]
end
start level :: custom
Plus delete all of [times v] and set [floor start v] to (timer) under the green flag.
Summary 5 min
- A whole level fits in one save code, so many levels fit in one list.
unpack code :: customcuts a code back into 12 rows of 16.- Scanning every cell finds a letter; the cell gives its centre.
- Letters can do things:
Eloads the next floor,^sends you back.
- Save code
- A whole level written as one line of letters.
- Unpack
- Turning a packed-up form back into the form a program uses.
- Scan
- Checking every cell in turn, row by row.
- Respawn
- Coming back to the start after losing.
Hand in
Save your three-floor climb. Next lesson the map starts changing while you play: coins vanish, keys open doors, and flags move your restart.