Workshop › Tetris-style Blocks › Step 1 of 32

Tetris-style Blocks: The well

Falling pieces, rotation and line clears. Pieces as matrices, rotating a matrix, collision against a grid, locking, clearing rows, levels and a fair random bag.

What we are doing

The pieces fall into a well 10 cells wide and 20 tall, each cell 24 pixels. We paint the page dark and the well a little lighter; the space on the right is for the score later.

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

const COLS = 10
const ROWS = 20
const CELL = 24

ctx.fillStyle = '#0f172a'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = '#1e293b'
ctx.fillRect(0, 0, COLS * CELL, ROWS * CELL)
```

What it means

Your turn

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

Skills: Drawing on a canvas

All steps

  1. The well
  2. The board as rows of numbers
  3. Draw one cell
  4. Draw the whole board
  5. The number is the color
  6. A piece is a small grid
  7. A list of shapes
  8. All seven pieces
  9. Does it fit inside the well?
  10. Blocks are in the way
  11. Try, then move
  12. Move with the arrows
  13. Turn a matrix
  14. Turn with Up
  15. Wall kicks
  16. Falling by itself
  17. A random piece at the top
  18. Land and lock
  19. One function starts a game
  20. When the pile reaches the top
  21. Game Over on screen
  22. Play again
  23. Clear full rows
  24. Points for lines
  25. The side panel
  26. Levels
  27. Faster every level
  28. A fair bag of pieces
  29. The next piece
  30. Drop it
  31. The ghost piece
  32. Build it yourself: hold a piece