- Learnt28 min · two kinds of memory, a title screen, counting once, Play Again
- Basic24 min · finish The Castle Quest
- Challenge 1–327 min · the count in words, all four found, the name once
- Extrabonus · the Secret Path
Review — where we got to 5 min
(has key)and(has apple)remember what the reader picked up.- The storyteller sets them back to no at the start of every story.
<<> and <>>needs both sides true. The fussiest check goes first.- Ask once, then
(join [Hello, ] (name))anywhere.
Quick-fire
You play the story twice without pressing the flag in between. The second time, the door opens though you left the key. Why?
Reveal the answer
has key is still yes from the first story. Something has to set it back to no when a new story starts. Today we decide carefully what gets reset — and what must not be.
Today’s Topic 3 min
- Two kinds of memory: for one story, and for ever
- A title screen, run by a message
(endings found)— a count the green flag leaves alone- Counting each ending once, with
<not <>> - Play Again and Clear Endings
Learning outcome
By the end of this lesson you will be able to:
- Build a title screen with a Start button and a Clear Endings button.
- Count each ending once, however many times it is reached.
- Choose which variables a new story resets, and which it keeps.
Learnt — a story you play again 28 min
Two kinds of memory
A good story game makes you want to find every ending. So it has to remember two different things.
| Variable | Remembers | Set back by |
|---|---|---|
(has key), (has apple) | What happened in this story | every new story |
(endings found) | How many different endings you have ever found | only the Clear Endings button |
The title screen
The green flag no longer starts the story. It shows the title screen:
when flag clicked
switch backdrop to [Castle 2 v]
broadcast [title screen v]
Three new sprites listen for it: a title card, a Start button and a Clear Endings button.
when I receive [title screen v]
show
when this sprite clicked
broadcast [new story v]
when I receive [new story v]
hide
when I receive [title screen v]
show
when I receive [new story v]
hide
when this sprite clicked
set [endings found v] to (0)
And the story? Take the Wizard’s old when flag clicked stack. Swap its hat for when I receive [new story v]. Nothing else in it changes.
How to use this: Click the green flag. Click Clear Endings and watch endings found, then click Start.
Every ending is a message
Last lesson, each ending already had its own message: lake ending, locked ending, hungry ending and friend ending. That makes counting easy. One more script can listen for each.
The first idea is wrong:
when I receive [lake ending v]
change [endings found v] by (1)
Count each ending once
Each ending gets its own yes / no variable: found lake, found locked, found hungry, found friend. The count only goes up the first time.
when I receive [lake ending v]
if <not <(found lake) = [yes]>> then
set [found lake v] to [yes]
change [endings found v] by (1)
end
How to use this: Click the green flag. Click The Lake three times and watch endings found. Then click The Castle, then Clear Endings.
The end, and Play Again
Both of the Wizard’s endings — the door and the lake — finish with one more block: broadcast [the end v]. A Play Again button listens for it.
when flag clicked
hide
when I receive [the end v]
show
when this sprite clicked
broadcast [new story v]
when I receive [new story v]
hide
| You press | has key, has apple | endings found |
|---|---|---|
| Start or Play Again | set to no | kept |
| the green flag | kept (until you press Start) | kept |
| Clear Endings | kept | set to 0 |
Basic — finish The Castle Quest 24 min
Step 1 — the title screen pieces
- Add the Castle 2 backdrop.
- Paint a title: a new sprite, the T tool, big letters — The Castle Quest.
- Make three more buttons from Button2: Start, Clear Endings and Play Again. Hide all four sprites.
Step 2 — five variables
endings found, and one yes / no for each ending: found lake, found locked, found hungry, found friend. Show only endings found on the Stage.
Step 3 — the Wizard’s start
Build the short flag script. Then pull the old flag stack away from its hat and give it when I receive [new story v] instead:
when I receive [new story v]
set [has key v] to [no]
set [has apple v] to [no]
switch backdrop to [Forest v]
ask [What is your name?] and wait
set [name v] to (answer)
broadcast [forest scene v] and wait
broadcast [fork v]
Step 4 — the buttons
Give Start and Play Again their stacks from Learnt. The title card shows on title screen and hides on new story. Clear Endings forgets everything about endings:
when I receive [title screen v]
show
when I receive [new story v]
hide
when this sprite clicked
set [endings found v] to (0)
set [found lake v] to [no]
set [found locked v] to [no]
set [found hungry v] to [no]
set [found friend v] to [no]
Step 5 — four counting scripts
On the Wizard, build the lake ending counter from Learnt. Duplicate it three times. On each copy change the message and both variable dropdowns.
Step 6 — the end of every path
when I receive [go door v]
switch backdrop to [Castle 1 v]
broadcast [door scene v] and wait
if <<(has key) = [yes]> and <(has apple) = [yes]>> then
switch backdrop to [Castle 3 v]
broadcast [friend ending v] and wait
else
if <(has key) = [yes]> then
switch backdrop to [Castle 3 v]
broadcast [hungry ending v] and wait
else
broadcast [locked ending v] and wait
end
end
say [The end!] for (2) seconds
broadcast [the end v]
when I receive [go lake v]
switch backdrop to [Underwater 1 v]
broadcast [lake ending v] and wait
say [Gobo plays with the fish all day.] for (2.5) seconds
say [The end!] for (2) seconds
broadcast [the end v]
Step 7 — tidy the title screen
Leftovers from the last story must not show on the title.
when I receive [title screen v]
hide
when backdrop switches to [Castle 2 v]
go to x: (160) y: (-100)
Step 8 — find them all
Play until you have found all four endings. Press the flag between some of them. The count must never drop — until you click Clear Endings.
How to use this: Click the green flag, then click Start. Type your name in the box and press Enter. Click the apple if you like, then The Woods or The Lake; in the woods, the key if you like, then Walk On. Click Play Again to go again.
The count goes back to 0 when I press the flag.
Some when flag clicked script still sets (endings found). Search every sprite and the Stage for it. Only Clear Endings may set it.
The same ending counts twice.
That counter is missing its if, or its dropdowns still say found lake after you duplicated it.
The first ending never counts.
The check says <(found lake) = [no]>. A new variable is 0, not no. Use the <not <>> version.
Play Again does nothing.
The Wizard’s story stack still starts with when flag clicked. Its hat must be when I receive [new story v].
The count, in words
- On the title screen, the Wizard says how many endings you have found.
- In a full sentence: You have found 2 of 4 endings.
- The bubble stays until the story starts.
Teacher reveal
when I receive [title screen v]
say (join (join [You have found ] (endings found)) [ of 4 endings.])
A join inside a join, as in Lesson 23. Plain say [Hello!] with no seconds keeps the bubble up; the Wizard’s first line in the story replaces it. Watch for the two spaces, either side of the number.
All four found
- When endings found is 4, the title screen says You found every ending!
- The Dragon flies onto the title screen to celebrate.
- Below 4, the title screen is the ordinary one.
How to use this: Click the green flag to see the title screen. Click Clear Endings, then the green flag again to compare.
Teacher reveal
when I receive [title screen v]
if <(endings found) = (4)> then
switch costume to [dragon-b v]
go to x: (80) y: (30)
show
else
hide
end
On the Dragon, replacing its plain title-screen hide. The Wizard’s line from Challenge 1 gets the same if / else.
Ask the name once
- The first story asks for the reader’s name.
- Play Again and Start do not ask again. They already know it.
- Clear Endings forgets the name too — a new reader is starting.
How to use this: Click the green flag, then click Start and type your name in the box and press Enter. Finish the story, then click Play Again.
Teacher reveal
if <(name) = []> then
ask [What is your name?] and wait
set [name v] to (answer)
end
In the new story stack, around the ask. Clear Endings gains set [name v] to []. The empty box is the whole trick — it is how the story knows nobody has told it a name yet.
The Secret Path
- Once all four endings are found, the fork shows a third button: Secret Path.
- It leads to a secret ending: Gobo and the Dragon fly over the castle.
- The secret ending does not change endings found. It is a reward, not a fifth ending.
How to use this: Click the green flag, then click Start. Type your name in the box and press Enter, then click Secret Path when the buttons appear.
Summary 3 min
- Some things are remembered for one story. Some are remembered for ever.
- A variable only changes when a block changes it. Keep the count out of every flag script.
- A title screen is a message. The story starts from another message.
- A yes / no for each ending, and
<not <>>, counts each one once. - Play Again resets the story. Clear Endings resets the count.
- Title screen
- The first thing a player sees, before the game or story starts.
- Counter
- A variable that goes up by one each time something happens.
- Flag (yes / no)
- A variable that remembers whether something has happened yet.
- Replay
- Playing again, to find what you missed the first time.
Ship it
That is the whole Choose Your Path arc. Before you share it:
- Click Clear Endings, so a new reader starts at 0.
- Write two lines of instructions on the project page: what to click, and that there are four endings.
- Share it on scratch.mit.edu if you have an account, and send the link to your teacher.
Next lesson starts a new project: the Monster Maker.