3 − current-player swap alternates the prompt between the two players after every answer.Learning Goals 3 min
By the end of this lesson you will be able to:
- Use a current-player variable to track whose turn it is in a two-player game, and update an on-screen prompt to match.
- Keep two parallel score variables (score-p1 and score-p2) and add a point to the correct one based on the current player.
- Swap players in one line using the trick set [current-player v] to ((3) - (current-player)) — the maths shortcut that flips 1 to 2 and 2 to 1 without an if-then.
Warm-Up — the chess clock problem 7 min
Imagine you and your cousin are playing a paper quiz. You answer a question, then your cousin answers a question, then you again, then your cousin again. Whose turn is it after question 7? Question 14? Question 33?
You don't really remember — you just look around the table: "Oh, the pen is on her side now, so it's my turn." The pen is a state variable. It only ever holds one of two values: my side or her side.
Predict puzzle. Here's a tiny script that runs three times in a row. What does it say?
when flag clicked
set [current-player v] to (1)
say (current-player) for (1) seconds
set [current-player v] to ((3) - (current-player))
say (current-player) for (1) seconds
set [current-player v] to ((3) - (current-player))
say (current-player) for (1) seconds
Reveal the answer
It says 1, then 2, then 1. The first say prints the starting value. Then 3 − 1 = 2, so it prints 2. Then 3 − 2 = 1, so it prints 1. The expression () - () with the literal number 3 on the left always flips between 1 and 2. No if-then needed — just one subtraction. Today's whole lesson is built around this one little trick.
The pen on the table is a variable. The "3 minus" trick is the script that moves the pen. Together they let one Scratch project run a fair two-player game on one keyboard, with no extra hardware.
New Concept — alternating turns with one variable 15 min
A two-player game needs three things the computer can keep track of: whose turn is it, Player 1's score, and Player 2's score. That's three variables — current-player, score-p1, score-p2 — and almost every two-player Scratch game in the world uses some version of them.
The current-player variable
current-player only ever holds two values: 1 or 2. It starts as 1 (Player 1 always goes first by convention). At the end of every turn, it flips to the other value.
set [current-player v] to (1)
Talking to whichever player's turn it is
Because current-player is just a number, you can drop it straight into a join block to build a sentence:
ask (join (join [Player ] (current-player)) [: your turn!]) and wait
Player 2: your turn! and waits for them to type. Same script handles both players because the variable changes the wording for you.The double join is the standard Scratch pattern for "stitch three pieces together": [Player ] + the number + : your turn!. Drop a join inside a join.
Scoring the right player
When a player gets a question right, you have to add the point to their score variable — not the other one. That means an if-then on current-player:
if <(current-player) = (1)> then
change [score-p1 v] by (1)
else
change [score-p2 v] by (1)
end
The 3-minus trick — swapping in one line
At the end of every turn, you need to flip current-player from 1 to 2, or from 2 to 1. The slow way is an if-then:
if <(current-player) = (1)> then
set [current-player v] to (2)
else
set [current-player v] to (1)
end
The fast way uses a maths fact: 3 − 1 = 2 and 3 − 2 = 1. So if you subtract current-player from 3, you always get the other player's number:
set [current-player v] to ((3) - (current-player))
Announcing the winner
When the game ends (say, after each player has answered five questions), compare the two scores and say who won:
if <(score-p1) > (score-p2)> then
say [Player 1 wins!] for (3) seconds
else
if <(score-p2) > (score-p1)> then
say [Player 2 wins!] for (3) seconds
else
say [Tie!] for (3) seconds
end
end
Worked Example — a two-player Malaysia quiz round 12 min
Open Scratch. We'll build a tiny game: each player gets three turns to answer a question about Malaysia. (We'll use a single fixed question today — the focus is turn-swapping, not the question bank.) Whoever gets more right wins.
Step 1 — Make three variables
Variables palette → Make a Variable. Create current-player, score-p1, and score-p2. Tick the boxes so all three appear on the Stage.
Step 2 — Add the hat and reset
From Events: when ⚑ clicked. Then three set [v] to (0) blocks: current-player to 1, score-p1 to 0, score-p2 to 0.
Step 3 — Loop six turns
From Control: repeat (6). Six iterations = three turns each.
Step 4 — Ask the current player
Inside the repeat, drop a ask () and wait. For the question, use the join-inside-join pattern from the concept: join (join [Player ] (current-player)) [, what is Malaysia's national flower?].
Step 5 — Check the answer
From Control: an if <> else. The diamond holds (answer) = (Hibiscus).
Step 6 — Score the right player
Inside the if (the "yes — correct" branch), nest another if <> else. The diamond: (current-player) = (1). Inside: change [score-p1 v] by (1). Else: change [score-p2 v] by (1). Add a say [Correct!] for (1) seconds too.
Step 7 — Wrong answer feedback
In the outer else (the "no — wrong" branch), add say [Nope!] for (1) seconds. No score change. Players move on either way.
Step 8 — The swap line
Still inside the repeat, after the if-else block, drop the magic block: set [current-player v] to ((3) - (current-player)). This is the line that makes the whole game alternate. Without it, Player 1 answers all six questions and Player 2 never gets a turn.
Step 9 — Announce the winner
After the repeat finishes, add the three-way winner check from the concept section: P1 score > P2 score, P2 score > P1 score, or tie.
Step 10 — Test the whole thing
Click the flag. The cat asks Player 1, what is Malaysia's national flower?. Type your answer. Then it asks Player 2. Then Player 1 again. Six questions total. At the end, the cat announces the winner. Try it with a friend.
The full assembled stack
when flag clicked
set [current-player v] to (1)
set [score-p1 v] to (0)
set [score-p2 v] to (0)
repeat (6)
ask (join (join [Player ] (current-player)) [, what is Malaysia's national flower?]) and wait
if <(answer) = (Hibiscus)> then
say [Correct!] for (1) seconds
if <(current-player) = (1)> then
change [score-p1 v] by (1)
else
change [score-p2 v] by (1)
end
else
say [Nope!] for (1) seconds
end
set [current-player v] to ((3) - (current-player))
end
if <(score-p1) > (score-p2)> then
say [Player 1 wins!] for (3) seconds
else
if <(score-p2) > (score-p1)> then
say [Player 2 wins!] for (3) seconds
else
say [Tie!] for (3) seconds
end
end
What you just built: the smallest possible two-player quiz. Swap out the fixed Malaysia question for a list of trivia and you have the foundation for SCR-L04-41 — the Build lesson next door.
Try It Yourself — three turn-swap drills 15 min
Goal: Print the first ten players in turn order with no questions, no scores — just which player is next. Use a repeat (10) with the 3-minus swap.
when flag clicked
set [current-player v] to (1)
repeat (10)
say (join [Next: Player ] (current-player)) for (1) seconds
set [current-player v] to ((3) - (current-player))
end
Think: No scoring, no asking, no logic beyond the swap. This is the bare skeleton of every two-player game. Once you trust the alternation, you can pile real gameplay on top of it.
Goal: Build a two-player "guess my number" game. The cat picks a random number 1–10 at the start. Each player gets two guesses. Whoever guesses correctly first wins. If nobody guesses after four total turns, the cat reveals the answer and says Tie!.
when flag clicked
set [secret v] to (pick random (1) to (10))
set [current-player v] to (1)
set [winner v] to (0)
repeat (4)
if <(winner) = (0)> then
ask (join (join [Player ] (current-player)) [: guess 1-10]) and wait
if <(answer) = (secret)> then
set [winner v] to (current-player)
end
set [current-player v] to ((3) - (current-player))
end
end
if <(winner) > (0)> then
say (join (join [Player ] (winner)) [ wins!]) for (3) seconds
else
say (join [Tie! It was ] (secret)) for (3) seconds
end
Think: A real game has to handle early exits — what happens when someone wins before the loop runs out. The winner flag is the simplest pattern for that.
Goal: Three-player version. Now the swap formula isn't 3 - current-player. Work out the pattern: from Player 1 → 2 → 3 → 1 → 2 → 3. Hint: use the mod operator (also written (a) mod (b) in Scratch). The formula ((current-player) mod (3)) + (1) cycles 1 → 2 → 3 → 1.
when flag clicked
set [current-player v] to (1)
repeat (9)
say (join [Now: Player ] (current-player)) for (1) seconds
set [current-player v] to (((current-player) mod (3)) + (1))
end
(current-player) mod (N) + 1.Think: Two-player is special — it has a tidy 3 − x shortcut. Three or more players need the mod-plus-one pattern. Once you know mod, you can run any number of players with a single line.
Mini-Challenge — the game that won't switch 5 min
"Aisha's stuck game"
Aisha built a two-player adding game. Player 1 answers, gets the point, hears "Correct!", and then... Player 1 is asked again. And again. Player 2 never gets a turn. Here's her script:
when flag clicked
set [current-player v] to (1)
set [score-p1 v] to (0)
set [score-p2 v] to (0)
repeat (6)
ask (join (join [Player ] (current-player)) [, what is 5 + 6?]) and wait
if <(answer) = (11)> then
say [Correct!] for (1) seconds
if <(current-player) = (1)> then
change [score-p1 v] by (1)
set [current-player v] to ((3) - (current-player))
else
change [score-p2 v] by (1)
set [current-player v] to ((3) - (current-player))
end
else
say [Nope!] for (1) seconds
end
end
Walk through it. What happens on the very first turn if Player 1 types something wrong?
Reveal one valid solution
Aisha put the swap line inside the "Correct!" branch — so the swap only happens when someone answers correctly. If Player 1 answers wrong, the swap is skipped, and Player 1 is asked again next turn. If they keep typing wrong, the game stays on Player 1 forever.
The fix is to move the swap outside the if-else for correctness, so it always runs no matter the answer:
when flag clicked
set [current-player v] to (1)
set [score-p1 v] to (0)
set [score-p2 v] to (0)
repeat (6)
ask (join (join [Player ] (current-player)) [, what is 5 + 6?]) and wait
if <(answer) = (11)> then
say [Correct!] for (1) seconds
if <(current-player) = (1)> then
change [score-p1 v] by (1)
else
change [score-p2 v] by (1)
end
else
say [Nope!] for (1) seconds
end
set [current-player v] to ((3) - (current-player))
end
Same blocks, different fence. The swap belongs to "end of turn", not "got it right". Scoring is conditional; turn-swapping isn't. Knowing which actions belong outside conditionals is the whole skill of multiplayer logic.
Recap 3 min
You built your first two-player game on one keyboard. The current-player variable tracks whose turn it is, the score-p1 and score-p2 variables track each player's score, and the magic one-line swap set [current-player v] to ((3) - (current-player)) flips between players without an if-then. The prompts that go to the current player use join to insert the player number into the question. At the end, an if-else-if-else compares the two scores and announces the winner — or a tie. This is the pattern behind every pass-and-play game in the world.
- Pass-and-play
- A multiplayer mode where two (or more) players share one keyboard and one screen, taking turns. The opposite of online multiplayer — no internet needed, just a friend next to you.
- current-player
- The variable that holds
1or2, depending on whose turn it is. Reset to 1 at the start of every game; flipped at the end of every turn. - 3-minus swap
- The expression ((3) - (current-player)). When current-player is 1, it gives 2; when it's 2, it gives 1. The fastest way to alternate between two players.
- join-inside-join
- Stitching three text pieces together by nesting two join blocks. Standard Scratch pattern for sentences that have a variable in the middle, like
Player 2: your turn!. - Winner flag
- A variable (like winner) that starts at 0 and stores the winning player's number once they've won. Lets your game exit early instead of running every remaining turn.
Homework 2 min
The Cousin Test. Take your example stack from class and tune it for someone else to play with you.
- Pick a real category of question — favourite Malaysian food, favourite festival, favourite state — and write five questions in a notebook before opening Scratch.
- Replace the single fixed question with five different ones: use an if <(current-turn) = (1)> then ask [first question] chain, or store the questions in a list (jumping ahead to L04-37 — that's fine).
- Have one round (P1 + P2 = 2 turns) per question. Use a repeat (5) for the five rounds, with an inner repeat (2) for the two players.
- Save as
HW-L4-40-Two-Player-Quiz.sb3. - Actually play it with a sibling, cousin, or classmate. Watch their face when the prompt says
Player 2: your turn!and notice how natural it feels — they don't need an explanation.
Bring back next class:
- The
.sb3file. - Your five questions, written out in your notebook.
- Your answer to: "Who won — and was it close? If your scores were tied, what could you add to break ties? (Sudden-death round? Tiebreaker question? Coin flip?)"
Heads up for next class: SCR-L04-41 — Malaysia Trivia Quiz is the cluster G capstone Build. You'll fold today's two-player swap into a full ten-question Malaysia-themed quiz, with the no-repeat shuffle from L04-38 and the high-score persistence from L04-39. Three lessons' worth of patterns, one finished game. Bring your homework — we'll reuse questions where they fit.