- Learnt28 min · screens as states, a title, a best time, credits
- Basic25 min · Boss Tower, shipped
- Challenge 1–327 min · play again, skip the credits, a top three
- Extrabonus · a game that plays itself
Review — Boss Tower III 5 min
- A Pen health bar: grey, then red, ending at one sum.
- At half health the dragon changes its list and speeds up.
- A referee picks the ending:
winorlose.
Quick-fire
In Storm Flight, a higher score was better, so best started at 0. Today the record is a time, and lower is better. What goes wrong if best starts at 0?
Reveal the answer
No time is ever lower than 0, so no record is ever set. Today 0 will mean “no record yet”.
Today’s Topic 3 min
- The whole game as a state machine of screens
- A title screen and a how-to-play card
- A best time that survives the green flag
- Credits that scroll, naming everything you borrowed
Learning outcome
By the end of this lesson you will be able to:
- Move a whole game between screens with messages.
- Keep a record where lower is better.
- Ship a game a new player can start, finish and credit.
Learnt — the doors 28 min
1 · Screens are states too
The dragon has phases. The whole game has screens, and they work the same way: one state variable, and rules for moving on.
title --START--> fight --dragon at 0--> win --> credits --> title title --HOW TO PLAY--> how --click the card--> title fight --lives at 0--> lose --> title
Each screen is a message with the same name. Every sprite has a when I receive [title v], a when I receive [fight v] and so on, and shows or hides itself.
2 · The title and how to play
when flag clicked
broadcast [title v]
when I receive [title v]
set [state v] to [title]
go to x: (0) y: (90)
show
forever
repeat (10)
change y by (1)
wait (0.05) seconds
end
repeat (10)
change y by (-1)
wait (0.05) seconds
end
end
when I receive [fight v]
stop [other scripts in sprite v]
hide
when I receive [title v]
go to x: (0) y: (-20)
show
when this sprite clicked
if <(state) = [title]> then
broadcast [fight v]
end
when I receive [fight v]
hide
The HOW TO PLAY button sets state to how and broadcasts how. A Card sprite shows one rule per line, and a click on the card broadcasts title again.
How to use this: Click the green flag. Click HOW TO PLAY to open the card, then click the card to go back. Click START to leave the title.
3 · A best time
Three rules make a record you can trust:
- Never set
beston the green flag. It must outlast every game. - 0 means “no record yet”, so the first time always counts.
- After that, only a lower time counts.
when I receive [win v]
set [time v] to ((round (((timer) - (start)) * (10))) / (10))
say (join (join [Victory! ] (time)) [ seconds.]) for (2) seconds
if <<(best) = (0)> or <(time) < (best)>> then
set [best v] to (time)
say [A new best time!] for (1.5) seconds
end
start is set to the timer when the fight begins. The round-then-divide keeps one decimal place.How to use this: Click the green flag to start it. Nothing else to press — three finished fights are read off a list.
4 · The credits
Credits name everyone and everything your game borrowed. Every sprite, backdrop and sound from the Scratch library goes on the list.
when I receive [credits v]
set [state v] to [credits]
go to x: (0) y: (-460)
go to [front v] layer
show
glide (8) secs to x: (0) y: (460)
hide
broadcast [title v]
How to use this: Click the green flag to start it. Nothing else to press — the credits roll up the Stage.
5 · Shipping it
- Write the Instructions on the project page: the keys, and how to win.
- Write Notes and Credits: the same names as your credits screen.
- Do the cold test: save it, wait ten minutes, then play it from the title.
Basic — Boss Tower, shipped 25 min
Open your boss fight from last lesson.
Step 1 — the new sprites
- Paint four new sprites:
Title,Start,HowandCard. Use the text tool for the words. - Paint
Credits: one tall costume with every name on it. - New variables for all sprites:
start,timeandbest. Showbeston the Stage.
Step 2 — the front door
- Build the Title and START scripts from Learnt section 2. Build How and Card the same way.
- Change every
when flag clickedon the Dragon and the Knight towhen I receive [fight v]. Give them awhen flag clickedthat only hides them. - On the Knight’s fight script, add
set [start v] to (timer).
Step 3 — the back door
- Add the best-time check from section 3 to the Knight’s win script.
- At the end of it:
broadcast [credits v]. At the end of the lose script:broadcast [title v]. - Build the credits script from section 4.
- Every game sprite gets
when I receive [title v]withstop [other scripts in sprite v]andhide.
How to use this: Click the green flag, then click START. Click the stage once and hold ← or → to move the Knight, and press the space bar to throw a bolt. Click HOW TO PLAY on the title to read the rules.
The fight starts behind the title screen.
A game sprite still has a when flag clicked that starts the fight. Only the Title should do anything on the green flag.
The best time goes back to 0 every game.
There is a set [best v] to (0) somewhere. Delete it — that is the whole rule.
The second game starts with the dragon already beaten.
The fight script must set set [boss hp v] to (max hp) and the lives before the referee looks.
Play again
A player who just lost wants another go, not the title screen.
- After either ending, a Play Again button appears.
- Clicking it starts a fresh fight straight away.
How to use this: Click the green flag, then click START. Click the stage once, hold ← or → and press the space bar. This dragon has only 2 health. On the ending, click Play Again.
Teacher reveal
The Knight’s endings broadcast again? instead of credits or title. Then, on the Again sprite:
when I receive [again? v]
set [state v] to [again?]
show
when this sprite clicked
if <(state) = [again?]> then
hide
broadcast [fight v]
end
Fast-forward the credits
Credits are polite. Making someone watch them ten times is not.
- Holding the space bar makes the credits roll four times faster.
- Letting go slows them down again.
How to use this: Click the green flag, then click the stage once. Hold the space bar to speed the credits up.
Teacher reveal
A glide cannot change speed halfway, so swap it for a loop:
repeat until <(y position) > (455)>
if <key (space v) pressed?> then
change y by (8)
else
change y by (2)
end
end
A top three
One record is good. A board is better.
- A
top threelist, shown on the Stage, fastest first. - Each new time goes in at its right place — no sorting afterwards.
- The list never holds more than three times.
How to use this: Click the green flag to start it. Nothing else to press — five finished fights are read off a list.
Teacher reveal
define record (t)
set [i v] to (1)
repeat until <<(i) > (length of [top three v])> or <(t) < (item (i) of [top three v])>>
change [i v] by (1)
end
insert (t) at (i) of [top three v]
if <(length of [top three v]) > (3)> then
delete (4) of [top three v]
end
Walk down the list until you meet a slower time, and slide in above it. Bubble sort in Level 3 fixed a whole list; this keeps a list fixed one item at a time.
Attract mode
Arcade machines play themselves when nobody is using them. Yours can too.
- If the title sits untouched for ten seconds, a demo fight starts.
- The knight plays itself: it reads each warning and moves to safety.
- Any key ends the demo and goes back to the title.
How to use this: Click the green flag and leave it alone — the demo starts after 5 seconds here. Then click the stage once and press any key to go back to the title.
Teacher reveal
when I receive [demo v]
set [state v] to [demo]
forever
wait until <(phase) = [wind-up]>
if <(move) = [rain]> then
glide (0.4) secs to x: ((-270) + ((gap) * (90))) y: (-115)
end
if <(move) = [sweep]> then
glide (0.5) secs to x: (-205) y: (-115)
end
if <(move) = [fireball]> then
glide (0.4) secs to x: ((x position) + (100)) y: (-115)
end
set [knight x v] to (x position)
wait until <(phase) = [recover]>
end
when I receive [demo v]
wait until <key (any v) pressed?>
broadcast [title v]
The demo’s fireball step turns back when it nears the right wall. The Title waits with wait until <<(timer) > ((quiet since) + (10))> or <not <(state) = [title]>>> and only starts the demo if the state is still title.
Summary 5 min
- A whole game is a state machine of screens, moved along by messages.
- Every sprite answers every screen message: show, hide, start or stop.
- A best time is never reset on the green flag; 0 means “no record”, and lower wins.
- Credits name every sprite, backdrop and sound you borrowed.
- Title screen
- The first thing a player sees: the name and a way to start.
- Credits
- The list of who and what made the game.
- Record
- The best result so far, kept between games.
- Attract mode
- A game playing itself while it waits for a player.
Hand in
Save Boss Tower — the arc is finished, and so is the castle. Next lesson the games pause for animation: you will build a character from cut-out pieces, like a paper puppet, and make it move.