- Learnt30 min · rewrite one letter, coins, keys and doors, checkpoints
- Basic25 min · the key level
- Challenge 1–327 min · coins left, a flag that turns gold, a coin gate
- Extrabonus · keys that follow you
Review — Castle Climb II 5 min
- Each level is a save code in the
levelslist.unpack code :: customcuts it into thelevellist. find start :: customscans for the S and keeps its centre.Eloads the next level;^sends the knight back to the start.
Quick-fire
The knight walks into column 6, row 11. If that cell were a checkpoint, what would start x and start y become?
Reveal the answer
x −75, y −135 — the cell’s centre. −225 + 30 × 5 = −75, and 165 − 30 × 10 = −135. That is today’s checkpoint, already worked out.
Today’s Topic 3 min
- A map that changes while the game is running
clear tile :: custom— rebuild one row with a dot in it- Coins
*and a keyKyou collect; a doorDthat opens with a key - A checkpoint
Cthat moves where you come back
Learning outcome
By the end of this lesson you will be able to:
- Change one letter of a level while the game runs, and redraw the map.
- Make tiles you collect, and a tile that is solid only some of the time.
- Move the restart point when the player reaches a checkpoint.
Learnt — a map that changes 30 min
1 · Changing one letter
A coin you have taken must stop being a coin. In the list, its * becomes a .. You met this problem in Tile Maker III: Scratch has no block that changes one letter, so set tile built the row again. clear tile :: custom is the knight’s version. It always writes a dot, at the col and row that tile at (x) (y) :: custom just found — and it does not stamp, because only the Tile sprite stamps.
- Start with an empty
new row. - Go along the row, letter 1 to letter 16.
- At the knight’s column, add a
.. Everywhere else, add the letter that was there. - Put the new row back in the list, then ask Tile to draw again.
define clear tile
set [new row v] to []
set [i v] to (1)
repeat (16)
if <(i) = (col)> then
set [new row v] to (join (new row) [.])
else
set [new row v] to (join (new row) (letter (i) of (item (row) of [level v])))
end
change [i v] by (1)
end
replace item (row) of [level v] with (new row)
col and row from the last tile at (x) (y) :: custom. Call it straight after.2 · Coins
pick up :: custom runs after the middle-of-the-knight check, so tile already holds the letter there.
define pick up
if <(tile) = [*]> then
clear tile :: custom
change [coins v] by (1)
start sound [Coin v]
broadcast [redraw v]
end
if <(tile) = [K]> then
clear tile :: custom
change [keys v] by (1)
start sound [Collect v]
broadcast [redraw v]
end
if <(tile) = [C]> then
set [start x v] to ((-225) + ((30) * ((col) - (1))))
set [start y v] to ((165) - ((30) * ((row) - (1))))
end
How to use this: Click the green flag to start it. Nothing else to press — the knight walks along the row of coins.
3 · Keys and doors
A door is a brick that can change its mind. It lives in corner (dx) (dy) :: custom, where the knight asks “am I in something solid?”
- No key: count it as a hit, the same as a brick.
- A key: clear the door, use up the key, and do not count a hit. The knight walks in.
define corner (dx) (dy)
tile at ((x position) + (dx)) ((y position) + (dy)) :: custom
if <(tile) = [#]> then
change [hits v] by (1)
end
if <(tile) = [D]> then
if <(keys) > (0)> then
clear tile :: custom
change [keys v] by (-1)
broadcast [redraw v]
else
change [hits v] by (1)
end
end
How to use this: Click the green flag to start it. Nothing else to press — the knight tries the door, fetches the key, and tries again.
4 · Checkpoints
Spikes already send the knight to start x, start y. So a checkpoint only has to change those two numbers to its own centre.
How to use this: Click the green flag to start it. Nothing else to press — the knight walks past the flag and into the spikes.
Basic — the key level 25 min
Carry on from your three-floor climb. Today it plays one new level with everything in it.
#################..............##...*.*.*K.....##..########....##..............##...........##.##.....C.**.....##....######....##..............##.##.......######S...^^^.*.D..E#################
Step 1 — the level
- Paste the code above into
levelsas item 1 (or make your own with a K, a D, some coins and a C). - Make variables for all sprites:
coins,keys,new row. - Add the Coin and Collect sounds to the knight.
Step 2 — the new My Blocks
- Make
clear tile :: customandpick up :: custom. Tick Run without screen refresh on both. - Add the door to
corner (dx) (dy) :: custom.
Step 3 — join them up
- In
check tiles :: custom, callpick up :: customstraight after the middle check. - In
start level :: custom, setkeysto 0. - Under the green flag, set
coinsto 0. - Get the key off the top shelf and out through the door.
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
pick up :: custom
if <(tile) = [E]> then
next level :: custom
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)
set [keys v] to (0)
broadcast [redraw v]
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 coin is taken, but it is still on the screen.
The list changed and nobody drew it. broadcast [redraw v] after clear tile :: custom.
The wrong tile disappears.
Another tile at (x) (y) :: custom ran between the check and clear tile :: custom, and moved col and row.
One coin gives 3 or 4 points.
The coin was counted but the row was never changed — check the replace item (row) of [level v] with (new row) at the end of clear tile :: custom.
Coins left
Show how many coins are still out there.
- A
coins leftmonitor, counted from the level when it loads. - It goes down by one for every coin taken.
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
define count coins
set [coins left v] to (0)
set [i v] to (1)
repeat (192)
if <(letter (i) of (code)) = [*]> then
change [coins left v] by (1)
end
change [i v] by (1)
end
Called at the end of start level :: custom, plus change [coins left v] by (-1) in the coin part of pick up :: custom. Counting the code is simpler than counting 12 rows — Tile Maker IV’s level check counted its starts and exits the same way.
A flag that turns gold
A checkpoint should show that it worked.
- Give the Tile sprite a costume called
reached: the checkpoint tile with its flag painted gold. - The checkpoint you reached is drawn gold. The others stay green.
- Do not change the letter: it stays
C, so the save format still works.
How to use this: Click the green flag to start it. Nothing else to press — the knight walks past the flag.
Teacher reveal
set [letter v] to (letter (c) of (item (r) of [level v]))
if <<(letter) = [C]> and <<(c) = (flag col)> and <(r) = (flag row)>>> then
switch costume to (reached v)
else
switch costume to (letter)
end
Inside the Tile sprite’s draw level :: custom, in place of its switch costume. The knight sets flag col and flag row from col and row in the C part of pick up :: custom, and broadcasts redraw — but only when they change, or it redraws 30 times a second while standing on the flag.
A coin gate
Make the player collect everything before they can leave.
- While any coin is left, the exit does nothing.
- Standing in the exit with coins left, the knight says how many:
2 coins to go! - With every coin taken, the exit works as before.
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
if <(tile) = [E]> then
if <(coins left) > (0)> then
say (join (coins left) [ coins to go!]) for (1) seconds
else
next level :: custom
end
end
It needs Challenge 1’s coins left. The one-second say pauses the check, so the knight is not told 30 times a second.
Keys that follow you
A number in a monitor is easy to miss. Show the keys the knight is carrying.
- Every key you carry trails behind the knight, in a line.
- Use a key on a door, and one fewer follows.
- Up to three keys.
How to use this: Click the green flag to start it. Nothing else to press — the knight picks up two keys and opens one door.
Teacher reveal
when flag clicked
hide
set [place v] to (1)
repeat (3)
create clone of [myself v]
change [place v] by (1)
end
when I start as a clone
forever
go to x: ((knight x) - ((16) * (place))) y: ((knight y) + (6))
if <(keys) < (place)> then
hide
else
show
end
end
place is for this sprite only, so each clone keeps its own. The knight sets knight x and knight y at the end of every tick. The demo trails the keys to the left; a strong pupil flips them with the knight’s direction.
Summary 5 min
- To change one letter, build a new row and replace the old one.
- Change the list, then draw it again: the picture follows the list.
- A door is a brick only while you have no key.
- A checkpoint is two numbers: where the knight comes back.
- Rewrite
- Changing the level list while the game runs.
- Redraw
- Stamping the whole map again from the list.
- Checkpoint
- A place that becomes your new start once you reach it.
- Collectable
- A tile that is counted and removed when you touch it.
Hand in
Save your key level. Next lesson the climb gets lives, a moment when each floor is cleared, a title — and you ship the whole game.