- Learnt28 min · coins from lists, and the edge again
- Basic24 min · the coin run
- Challenge 1–327 min · a gated flag, coins per level, stars
- Extrabonus · ship it
Review — levels 5 min
- One
levelvariable picks the screens and the sky throughjoin. - A rebuild deletes the old clones and makes the new ones.
- A list can decide the order of the levels.
Quick-fire
You want ten coins in level 1. You could make ten Coin sprites. What is wrong with that, once there are two levels?
Reveal the answer
Level 2 would need ten more sprites, or ten sprites that move. One Coin sprite and a list of where the coins go scales to any number of coins and any number of levels. The list is the level design; the sprite is the machine that reads it.
Today’s Topic 3 min
- Two lists,
coin xandcoin y— one line per coin - One clone per line, placed like a screen
- Collected coins delete themselves
- Coins that belong to a level
Learning outcome
By the end of this lesson you will be able to:
- Place any number of collectables in a world from lists.
- Keep small things from gathering at the edge of the Stage.
- Give each level its own set of coins.
Learnt — coins 28 min
1 · Coins from lists
Make two lists, coin x and coin y. Line 1 of each is the first coin’s world position, line 2 the second, and so on. The Coin sprite reads them one line at a time and leaves a clone at each:
when I receive [build v]
if <(is clone) = (1)> then
delete this clone
else
hide
set [n v] to (0)
repeat (length of [coin x v])
change [n v] by (1)
set [my x v] to (item (n) of [coin x v])
set [my y v] to (item (n) of [coin y v])
create clone of (myself v)
end
end
when I start as a clone
set [is clone v] to (1)
set y to (my y)
set x to ((my x) - (scroll x))
wait until <touching (Pico v)?>
change [coins v] by (1)
delete this clone
How to use this: Click the green flag and watch Pico cross level 1 — and the coin counter.
2 · The edge, again
Lesson 3-33’s flag waited at the edge of the Stage. Coins do exactly the same, and there are ten of them. A coin can even tell it has been moved: its x position is not the x it asked for.
How to use this: Click the green flag and look at the right-hand edge of the Stage.
So a coin places itself exactly as a screen does, with the hide:
when I receive [scroll v]
if <(is clone) = (1)> then
set x to ((my x) - (scroll x))
if <([abs v] of ((my x) - (scroll x))) > (250)> then
hide
else
show
end
end
Basic — the coin run 24 min
Add coins to level 1 of your game.
Step 1 — draw a coin
- Paint a small gold coin — no green and no blue in it.
- Give it
my x,my y,nandis clone, all for this sprite only.
Step 2 — design the coins
- Make
coin xandcoin yfor all sprites, and tick them to show them on the Stage. - Type ten positions in. Stand Pico where you want a coin, and read its world x from Lesson 3-34.
- Put at least one where only a jump can reach it.
Step 3 — the machine
- Build the Coin’s build script and clone script from Learnt part 1.
- Add the scroll script from part 2, with its hide.
- Make
coins, set it to 0 when the flag is clicked, and untick the two lists to tidy the Stage.
Step 4 — play it through
- Collect every coin once. Check the counter reaches your total.
- Fall in the water and check the coins you already have stay collected.
How to use this: Click the green flag. Arrows to walk, space to jump. Collect all ten coins on the way to the flag.
All the coins are in one place.
my x is for all sprites, so every clone reads the last coin’s position. Make it for this sprite only.
There is one coin too many, sitting at the start.
The original sprite is showing. It hides in the build script and must never show — only clones should answer the scroll, which is why the scroll script checks is clone.
Coins pile up at the right-hand edge.
The hide is missing, or it tests x position. That is Learnt part 2.
A coin counts twice.
The delete this clone is missing after the count, so the clone is still touching Pico next tick.
A flag that waits for every coin
The finish flag stays down until every coin is collected — Cheese Maze’s locked cheese, in a scrolling world.
- It waits until
coinsequals(length of [coin x v]). - Then it switches to its raised costume.
- Touching it while it is down does nothing.
How to use this: Click the green flag. Pico crosses only the first screen and stops.
Teacher note
Comparing with the length of the list rather than typing 10 is the point: add a coin to the list and the flag still waits for all of them. Ask the class what would break if they had typed 10.
Coins that belong to a level
Level 2 needs its own coins, and level 1’s must not follow it.
- The build clears both lists first, then fills them for the current level.
- Old coin clones delete themselves on
build, like the screens. - Show how many coins each level has.
How to use this: Click the green flag. Level 1 is built and counted, then level 2.
Teacher reveal — filling the lists by level
delete all of [coin x v]
delete all of [coin y v]
if <(level) = (1)> then
add (60) to [coin x v]
add (-100) to [coin y v]
else
add (100) to [coin x v]
add (-100) to [coin y v]
end
Shortened to one coin a level. A tidier version keeps every level’s coins in one list and a third list saying which level each belongs to — the Extra of Lesson 3-35 in a new place.
Coins worth more
Make two of the coins stars, worth 5 each.
- A third list,
coin worth, the same length as the other two. - Each clone keeps its own
worthand wears the star costume if it is 5. - Collecting adds
worth, not 1.
How to use this: Click the green flag and watch the counter jump by 5 at each star.
Teacher note
Three lists kept in step, one line per thing — exactly the deck of cards in Card Battle. A pupil who spots that has understood what a list of records is for.
Ship it
Put the whole arc together into a scrolling game you would hand to somebody else.
- A title that explains the keys.
- Two levels, each with its own screens, sky and coins.
- Water, three lives, and a card between the levels.
- A best coin total that survives a new game.
How to use this: Click the green flag, read the title, then play: arrows to walk, space to jump. Two levels and every coin.
Teacher note
Notice what the arc added up to: one idea — everything has a world position — used four ways. The ground, the flag, the crab and the coins all place themselves with the same subtraction, and all of them hide the same way.
Summary 5 min
- Coins are one sprite, two lists, and one clone per line.
- Every clone places itself with
((my x) - (scroll x))and hides when it is off the Stage. - A collected coin deletes itself, so it can never be counted twice.
- Each level fills the lists with its own coins.
- A third list can give every coin its own worth.
- Collectable
- Something in a level that is picked up by touching it.
- Level data
- The lists that say where things go — separate from the code that puts them there.
- Record
- One line across several lists, all describing the same thing.
- Clamped
- Pulled back to the edge because Scratch will not let a sprite leave the Stage.
Hand in
Save your game. You have finished the Scroll World arc — a world bigger than the Stage, where everything knows where it lives.