- Learnt28 min · timer ticks, speed, walls, wrap
- Basic25 min · a snake in a walled garden that speeds up
- Challenge 1–327 min · levels, portals, pause
- Extrabonus · draw your own walls
Review — Snake II 5 min
- Apples land on a random cell, rolled again while the cell is on the snake.
- Eating skips the delete, so the snake grows.
- A crash is the new head found in the body, from item 2, after the tail has moved.
Quick-fire
Your loop has wait (0.2) seconds and nothing else slow. How long do 30 loops take?
Reveal the answer
Longer than 6 seconds. Every block in the loop takes a little time too, and Scratch only moves on when the next frame is drawn. The demo in section 1 measures it.
Today’s Topic 3 min
- A tick driven by the
timer, not by counting waits - Speed that grows with the snake’s length, with a limit
- Walls typed as rows of letters — Tile Maker’s format — stamped once
- Wrap-around with
mod
Learning outcome
By the end of this lesson you will be able to:
- Keep a steady beat with the timer.
- Read a level from a list of text and ask what is in any cell.
- Redraw only what changed, instead of everything.
Learnt — time, speed and walls 28 min
1 · A tick on the timer
wait (0.2) seconds waits 0.2 seconds on top of everything else the loop does. Instead, keep the time of the next tick in next, and step whenever the timer passes it.
when flag clicked
reset timer
set [next v] to (0)
forever
wait until <(timer) > (next)>
change [next v] by (gap)
step :: custom
draw changes :: custom
end
next goes up by exactly gap each time, so a slow tick never pushes the rest late.How to use this: Click the green flag to start it. Nothing else to press — watch the timer monitor.
How to use this: Click the green flag to start it. Nothing else to press.
2 · Faster as it grows
Work out gap from the length after every tick. Start at 0.25 s, take 0.01 off for every cell past 3 — but never go below 0.08, or nobody could steer.
set [gap v] to ((0.25) - ((0.01) * ((length of [xs v]) - (3))))
if <(gap) < (0.08)> then
set [gap v] to (0.08)
end
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 — the snake eats a row of ten apples.
3 · Walls are letters
A level is a list called level: 12 items, one per row, each 16 letters long. . is grass and # is a wall — exactly like Tile Maker. A Wall sprite stamps every # once, at the start.
define tile at (c) (r)
set [tile v] to (letter (c) of (item (r) of [level v]))
step, after the new head is worked out: tile at (c) (r) :: custom. If tile is #, the snake hit a wall.
define build walls
set [wr v] to (1)
repeat (12)
set [wc v] to (1)
repeat (16)
if <(letter (wc) of (item (wr) of [level v])) = [#]> then
go to cell (wc) (wr) :: custom
stamp
end
change [wc v] by (1)
end
change [wr v] by (1)
end
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 — the route turns down into a wall.
How to use this: Click the green flag to start it. Nothing else to press.
The fix is to stop redrawing everything. Each tick only two cells change: the old head becomes body, and the old tail becomes grass again. Our backdrop has a cell of each green as a costume (grass light where c + r is even, grass dark where it is odd).
define draw changes
if <(length of [xs v]) > (1)> then
switch costume to [body v]
go to cell (item (2) of [xs v]) (item (2) of [ys v]) :: custom
stamp
end
switch costume to [head v]
point in direction (dir)
go to cell (item (1) of [xs v]) (item (1) of [ys v]) :: custom
step, just before the tail is deleted, paint its cell back to grass with the right costume and a stamp.4 · Wrap-around with mod
Lesson 41’s wrap needed four ifs. mod does it in one line per list: it is the remainder after dividing, and in Scratch it is never negative.
set [c v] to ((((c) - (1)) mod (16)) + (1))
set [r v] to ((((r) - (1)) mod (12)) + (1))
How to use this: Click the green flag to start it. Nothing else to press.
Basic — a walled garden that speeds up 25 min
Open your snake from Lesson 42.
Step 1 — the level and the walls
- Make a list
leveland type in 12 rows: a full row of#top and bottom, and#at both ends of every row between. - Add a Wall sprite with wall.svg. Give it
build walls, run when it receivesbuild.
Step 2 — draw only what changes
- Upload grass-light.svg and grass-dark.svg to the Snake.
- Build
draw changes, and paint the tail back to grass instep. - At the green flag:
erase all, thenbroadcast [build] and wait, then draw the whole snake once.
Step 3 — the timer and the walls
- Swap the wait in the loop for the timer tick from Learnt section 1.
- Work out
gapafter every tick (Learnt section 2). - Add
tile at. Afterstep: iftileis#, say Hit a wall! and stop. - Keep apples off the walls: pick columns 2–15 and rows 2–11.
How to use this: Click the green flag, then steer with the arrow keys. Click the Stage first so it hears the keys.
The snake leaves a trail of body behind it.
The tail is never painted back to grass. The grass stamp must go before the delete, while you still know where the tail is.
Grass squares appear in the wrong green.
The mod test is the wrong way round. Light grass is where (c + r) mod 2 = 0.
After a while the game races through several ticks at once.
Something set next back, or a long say … for … seconds sat inside the loop. The timer kept counting, so the loop catches up. Challenge 3 is about exactly this.
Levels
Five apples on the open garden, then level 2: the pillars.
- Keep a
level novariable. - When the score reaches 5 on level 1: fill
levelwith level 2’s rows and build the walls. - Set
nextto the timer afterwards, so the pause for the new level does not make it race.
How to use this: Click the green flag to start it. Nothing else to press.
Teacher reveal
if <<(score) = (5)> and <(level no) = (1)>> then
set [level no v] to (2)
delete all of [level v]
add [................] to [level v]
add [................] to [level v]
add [...##......##...] to [level v]
say [Level 2!] for (1) seconds
set [next v] to (timer)
end
… with all 12 rows, and broadcast [build] and wait before the say.
Portals
Two O letters in the level are a pair of portals. Steer into one and come out of the other, still going the same way.
- Upload portal.svg to the Wall sprite and stamp it for every
O. - In
step: if the new head’s tile isO, move it to the other portal, then one more cell on.
How to use this: Click the green flag to start it. Nothing else to press.
Teacher reveal
tile at (c) (r) :: custom
if <(tile) = [O]> then
if <<(c) = (2)> and <(r) = (2)>> then
set [c v] to (15)
set [r v] to (11)
else
set [c v] to (2)
set [r v] to (2)
end
change [c v] by (dx)
change [r v] by (dy)
end
This goes before the two insert blocks in step.
Pause
Press p to pause. The catch: the timer does not stop. Unpause and the loop sees it is two seconds behind — and races through ten ticks at once.
- While paused, the loop waits.
- When it carries on, push
nexton by however long the pause lasted.
How to use this: Click the green flag to start it. Nothing else to press — it pauses itself for 2 seconds on tick 4.
Teacher reveal
when [p v] key pressed
if <(paused) = (0)> then
set [paused v] to (1)
set [paused at v] to (timer)
else
set [paused v] to (0)
change [next v] by ((timer) - (paused at))
end
And in the loop: wait until <(paused) = (0)> before the timer check.
Draw your own walls
Tile Maker’s editor, for Snake: click a cell to add or remove a wall, then press space to play.
- Turn the mouse into a cell:
floor((mouse x + 240) / 30) + 1andfloor((180 − mouse y) / 30) + 1. - A click rebuilds that row letter by letter, swapping the clicked letter between
.and#. - Then
erase alland build the walls again.
How to use this: Click the green flag, then click cells on the Stage to add or remove walls. Press space when you are done.
Summary 5 min
- A
waitin a loop drifts. A tick on the timer keeps a steady beat. - Speed comes from a sum on the length, with a floor so it never gets impossible.
- A level is a list of text.
letter (c) of (item (r) of level)tells you what is in any cell. - Redraw only what changed — the head and the tail — and stamped walls survive.
modwraps a number round, in one block.
- Timer
- Scratch’s stopwatch: seconds since it was last reset.
- Drift
- A small delay that adds up, tick after tick, until the game is noticeably slow.
- mod
- The remainder after dividing. 17 mod 16 is 1.
Hand in
Save your game. Next lesson a second snake joins — on the same keyboard — and the hardest question is who crashed first.