- Learnt28 min · a random cell, searching two lists, eating, crashing
- Basic25 min · the real game: apples and a score
- Challenge 1–327 min · grow by three, a golden apple, poison
- Extrabonus · a snake that hunts apples on its own
Review — Snake I 5 min
- The snake is two lists,
xsandys. Item 1 is the head. - Each tick a new head is inserted at item 1 and the last item is deleted — a queue.
- The body is drawn by stamping one cell for every item.
Quick-fire
A snake is 5 long. In one tick, how many items go into xs, and how many come out?
Reveal the answer
One in, one out — so it stays 5 long. Today we find out what happens when one tick skips the “out”.
Today’s Topic 3 min
- An apple on a random cell — but never on the snake
- Searching two lists at once: is this column and row in the snake?
- Eating means skipping the delete, so the snake grows
- A crash when the head lands on the body
Learning outcome
By the end of this lesson you will be able to:
- Search two lists in step for a pair of values.
- Re-roll a random choice until it is allowed.
- Explain why the order of “delete the tail” and “check for a crash” matters.
Learnt — apples and crashes 28 min
1 · An apple on a random cell
Keep the apple’s cell in ac (column) and ar (row). The Apple sprite just follows them for ever, using the same cell sum as the snake.
when flag clicked
set size to (55) %
forever
go to x: ((-225) + ((30) * ((ac) - (1)))) y: ((165) - ((30) * ((ar) - (1))))
end
How to use this: Click the green flag to start it. Nothing else to press — it picks five cells.
2 · Is this cell on the snake? Ask both lists
An apple must never appear under the snake. (item # of (9) in [xs v]) looks tempting — but it only searches the columns. It finds the first 9 in xs, whatever row goes with it.
How to use this: Click the green flag to start it. Nothing else to press — read the bubbles.
So walk the lists yourself, and check the column and the row at the same item number:
define on snake (c) (r) (from)
set [hit v] to (0)
set [k v] to (from)
repeat until <<(k) > (length of [xs v])> or <(hit) = (1)>>
if <<(item (k) of [xs v]) = (c)> and <(item (k) of [ys v]) = (r)>> then
set [hit v] to (1)
end
change [k v] by (1)
end
hit is 1 if the cell is on the snake. from lets us skip the head when we need to — you will see why in section 4.
define place apple
set [ac v] to (pick random (1) to (16))
set [ar v] to (pick random (1) to (12))
on snake (ac) (ar) (1) :: custom
repeat until <(hit) = (0)>
set [ac v] to (pick random (1) to (16))
set [ar v] to (pick random (1) to (12))
on snake (ac) (ar) (1) :: custom
end
How to use this: Click the green flag to start it. Nothing else to press — 20 apples are placed on a crowded garden.
3 · Eating is skipping the delete
When the new head lands on the apple, do not delete the tail this tick. The snake is one longer. That is the whole of “growing”.
define step
set [c v] to ((item (1) of [xs v]) + (dx))
set [r v] to ((item (1) of [ys v]) + (dy))
insert (c) at (1) of [xs v]
insert (r) at (1) of [ys v]
if <<(c) = (ac)> and <(r) = (ar)>> then
change [score v] by (1)
place apple :: custom
else
delete (length of [xs v]) of [xs v]
delete (length of [ys v]) of [ys v]
end
on snake (c) (r) (2) :: custom
How to use this: Click the green flag to start it. Nothing else to press — the snake follows a route through three apples.
4 · Crashing into yourself
The last line of step asks whether the new head is anywhere in the body — from item 2, because item 1 is the head itself. If hit is 1, the game is over.
if <(hit) = (1)> then
say (join [Crashed into myself! Score ] (score))
stop [this script v]
end
step.How to use this: Click the green flag to start it. Nothing else to press — the route turns the snake back across itself.
How to use this: Click the green flag to start it. Nothing else to press — this one checks BEFORE the delete.
How to use this: Click the green flag to start it. Nothing else to press — the same route, checking AFTER the delete.
Basic — apples and a score 25 min
Open your snake from Lesson 41.
Step 1 — the apple
- Add the Apple sprite from the library. Give it the follow script from Learnt section 1.
- Make variables
ac,ar,hit,kandscore.
Step 2 — the My Blocks
- On the Snake, build
on snakeandplace apple. - Change
stepto the version in Learnt section 3.
Step 3 — the loop
- After the lists are filled at the green flag:
set [score v] to (0)andplace apple :: custom. - In the forever loop, after the edge check, add the crash check from Learnt section 4.
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 grows but the apple stays where it is.
place apple is not in the eating branch of step, so the apple never moves — and the next tick “eats” it again.
The game says I crashed into myself on the very first tick.
The search starts at item 1, which is the new head — so it always finds itself. Use on snake (c) (r) (2) :: custom.
Scratch freezes when the snake is very long.
Tick Run without screen refresh on on snake and place apple. Without it, the search draws a frame for every item it checks.
Grow by three
Make each apple worth three cells, not one.
- Add a variable
grow. Eating an apple adds 2 to it — the tick you eat already skips one delete. - On a tick with no apple: if
growis more than 0, take 1 off it and skip the delete.
How to use this: Click the green flag to start it. Nothing else to press.
Teacher reveal
if <<(c) = (ac)> and <(r) = (ar)>> then
change [score v] by (1)
change [grow v] by (2)
place apple :: custom
else
if <(grow) > (0)> then
change [grow v] by (-1)
else
delete (length of [xs v]) of [xs v]
delete (length of [ys v]) of [ys v]
end
end
A golden apple
A second, golden apple is worth 3 points — but it only stays for 15 ticks.
- Upload golden.svg as a new sprite.
- Keep its cell in
gold candgold r, and a countdown ingold left. - Every tick, count down. At 0 the golden apple hides. Eating it scores 3.
How to use this: Click the green flag to start it. Nothing else to press.
Teacher reveal
In the loop, after draw:
change [gold left v] by (-1)
if <<(item (1) of [xs v]) = (gold c)> and <(item (1) of [ys v]) = (gold r)>> then
change [score v] by (3)
set [gold left v] to (0)
end
The Golden sprite shows itself only while gold left is more than 0.
Poison
A purple apple shrinks the snake: two cells off the tail.
- Upload poison.svg.
- Eating it deletes the last item twice — in both lists.
- Redraw straight away, so the shorter snake shows at once.
How to use this: Click the green flag to start it. Nothing else to press.
Teacher reveal
if <<(item (1) of [xs v]) = (pc)> and <(item (1) of [ys v]) = (pr)>> then
repeat (2)
delete (length of [xs v]) of [xs v]
delete (length of [ys v]) of [ys v]
end
set [pc v] to (0)
draw :: custom
end
A snake that hunts
No keys. Every tick the snake picks a way towards the apple that will not crash.
- A My Block
safe (way)works out the cell one step that way, and setshitif it is off the grid or on the snake. - Try the ways that get closer to the apple first. If none is safe, try any way that is.
How to use this: Click the green flag to start it. Nothing else to press.
Teacher reveal
define safe (way)
set [try c v] to (item (1) of [xs v])
set [try r v] to (item (1) of [ys v])
if <(way) = [R]> then
change [try c v] by (1)
end
if <(way) = [L]> then
change [try c v] by (-1)
end
if <(way) = [U]> then
change [try r v] by (-1)
end
if <(way) = [D]> then
change [try r v] by (1)
end
if <<<(try c) < (1)> or <(try c) > (16)>> or <<(try r) < (1)> or <(try r) > (12)>>> then
set [hit v] to (1)
else
on snake (try c) (try r) (1) :: custom
end
Summary 5 min
item # ofsearches one list. A cell is a pair, so walk both lists and compare both at the same item number.- A random choice that is not allowed is simply rolled again.
- Growing is not a new block — it is skipping the delete for one tick.
- Check for a crash after the tail has moved, starting from item 2.
- Search
- Looking through a list item by item until you find what you want, or reach the end.
- Re-roll
- Picking at random again because the first pick was not allowed.
- Greedy
- A plan that always takes the move that looks best right now.
Hand in
Save your game. Next lesson it gets faster as it grows, walls appear — and the wait in the loop turns out to be lying about the time.