- Learnt28 min · copies drift; one sprite from lists
- Basic24 min · the rulebook duel
- Challenge 1–327 min · a handicap, a computer player, swapping sides
- Extrabonus · ship it
Review — rounds 5 min
- A referee waits for a knock-out and scores the round.
- A reset covers health, positions and orbs still in the air.
- Every script asks the state, so new states cost nothing.
Quick-fire
In this arc, how many times have you changed something in Tera and then had to make the same change in Giga?
Reveal the answer
Every lesson. The speed, the other fighter’s name, the cooldown, the reset. Each change had to be made twice, and forgetting once made the game unfair. Today the two copies become one sprite, so there is only one place to change.
Today’s Topic 3 min
- Why copied code drifts
- One Fighter sprite, a clone per player, each with its own
playernumber - Keys, looks and starts read from lists:
<key (item (player) of [fire key v]) pressed?> - A shot queue, so one Orb sprite serves both players
Learning outcome
By the end of this lesson you will be able to:
- Replace two copies of a sprite with one sprite driven by data.
- Let a clone read its own settings from lists by its number.
- Change who plays, how, and how fast, without touching a script.
Learnt — one rulebook 28 min
1 · Copies drift
Here is the duel after one ordinary afternoon of fixing. The cooldown was mended in Tera — and forgotten in Giga, whose copy still says 0.1 seconds.
How to use this: Click the green flag. Both fighters hold fire for three seconds; watch the bars.
Nobody would spot that by reading one sprite. The code is not wrong; there is just two of it.
2 · One sprite, read from lists
Delete Giga. Rename Tera Fighter, give it both looks as costumes, and put everything that differs between the players into lists — one line per player:
| List | Line 1 (player 1) | Line 2 (player 2) |
|---|---|---|
up key … fire key | w, s, a, d, space | arrows, l |
look | tera a | giga a |
start x, start y, facing | −180, −10, 90 | 180, −10, −90 |
speed, health | 4, 100 | 4, 100 |
when flag clicked
hide
set [player v] to (0)
repeat (2)
change [player v] by (1)
create clone of (myself v)
end
when I start as a clone
switch costume to (item (player) of [look v])
go to x: (item (player) of [start x v]) y: (item (player) of [start y v])
point in direction (item (player) of [facing v])
show
forever
if <key (item (player) of [up key v]) pressed?> then
walk (0) (item (player) of [speed v]) :: custom
end
if <key (item (player) of [fire key v]) pressed?> then
fire :: custom
end
end
player is for this sprite only, so each clone reads its own line.One Orb sprite serves both players through a queue: fire adds a line to four lists — shot x, shot y, shot dir, shot by — and the Orb takes line 1, deletes it, and clones. Each orb knows who fired it, and only hurts the other colour.
How to use this: Click the green flag. Both clones fire as fast as the shared cooldown allows.
Basic — the rulebook duel 24 min
Turn your two fighters into one. Save a copy of your old project first.
Step 1 — the lists
- Make the lists in the table and type in both lines of each.
- Make a
healthlist with 100 and 100 — it replaceshealth 1andhealth 2.
Step 2 — one Fighter
- Rename Tera to Fighter and add Giga’s costume to it. Delete the Giga sprite.
- Give Fighter
playerandnext shot, for this sprite only. - Build the two scripts from Learnt part 2, with all five keys.
Step 3 — one Orb
- Change
fireto add a line to the four shot lists instead of cloning. - One Orb sprite takes line 1 of the shot lists, deletes it, and clones — for ever.
- Each bar reads its own line of
health.
How to use this: Click the green flag. Player 1: W A S D and space. Player 2: arrows and L.
Both clones walk on the same keys.
player is for all sprites, so both clones read the last value, 2. Make it for this sprite only.
The key blocks will not accept the list item.
Drag (item (player) of [up key v]) right onto the key dropdown — the menu takes a reporter, even though it does not look as if it would.
Only one orb ever appears, however fast I press.
The Orb takes line 1 but never deletes it, so it keeps making the same shot. Delete line 1 of all four lists.
An orb hurts the player who fired it.
It is starting on top of its fighter. Start it 30 in front, in the direction the fighter faces.
A handicap
Playing against a younger brother or sister? Give each player their own speed — by editing one line of a list.
- Set
speedto 4 and 2. - Race both down the arena, as in Lesson 3-41.
How to use this: Click the green flag. Both clones race down the arena at the speeds in the list.
Teacher note
Compare with Lesson 3-41’s Challenge 1, where an unequal speed was an accident hiding in a copy. Here it is a deliberate, visible choice in a table. Same number, completely different status.
A computer player
No partner? Let player 2’s clone decide for itself — using the same walk and fire as a person.
- If Tera is higher, walk up; if lower, walk down.
- If level with Tera, fire.
- Player 1 keeps the keys.
How to use this: Click the green flag. Tera wanders up and down; the computer follows and fires.
Teacher reveal — a brain is just a choice of blocks
if <(tera y) > ((y position) + (4))> then
walk (0) (3) :: custom
end
if <(tera y) < ((y position) - (4))> then
walk (0) (-3) :: custom
end
if <([abs v] of ((tera y) - (y position))) < (15)> then
fire :: custom
end
The computer player uses exactly the same moves as a person, so it cannot cheat. That is the rulebook’s other gift: whoever — or whatever — is deciding, the rules are the same.
Swap sides
Players argue about who gets the left. Let them swap by editing the lists.
- Swap the two numbers in
start xand infacing. - Run it. Change nothing else.
How to use this: Click the green flag and see where each fighter starts.
Teacher note
A good follow-up: swap the look list too. Now player 1 is Giga — on the keys that used to be Tera’s. What a player is has become entirely a row in a table.
Ship it
Rebuild the match from Lesson 3-43 on top of the rulebook, and hand it to two friends.
- A title that tells both players their keys.
- A referee, rounds, a banner and a match to two.
- Healths from the
healthlist; a round resets both lines to 100.
How to use this: Click the green flag, read the title, then fight: W A S D + space against the arrows + L. First to two rounds.
Teacher note
This is the idea of the whole arc, and one of the most important in programming: when two things must behave the same, write the behaviour once and give each thing its own data. Every bug this arc found — unequal speeds, the missing wall check, the forgotten cooldown — came from having two copies.
Summary 5 min
- Two copies of the same code drift apart, one fix at a time.
- One sprite with a clone per player, and a
playernumber for this sprite only. - Everything that differs between players lives in lists, one line each.
- A key dropdown takes a reporter:
<key (item (player) of [fire key v]) pressed?>. - A computer player is just a different brain calling the same rulebook.
- Rulebook
- The one set of scripts every player obeys.
- Data-driven
- Behaviour chosen by lists and variables, not by separate code.
- Queue
- A list where things are added at the end and taken from the front.
- Don’t repeat yourself
- Write a behaviour once, and give each user of it their own settings.
Hand in
Save your game. You have finished the Arena Duel arc — two players, one keyboard, and one rulebook that makes it fair.