Coding a Two-Player Game

Learn how to use code blocks, variables, and conditions to build a game where two players take turns and a winner is decided.

Reading is good — doing is better. Practice Coding a Two-Player Game as an interactive lesson.

Try the lesson

Definition

Coding a two-player game means writing a set of instructions (code) that lets two people play against each other on a computer or tablet. The code keeps track of each player's score or moves, checks whose turn it is, and figures out who wins when the game is over.

Remember the rule

Every two-player game needs three things: TRACK (keep score/turns), CHECK (test win conditions), and SWITCH (change whose turn it is).

Key words

Variable
A storage box in your code that holds a value, like a player's score, that can change as the game goes on.
Condition (If/Else)
A rule in your code that checks whether something is true or false, then does different things depending on the answer.
Loop
A block of code that repeats over and over until a certain thing happens, like the game ending.
Event
Something that triggers your code to run, like pressing a key or clicking the mouse.
Turn
One player's chance to make a move before the other player gets to go.
Sprite
A character or object you see on the screen that your code controls.
Boolean
A value that is only ever TRUE or FALSE, often used to track whether the game is still going.
Broadcast
A message sent inside your program that tells other parts of the code to start doing something.

Worked examples

Player 1 scores a point. How does the code remember the new score?

You have a variable called score1. When Player 1 scores, the code runs: set score1 to score1 + 1. If score1 was 3, it becomes 4. · Variables let the game remember numbers even as they keep changing.

How does the game know when to switch turns from Player 1 to Player 2?

Use a variable called turn. Start it at 1. After Player 1 moves, the code checks: if turn = 1 then set turn to 2, else set turn to 1. Now it is Player 2's turn. · This if/else flip-flop is the core of any turn-based game.

How does the game check if someone has won in a first-to-5-points game?

After every move, add an if block: if score1 = 5 then say 'Player 1 wins!' and stop the game. Do the same check for score2. · Putting the win check right after each move means the game stops the moment someone reaches 5.

Player 1 uses the W key to move up and Player 2 uses the Up Arrow. How do you code both at the same time?

In Scratch, give each sprite its own 'when flag clicked' loop. Inside Player 1's loop: if key W pressed then change y by 10. Inside Player 2's loop: if key Up Arrow pressed then change y by 10. · Separate loops for each player let both respond to keypresses without interfering with each other.

How do you show each player's score on the screen?

Create two variables, score1 and score2, and check the 'show variable' box for each one in Scratch. The score appears on screen automatically and updates every time the variable changes.

The game needs to reset when players click a restart button. What code does that take?

When the restart button is clicked, broadcast a 'reset' message. All sprites listen for that message and then set score1 to 0, set score2 to 0, set turn to 1, and move each sprite back to its starting position. · Using a broadcast keeps reset code organized instead of copying it everywhere.

Common mistakes

  • Forgetting to switch turns — if you never change the turn variable, the same player goes every time.
  • Using the same key for both players — if both players press Space to move, the game cannot tell them apart. Give each player unique keys.
  • Checking the win condition only at the end of the game instead of after every single move, so the game keeps going after someone already hit the winning score.
  • Not setting variables back to zero on restart, so scores carry over from the last game into the new one.
  • Putting both players' movement code inside one loop — this can cause one player's code to block the other player from moving.

FAQs

Does each player need their own sprite?

Usually yes. Giving each player their own sprite makes it easy to control them separately, track their position, and apply different rules to each one.

Can two players press keys at the exact same time in Scratch?

Scratch can detect multiple keys pressed at once, so yes. That is why you use separate forever loops for each player — both loops check their own keys every frame without waiting for each other.

What is the easiest way to start coding a two-player game for the first time?

Start with a simple turn-based game like tic-tac-toe or a coin-toss guessing game. These only need a turn variable, a few if/else blocks, and a win check, so you learn the big ideas without too many moving parts.

How do I make the game stop after someone wins?

Use a 'stop all' block (in Scratch) inside the if block that detects the win. You can also set a boolean variable called gameOver to true and have your loops check that variable before doing anything.

What if both players reach the winning score at the same time?

Check Player 1's score first. If that check triggers, the game stops before it ever checks Player 2, so Player 1 is declared the winner. You can add a tie condition by checking if both scores are equal before declaring a winner.

Want the whole picture for your child?

Every K–6 subject, an AI tutor that teaches step by step, unlimited practice, and a reward world.

Start a 3-day free trial

Related concepts (5th Grade Technology)