Search⌘ K

Freezing

Explore how to freeze tetrominoes in their final positions within a JavaScript Tetris game. Learn to merge pieces into the game board, draw landed shapes, and enhance collision detection for better gameplay.

When the piece can’t move down anymore, we want to freeze the piece in its final position before we spawn a new tetromino.

Drop or freeze

Let’s start by defining a freeze() method to merge the tetromino blocks to the board:

C++
freeze() {
this.piece.shape.forEach((row, y) => {
row.forEach((value, x) => {
if (value > 0) {
this.grid[y + this.piece.y][x + this.piece.x] = value;
}
});
});
}

When the piece has no more valid moves downwards, it lands on its final resting place and moves over to the ...