Workshop › Tetris-style Blocks › Step 6 of 32
A piece is a small matrix of numbers, like a tiny board: 3 is a purple block, 0 nothing. The falling piece is its
shape plus its place in the well, x (column) and y (row). drawShape draws each filled cell of a matrix at that
place.
```js
let piece = { shape: [[0, 3, 0], [3, 3, 3], [0, 0, 0]], x: 3, y: 0 }
function drawShape(shape, x, y) {
shape.forEach((cells, r) => {
cells.forEach((value, c) => {
if (value) drawCell(x + c, y + r, COLORS[value])
})
})
}
drawShape(piece.shape, piece.x, piece.y)
```piece is an object: shape (the matrix), x and y (where its top-left corner is in the well).forEach runs a function for every element and gives it the element and its index: the outer one each row (cells,
index r), the inner one each number in that row (value, index c).[r][c] of the matrix is drawn in the well at (x + c, y + r).draw draws the piece after the board, on top of it.let board write let piece.drawCell write drawShape.draw, after the two loops, write the drawShape(...) call.Skills: Arrays and objects, Drawing on a canvas
← The number is the color A list of shapes →