2 Player Controller โ€” Bear vs. Opponent
Grades 3โ€“5

Learning Targets

  • Create two sprites (Bear and Opponent) with separate controls
  • Player 1 uses WASD; Player 2 uses Arrow Keys
  • Add health variables and hit detection to make a battle game

Materials

  • Scratch

How a 2-Player Controller Works

  • Separate Keys: Each player listens for different keys.
  • Movement: Change x for left/right, y for jump.
  • Battle: Use variables for health and sense collisions for hits.

Step 0: Setup

  1. Open Scratch and create a new project
  2. Add two sprites: Bear and Opponent (any character)
  3. Add a simple stage background

Step 1: Player 1 (Bear) Movement โ€” WASD

when green flag clicked
forever
  if <key [a v] pressed?> then
    change x by (-6)
  end
  if <key [d v] pressed?> then
    change x by (6)
  end
  if <key [w v] pressed?> then
    change y by (8)
    repeat (6)
      change y by (-3)
    end
  end
end

Step 2: Player 2 (Opponent) Movement โ€” Arrow Keys

when green flag clicked
forever
  if <key [left arrow v] pressed?> then
    change x by (-6)
  end
  if <key [right arrow v] pressed?> then
    change x by (6)
  end
  if <key [up arrow v] pressed?> then
    change y by (8)
    repeat (6)
      change y by (-3)
    end
  end
end

Step 3: Health Variables

  1. Make two variables: Bear HP and Opponent HP
  2. Set both to 100 at the start
when green flag clicked
set [Bear HP v] to (100)
set [Opponent HP v] to (100)

Step 4: Simple Attack + Hit Detection

When Player 1 presses f, check if the bear is touching the opponent. If yes, reduce Opponent HP.

// Bear sprite
when [f v] key pressed
if <touching [Opponent v]?> then
  change [Opponent HP v] by (-10)
end

When Player 2 presses l, check if the opponent is touching the bear. If yes, reduce Bear HP.

// Opponent sprite
when [l v] key pressed
if <touching [Bear v]?> then
  change [Bear HP v] by (-10)
end

Step 5: Win Condition

forever
  if <(Bear HP) <= (0)> then
    say [Opponent Wins!] for (2) seconds
    stop [all v]
  end
  if <(Opponent HP) <= (0)> then
    say [Bear Wins!] for (2) seconds
    stop [all v]
  end
end

Step 6: Visual Health Bars (Top of Screen)

  1. Create a new sprite named Bear HP Bar with a green rectangle costume.
  2. Place it at the top-left. Duplicate it for Opponent HP Bar (top-right).

Bear HP Bar sprite code:

when green flag clicked
go to x: (-200) y: (160)
set size to (100) %
show
forever
  set size to (Bear HP) %
end

Opponent HP Bar sprite code:

when green flag clicked
go to x: (200) y: (160)
set size to (100) %
show
forever
  set size to (Opponent HP) %
end

Tip: Keep each barโ€™s original costume the same height and set size by percent so it shrinks as HP drops.

Step 7: Add Punch and Kick

Bear (Player 1): Punch with F, Kick with G

// Bear sprite
when [f v] key pressed // Punch
if <touching [Opponent v]?> then
  change [Opponent HP v] by (-10)
end
wait (0.2) seconds

when [g v] key pressed // Kick
if <touching [Opponent v]?> then
  change [Opponent HP v] by (-15)
end
wait (0.3) seconds

Opponent (Player 2): Punch with K, Kick with L

// Opponent sprite
when [k v] key pressed // Punch
if <touching [Bear v]?> then
  change [Bear HP v] by (-10)
end
wait (0.2) seconds

when [l v] key pressed // Kick
if <touching [Bear v]?> then
  change [Bear HP v] by (-15)
end
wait (0.3) seconds

Optional: Add a short animation or โ€œsay [Punch!] for (0.2) secsโ€ when the attack happens.

Extra Challenges

  • Add knockback: briefly move the hit sprite backward
  • Add animations when attacking or getting hit
  • Add a block for โ€œcooldownโ€ so you canโ€™t spam attacks
  • Add platforms and gravity for a small arena

Review Questions

  1. How did we keep controls separate for each player?
  2. How do we check for a โ€œhitโ€ between sprites?
  3. Why do we use variables for health?