Workshop › Pool › Step 1 of 33

Pool: The room

Clear the table in as few shots as you can. Velocity from an angle, friction and cushions, elastic collisions between equal balls (and why they leave at 90 degrees), pockets, a slingshot aim, smaller physics steps for fast shots, and a ghost ball that predicts the hit.

What we are doing

We start with the usual frame: find the canvas, take its 2D context, and a loop that paints a dark background about 60 times a second. The table goes on top of it next.

```js
const canvas = document.getElementById('game')
const ctx = canvas.getContext('2d')

function draw() {
  ctx.fillStyle = '#0f172a'
  ctx.fillRect(0, 0, canvas.width, canvas.height)
}

function loop() {
  draw()
  requestAnimationFrame(loop)
}

requestAnimationFrame(loop)
```

What it means

Your turn

Write the code under the three comment lines, then press Run.

Skills: Drawing on a canvas, The game loop

All steps

  1. The room
  2. The table
  3. The cue ball
  4. Draw the balls
  5. The rack
  6. Numbers on the balls
  7. An aim line
  8. Turn the aim
  9. A shot is a velocity
  10. Roll
  11. Friction
  12. Cushions
  13. Aiming and rolling
  14. Hide the aim while rolling
  15. Count the shots
  16. Balls that touch
  17. Swap the push
  18. Six pockets
  19. In the pocket
  20. Scratch
  21. Balls left
  22. Clear the table
  23. Choose the power
  24. A power bar
  25. A slingshot
  26. Press to aim
  27. Pull back and let go
  28. Smaller steps
  29. Predict the hit
  30. Draw the ghost ball
  31. Where the hit ball goes
  32. Your best
  33. Build it yourself: two players