- 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 checks the state, so new states are easy to add.
Quick-fire
In these lessons, how many times did you change something in Tera? How often did you then 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, which means the copies slowly become different.
- One Fighter sprite, with one clone for each player. Each clone has 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 works for both players.
Learning outcome
By the end of this lesson you will be able to:
- Replace two copies of a sprite with one sprite that reads its data from lists.
- Let a clone use its number to read its own settings from lists.
- 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 afternoon of fixing. The cooldown was fixed in Tera. It was forgotten in Giga, and Giga’s 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 are just two copies of it.
2 · One sprite, read from lists
Delete Giga. Rename Tera Fighter and give it both looks as costumes. Put everything that is different for each player into lists. Use 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 works for both players. It uses a queue, a list that works like a waiting line. fire adds a line to four lists: shot x, shot y, shot dir and shot by. The Orb takes line 1, deletes it, and makes a clone. Each orb knows who fired it. It 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. - The one Orb sprite takes line 1 of the shot lists, deletes it, and clones. It does this 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 like it.
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. Just edit one line of a list. A handicap like this makes the game fairer for a weaker player.
- 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. It uses the same walk and fire as a person.
- If Tera is higher, walk up. If Tera is 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. Then give 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 new round resets both lines to 100.
How to use this: Click the green flag and read the title. Then fight: W A S D + space against the arrows + L. First to two rounds wins.
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 slowly become different, one fix at a time.
- One sprite with a clone for each player, and a
playernumber for this sprite only. - Everything that is different for each player goes in lists, one line each.
- A key dropdown takes a reporter:
<key (item (player) of [fire key v]) pressed?>. - A computer player makes its own choices, but uses 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 four Arena Duel lessons. You made two players, one keyboard, and one rulebook that makes it fair.