- Learnt28 min · two loops, two key sets, one arena
- Basic24 min · the face-off
- Challenge 1–327 min · fair speed, no overlapping, facing each other
- Extrabonus · a moving block
Review — the Tower Defence arc 5 min
- Clones, lists, My Blocks and a state variable all worked together in one game.
- Clones hear broadcasts too, so the original must be told apart.
- Changing a list rebalanced the game without touching a script.
Quick-fire
Two people want to play at one keyboard. If both press a key at the same moment, which one does Scratch listen to?
Reveal the answer
Both. <key (w v) pressed?> just asks whether that key is down right now, and any number can be down at once. Give each player different keys and their own loop, and they never get in each other’s way.
Today’s Topic 3 min
- Two sprites, two key sets, two loops running at once
- A
walk (dx) (dy)My Block with move-ask-undo - Fairness: the same numbers for both players
- Keeping the fighters out of each other
Learning outcome
By the end of this lesson you will be able to:
- Let two players control two sprites from one keyboard at the same time.
- Keep both inside an arena with a colour check.
- Spot where two copies of the same code have drifted apart.
Learnt — two at once 28 min
1 · Two loops, two key sets
Tera gets W A S D; Giga gets the arrows. Each sprite runs its own forever loop asking about its own keys only:
forever
if <key (w v) pressed?> then
walk (0) (4) :: custom
end
if <key (s v) pressed?> then
walk (0) (-4) :: custom
end
if <key (a v) pressed?> then
point in direction (-90)
walk (-4) (0) :: custom
end
if <key (d v) pressed?> then
point in direction (90)
walk (4) (0) :: custom
end
end
forever
if <key (up arrow v) pressed?> then
walk (0) (4) :: custom
end
if <key (down arrow v) pressed?> then
walk (0) (-4) :: custom
end
if <key (left arrow v) pressed?> then
point in direction (-90)
walk (-4) (0) :: custom
end
if <key (right arrow v) pressed?> then
point in direction (90)
walk (4) (0) :: custom
end
end
How to use this: Click the green flag. One of you uses W A S D, the other the arrow keys. Move at the same time.
2 · The arena holds them
The walls and the four pillars are one flat brown. walk is Cheese Maze’s move-ask-undo, once for x and once for y, so a fighter can slide along a wall:
define walk (dx) (dy)
change x by (dx)
if <touching color [#8B5A3C]?> then
change x by ((0) - (dx))
end
change y by (dy)
if <touching color [#8B5A3C]?> then
change y by ((0) - (dy))
end
How to use this: Click the green flag. Tera walks up and Giga walks right, each into a wall.
Basic — the face-off 24 min
Start a new project. You are building the arena and two fighters.
Step 1 — the arena
- Paint a floor, a wall round the edge, and a few pillars — the walls and pillars in one brown.
- Leave the middle open, and a clear corner on each side.
Step 2 — Tera
- Add Tera at size 35, start it at x −180, and set rotation style to left-right.
- Make the
walk (dx) (dy)block from Learnt part 2. - Build Tera’s key loop from Learnt part 1.
Step 3 — Giga
- Duplicate Tera. Swap the costumes for Giga’s and start at x 180, facing left.
- Change the four keys to the arrows. Nothing else.
Step 4 — play it
- Find a partner and move both at once.
- Try every wall and every pillar.
How to use this: Click the green flag. Player 1: W A S D. Player 2: arrow keys. Walk round the arena together.
Only one fighter moves at a time.
Both sprites are listening to the same keys, or one loop is a when [w v] key pressed hat instead of a forever loop. Hats wait for a press; loops ask every tick.
A fighter goes straight through a pillar.
The pillar is a slightly different brown. Pick the colour with the eyedropper from the wall.
A fighter gets stuck in a corner.
walk undoes x and y together instead of one at a time. Separate them, as in Learnt part 2.
Fair speed
When Giga was duplicated, someone typed 3 instead of 4. Race the two from the top of the arena to the bottom and count the ticks.
- Both start at y 100 and walk down until they are below y −120.
- Tera walks at 4, Giga at 3.
- Show both counts.
How to use this: Click the green flag and watch the race down the two sides of the arena.
Teacher note
Hold on to this. Two copies of the same code that must stay the same is the problem the whole arc solves: Lesson 3-44 replaces the two copies with one rulebook, and this bug becomes impossible.
No walking through each other
Right now the fighters pass through each other like ghosts. Make each one a wall to the other.
- In Tera’s
walk, undo the move if Tera touches the wall colour or touches Giga. - The same in Giga’s, the other way round.
- Walk them at each other and measure the gap.
How to use this: Click the green flag. The two fighters walk straight at each other.
Teacher reveal — one condition, two questions
if <<touching color [#8B5A3C]?> or <touching (Giga v)?>> then
change x by ((0) - (dx))
end
Giga’s copy needs <touching (Tera v)?> instead — the one line that must not be copied unchanged. Worth asking the class which lines of a duplicated sprite should differ.
Always facing each other
In a duel, you never turn your back. Make each fighter face the other every tick.
point towards (Giga v)at the end of Tera’s loop, and the other way round.- Left-right rotation, so neither tips over.
- Walk one past the other and check they both turn.
How to use this: Click the green flag. Tera walks along the top, past Giga, and keeps turning to face Giga.
Teacher note
point towards gives any angle, like −132, but left-right rotation only ever draws left or right — so the sprite looks correct while its direction is a precise number. Next lesson that precise number decides which way an orb flies.
A moving block
Add a block that slides up and down the middle of the arena, getting in the way.
- Paint it in exactly the wall brown.
- It moves up and down for ever.
- Count how often a fighter is blocked trying to get past.
How to use this: Click the green flag. Tera walks right across the arena, into the block's path.
Teacher note
A block moving into a standing fighter can trap it inside the colour, where every direction reads as wall. Worth letting pupils find that; the fix is to make the block wait if it is touching a fighter.
Summary 5 min
- Two sprites, each with its own loop and its own keys, move at the same time.
walkundoes x and y separately, so walls hold and fighters slide.- A duplicated sprite has to stay the same — except the lines that must differ.
point towardsplus left-right rotation keeps two fighters facing each other.
- Local multiplayer
- Two or more people playing on one computer.
- Key set
- The keys one player uses.
- Symmetry
- Both players getting exactly the same rules.
- Drift
- Two copies of code slowly becoming different.
Hand in
Save your arena. Next lesson the fighters can fire — and be hit.