- Learnt28 min · two pairs of lists, a fair referee, a best score, a title
- Basic25 min · Snake Duel
- Challenge 1–327 min · best of three, swapping heads, first to three apples
- Extrabonus · a computer player
Review — Snake III 5 min
- The tick runs on the timer, so the beat stays steady.
- Only two cells are drawn each tick: the old head becomes body, the old tail becomes grass.
- Walls come from a list of letters, exactly like Tile Maker’s levels.
Quick-fire
Two snakes move at once. Blue moves first, then checks whether it crashed. Is that fair to Orange?
Reveal the answer
No. Blue is checking against where Orange was, not where it is about to be. Section 2 shows the game it gives the wrong winner.
Today’s Topic 3 min
- A second snake with its own pair of lists, on its own keys
- A referee on the Stage: move both, then judge both
- Head-on, body hits and draws
- A best score that survives the green flag, and a title screen
Learning outcome
By the end of this lesson you will be able to:
- Run two copies of the same game logic on two sets of data.
- Explain why “everyone moves, then we check” is fairer than checking as each one moves.
- Finish a game with a front door and a result.
Learnt — a fair duel 28 min
1 · Two snakes, two pairs of lists
Blue keeps xs and ys. Orange gets its own: xs 2 and ys 2, with dx 2, dy 2, c 2 and r 2. Duplicate the Blue sprite, then change every list and variable in the copy.
Blue steers with W A S D; Orange with the arrow keys. Each sprite answers two messages: move (work out the new head, insert it, eat or delete the tail) and show (draw its two changed cells).
when I receive [move v]
move :: custom
when I receive [show v]
show :: custom
when [d v] key pressed
steer [R] :: custom
move 2, show 2 and the arrow keys.How to use this: Click the green flag to start it. Nothing else to press — both snakes follow routes.
2 · A referee: move both, then judge
The Stage runs the game. Each tick it tells both snakes to move and waits, and only then asks what happened:
when flag clicked
erase all
set [result v] to []
broadcast [new game] and wait
repeat until <not <(result) = []>>
wait (0.2) seconds
broadcast [move v] and wait
judge :: custom
broadcast [show v] and wait
end
say (result)
define judge
set [out v] to (0)
set [out 2 v] to (0)
on blue (c) (r) (2) :: custom
if <(hit) = (1)> then
set [out v] to (1)
end
on orange (c) (r) (2) :: custom
if <(hit) = (1)> then
set [out v] to (1)
end
on orange (c 2) (r 2) (2) :: custom
if <(hit) = (1)> then
set [out 2 v] to (1)
end
on blue (c 2) (r 2) (2) :: custom
if <(hit) = (1)> then
set [out 2 v] to (1)
end
if <<(c) = (c 2)> and <(r) = (r 2)>> then
set [out v] to (1)
set [out 2 v] to (1)
end
if <<(out) = (1)> and <(out 2) = (1)>> then
set [result v] to [A draw - both crashed.]
else
if <(out) = (1)> then
set [result v] to [Orange wins!]
end
if <(out 2) = (1)> then
set [result v] to [Blue wins!]
end
end
on blue and on orange are Lesson 42’s search, one for each pair of lists. Add the off-the-grid checks too: a head off the grid is out.How to use this: Click the green flag to start it. Nothing else to press.
How to use this: Click the green flag to start it. Nothing else to press.
How to use this: Click the green flag to start it. Nothing else to press.
3 · A best score that survives the green flag
A variable keeps its value until a block changes it. So never reset best at the green flag — only raise it when a game beats it. It is still there next time you click the flag, and it is saved with the project.
if <(score) > (best)> then
set [best v] to (score)
end
How to use this: Click the green flag to start it. Nothing else to press — it plays three short games.
4 · A front door
A title card waits for the space bar before anything moves. Upload title.svg as a sprite.
when flag clicked
show
go to [front v] layer
wait until <key [space v] pressed?>
hide
broadcast [play v]
when I receive [play], not the green flag.How to use this: Click the green flag, then press space to start.
Basic — Snake Duel 25 min
Open your snake from Lesson 43. Remove the walls for now — the garden is crowded enough with two snakes.
Step 1 — the second snake
- Duplicate the Snake sprite. Rename them
BlueandOrange. Upload head-orange.svg and body-orange.svg to Orange. - Make
xs 2,ys 2,dx 2,dy 2,dir 2,c 2,r 2andscore 2. Change every block on Orange to use them. - Blue starts at column 5, row 4, going right. Orange starts at column 12, row 9, going left.
Step 2 — the referee
- Move the game loop to the Stage. The sprites only answer
new game,moveandshow. - Build
on blue,on orangeandjudgeon the Stage. - The apple is eaten by whichever head reaches it. The referee places a new one, off both snakes.
Step 3 — the end
- Upload game-over.svg. It shows the result and the best score ever.
How to use this: Click the green flag. Blue steers with W A S D, Orange with the arrow keys. Click the Stage first so it hears the keys.
Orange moves when Blue’s keys are pressed.
A key script on Orange still calls steer, not steer 2. Each sprite needs its own steering block for its own variables.
A snake crashes into the other one’s old tail.
The referee judged before both had moved. Check the order: move and wait, then judge.
Scratch freezes when the apple is placed.
Your re-roll is waiting for something that never happens. If you save hit from the first search in a variable, do not call it k — on orange uses k to count, so it is never 0.
Best of three
One crash should not decide everything. Play rounds until somebody has won two.
- Count
blue winsandorange wins. A draw counts for nobody. - Each round starts the snakes from scratch.
- Announce who took the match, and the score.
How to use this: Click the green flag to start it. Nothing else to press — three rounds are played on routes.
Teacher reveal
repeat until <<(blue wins) = (2)> or <(orange wins) = (2)>>
change [round v] by (1)
set [result v] to []
broadcast [new game] and wait
repeat until <not <(result) = []>>
wait (0.2) seconds
broadcast [move v] and wait
judge :: custom
broadcast [show v] and wait
end
if <(result) = [Blue wins!]> then
change [blue wins v] by (1)
end
if <(result) = [Orange wins!]> then
change [orange wins v] by (1)
end
end
Swapping heads
Two snakes face each other, one cell apart. Next tick they swap cells — the heads never share a cell. Does your referee notice?
- Work out on paper where both snakes are after the swap.
- Find which check in
judgecatches it. - Make sure the answer is a draw.
How to use this: Click the green flag to start it. Nothing else to press.
Teacher reveal
After the swap, Blue’s head is on Orange’s item 2 (Orange’s old head), and Orange’s head is on Blue’s item 2. The body checks start at item 2, so both are out: a draw. Judging after both have moved is what makes this work.
First to three apples
A second way to win: the first snake to eat three apples wins, crash or no crash.
- After
judge, check both scores. - The loop now stops when there is a result or somebody reaches 3.
How to use this: Click the green flag to start it. Nothing else to press.
Teacher reveal
repeat until <<not <(result) = []>> or <<(score) > (2)> or <(score 2) > (2)>>>
wait (0.2) seconds
broadcast [move v] and wait
judge :: custom
broadcast [show v] and wait
end
if <(score) > (2)> then
set [result v] to [Blue wins - 3 apples!]
end
if <(score 2) > (2)> then
set [result v] to [Orange wins - 3 apples!]
end
A computer player
Play Snake Duel alone: Orange steers itself, using Lesson 42’s hunting idea.
- Before each move the referee broadcasts
thinkand waits. - Orange tries the ways towards the apple first. A way is no good if it leads off the grid or onto either snake.
- If no way towards the apple is safe, any safe way will do.
How to use this: Click the green flag to start it. Nothing else to press — Blue follows a route, Orange thinks for itself.
Summary 5 min
- Two players are the same logic on two sets of lists and variables.
- A referee moves everyone first, then judges everyone — that is what makes it fair.
- Head-on, body hits and swaps all fall out of the same checks.
- A best score is a variable you never reset; a title is a sprite that waits for a key.
- Referee
- One script that runs the turns and decides what happened, so no player decides for itself.
- Head-on
- Both heads arriving in the same cell on the same tick.
- Persist
- To keep a value between runs — the best score persists.
Hand in
Save Snake Duel. The last arc of Level 4 starts next lesson: a classmate remixes it, and you learn to give — and get — credit properly.