DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Tetris Game with JavaScript

Tetris is the falling-block puzzle everyone knows: seven shapes drop, you rotate and place them, and full rows disappear. It is a bigger beginner project than it looks, but it teaches the most useful game skill of all — representing a game world as a grid and updating it with pure functions.

The board is a 2D array of 10 columns and 20 rows. Each tetromino is a small matrix of 1s and 0s, plus a colour. The seven pieces are:

const SHAPES = {
  I: [[1, 1, 1, 1]],
  O: [[1, 1], [1, 1]],
  T: [[0, 1, 0], [1, 1, 1]],
  S: [[0, 1, 1], [1, 1, 0]],
  Z: [[1, 1, 0], [0, 1, 1]],
  J: [[1, 0, 0], [1, 1, 1]],
  L: [[0, 0, 1], [1, 1, 1]],
};

const board = Array.from({ length: 20 }, () => Array(10).fill(0));
Enter fullscreen mode Exit fullscreen mode

Collision detection is one function that answers a single question: does this piece fit at this position? Every move, rotation, and drop goes through it, which keeps the logic in one place:

function collides(piece, offsetX, offsetY) {
  return piece.some((row, y) =>
    row.some((cell, x) => {
      if (!cell) return false;
      const bx = offsetX + x, by = offsetY + y;
      return (
        bx < 0 || bx >= 10 || by >= 20 || (by >= 0 && board[by][bx])
      );
    })
  );
}
Enter fullscreen mode Exit fullscreen mode

Rotation is a matrix transpose followed by reversing each row — three lines that work for every piece:

function rotate(piece) {
  return piece[0].map((_, i) => piece.map((row) => row[i]).reverse());
}
Enter fullscreen mode Exit fullscreen mode

When a piece lands, merge it into the board, check for full rows, and spawn the next piece. Clearing rows is a filter — keep the rows that are not full, then add empty rows at the top to keep the board at 20 rows:

function lockPiece() {
  current.shape.forEach((row, y) => {
    row.forEach((cell, x) => {
      if (cell) board[current.y + y][current.x + x] = current.color;
    });
  });
  let cleared = 0;
  for (let y = board.length - 1; y >= 0; y--) {
    if (board[y].every((cell) => cell)) {
      board.splice(y, 1);
      board.unshift(Array(10).fill(0));
      cleared++;
      y++;
    }
  }
  score += [0, 100, 300, 500, 800][cleared] || 0;
  spawnPiece();
}
Enter fullscreen mode Exit fullscreen mode

The drop loop is a setInterval whose speed increases as you clear lines. Keyboard controls are a single keydown listener: left/right to move, up to rotate, down to soft-drop, space to hard-drop:

document.addEventListener("keydown", (e) => {
  if (e.key === "ArrowLeft" && !collides(current.shape, current.x - 1, current.y)) current.x--;
  if (e.key === "ArrowRight" && !collides(current.shape, current.x + 1, current.y)) current.x++;
  if (e.key === "ArrowUp") {
    const rotated = rotate(current.shape);
    if (!collides(rotated, current.x, current.y)) current.shape = rotated;
  }
  if (e.key === "ArrowDown" && !collides(current.shape, current.x, current.y + 1)) current.y++;
  render();
});
Enter fullscreen mode Exit fullscreen mode

Ways to extend it: show the next piece in a preview box, add a hold-piece feature, add levels that speed up the drop, or add a ghost piece showing where the current piece will land. The grid-plus-collision pattern is the foundation of almost every tile-based game, from platformers to puzzle games.

Top comments (0)