Workshop › Puzzle Bobble-style Bubble Shooter › Step 5 of 31

Bubbles that nest

What we are doing

Real bubbles pack like marbles in a box: every other row is shifted half a bubble to the right, so each bubble nests in the gap between the two above it. This is a hex grid, like a honeycomb. Rows get closer, and odd rows have one bubble less.

```js
const ROW_H = R * Math.sqrt(3) // rows overlap so the bubbles nest

const cols = (r) => (r % 2 === 0 ? COLS : COLS - 1)
const cellPos = (r, c) => ({ x: R + c * 2 * R + (r % 2) * R, y: TOP + R + r * ROW_H })

    for (let c = 0; c < cols(r); c++) grid[r].push(r < 5 ? Math.floor(Math.random() * COLORS.length) : -1)

    for (let c = 0; c < cols(r); c++) {
```

What it means

Your turn

  1. Under ROWS write ROW_H.
  2. Above cellPos write cols, and change cellPos as shown.
  3. In reset and in draw, replace c < COLS with c < cols(r).

Skills: Arrays and objects, Drawing on a canvas

All steps

  1. The night sky
  2. One bubble
  3. A grid of cells
  4. Draw the grid
  5. Bubbles that nest
  6. The shooter
  7. Aim upwards
  8. Turn with the arrows
  9. Aim with the pointer
  10. Fire!
  11. Fly
  12. Bounce off the walls
  13. Touching something?
  14. Stick into the grid
  15. Smaller steps
  16. Six neighbours
  17. Flood fill
  18. Three in a row pop
  19. Bubbles that fall
  20. Cut them loose
  21. Show the score
  22. Only useful colors
  23. Count the shots
  24. The ceiling comes down
  25. Win or lose
  26. The danger line
  27. The end message
  28. Play again
  29. Best score
  30. A dotted guide
  31. Build it yourself: swap bubbles