Workshop › Candy Crush-style Match 3 › Step 1 of 29

Candy Crush-style Match 3: A place for the board

Swap neighbouring gems to line up three of a color. A starting board with no matches, finding runs with a Set, gravity with two pointers, cascades that score more and more, a state machine for flashing and falling, a hint found by trying every move, and a move budget.

What we are doing

The game is an 8 × 8 grid of gems, each cell 48 pixels. We work out where the grid goes on the 400 × 480 canvas, centred left to right with room above for the score, and paint the background.

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

const N = 8 // 8 by 8 gems
const SIZE = 48
const LEFT = (canvas.width - N * SIZE) / 2
const TOP = 72 // room for the score and the moves left

ctx.fillStyle = '#1e1b4b'
ctx.fillRect(0, 0, canvas.width, canvas.height)
```

What it means

Your turn

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

Skills: Drawing on a canvas

All steps

  1. A place for the board
  2. A checkered grid
  3. The game loop
  4. A board of random gems
  5. Draw the gems
  6. Start without matches
  7. Click a gem
  8. Show the selection
  9. Swap two neighbours
  10. Find every match
  11. No match, no swap
  12. A score and a fresh start
  13. Clear the match
  14. Let the gems fall
  15. Two halves of a clear
  16. Wait before clearing
  17. Flash, and hands off
  18. Cascades
  19. Where a gem came from
  20. Slide down
  21. Out from under the top
  22. Is there any move?
  23. Never stuck
  24. A hint after five seconds
  25. Show the hint
  26. Twenty moves
  27. The end of a game
  28. Remember the best score
  29. Build it yourself: swipe to swap