Workshop › Pong › Step 25 of 26

One player or two

What we are doing

The 2 key switches between playing the computer and playing a friend, and a grey line at the bottom says which mode is on.

```js
if (event.key === '2') twoPlayers = !twoPlayers

ctx.fillStyle = '#888'
ctx.font = '14px sans-serif'
const hint = twoPlayers ? 'Two players · press 2 to play the computer' : 'W/S to move · press 2 for two players'
ctx.fillText(hint, canvas.width / 2, canvas.height - 12)
```

What it means

Your turn

  1. In the keydown listener, write the '2' line under keys[event.key] = true.
  2. At the very end of draw, after an empty line, write the hint lines.

Skills: Keyboard and mouse input

All steps

  1. Get the canvas and the pen
  2. A black court
  3. One dash of the net
  4. The whole net
  5. Two paddles
  6. A function that draws
  7. The game loop
  8. Remember held keys
  9. Move the paddles
  10. Stay on the court
  11. The ball
  12. The ball moves
  13. Bounce off the top and bottom
  14. Does the ball touch a paddle?
  15. The paddles hit back
  16. Serve again
  17. Keep the score
  18. Show the score
  19. First to five
  20. Who won?
  21. Play again
  22. Aim with the paddle
  23. Faster every hit
  24. A computer opponent
  25. One player or two
  26. Build it yourself: a serve pause