001
002
003
004type Matrix = number[][];
005type Piece = { matrix: Matrix; x: number; y: number };
006
007const COLS = 10;
008const ROWS = 20;
009const CELL = 24;
010const STEP = 650;
011const INK = "#72b8b5";
012const SHAPES: Matrix[] = [
013 [[1, 1, 1, 1]],
014 [[1, 1], [1, 1]],
015 [[0, 1, 0], [1, 1, 1]],
016 [[0, 1, 1], [1, 1, 0]],
017 [[1, 1, 0], [0, 1, 1]],
018 [[1, 0, 0], [1, 1, 1]],
019 [[0, 0, 1], [1, 1, 1]],
020];
021
022const canvas = document.createElement("canvas");
023const ctx = canvas.getContext("2d")!;
024canvas.width = COLS * CELL;
025canvas.height = ROWS * CELL;
026document.body.append(canvas);
027
028const board: Matrix = Array.from(
029 { length: ROWS }, () => Array(COLS).fill(0)
030);
031let piece = spawn();
032let score = 0;
033let elapsed = 0;
034let previous = 0;
035
036function spawn(): Piece {
037 const index = Math.floor(Math.random() * SHAPES.length);
038 const matrix = SHAPES[index].map(row => [...row]);
039 return { matrix, x: Math.floor((COLS - matrix[0].length) / 2), y: 0 };
040}
041
042function rotate(matrix: Matrix): Matrix {
043 return matrix[0].map((_, x) =>
044 matrix.map(row