Changed around line 1
+ const canvas = document.getElementById('gameCanvas');
+ const ctx = canvas.getContext('2d');
+ const nextCanvas = document.getElementById('nextCanvas');
+ const nextCtx = nextCanvas.getContext('2d');
+ const BLOCK_SIZE = 30;
+ const BOARD_WIDTH = 10;
+ const BOARD_HEIGHT = 20;
+
+ // Set canvas sizes
+ canvas.width = BLOCK_SIZE * BOARD_WIDTH;
+ canvas.height = BLOCK_SIZE * BOARD_HEIGHT;
+ nextCanvas.width = BLOCK_SIZE * 4;
+ nextCanvas.height = BLOCK_SIZE * 4;
+
+ let board = Array(BOARD_HEIGHT).fill().map(() => Array(BOARD_WIDTH).fill(0));
+ let score = 0;
+ let lines = 0;
+ let currentPiece = null;
+ let nextPiece = null;
+
+ const PIECES = [
+ [[1,1,1,1]], // I
+ [[1,1,1],[0,1,0]], // T
+ [[1,1,1],[1,0,0]], // L
+ [[1,1,1],[0,0,1]], // J
+ [[1,1],[1,1]], // O
+ [[1,1,0],[0,1,1]], // S
+ [[0,1,1],[1,1,0]] // Z
+ ];
+
+ const COLORS = ['#4fdcff', '#ff6b6b', '#45ffa5', '#ffd93d', '#ff8d4f', '#ba68c8', '#69a3ff'];
+
+ function createPiece() {
+ const pieceIndex = Math.floor(Math.random() * PIECES.length);
+ return {
+ shape: PIECES[pieceIndex],
+ color: COLORS[pieceIndex],
+ x: Math.floor(BOARD_WIDTH/2) - 1,
+ y: 0
+ };
+ }
+
+ function drawBlock(ctx, x, y, color) {
+ ctx.fillStyle = color;
+ ctx.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE-1, BLOCK_SIZE-1);
+ ctx.strokeStyle = 'rgba(255,255,255,0.2)';
+ ctx.strokeRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE-1, BLOCK_SIZE-1);
+ }
+
+ function drawBoard() {
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+
+ // Draw board
+ for(let y = 0; y < BOARD_HEIGHT; y++) {
+ for(let x = 0; x < BOARD_WIDTH; x++) {
+ if(board[y][x]) {
+ drawBlock(ctx, x, y, board[y][x]);
+ }
+ }
+ }
+
+ // Draw current piece
+ if(currentPiece) {
+ currentPiece.shape.forEach((row, y) => {
+ row.forEach((value, x) => {
+ if(value) {
+ drawBlock(ctx, currentPiece.x + x, currentPiece.y + y, currentPiece.color);
+ }
+ });
+ });
+ }
+ }
+
+ function drawNextPiece() {
+ nextCtx.clearRect(0, 0, nextCanvas.width, nextCanvas.height);
+ if(nextPiece) {
+ nextPiece.shape.forEach((row, y) => {
+ row.forEach((value, x) => {
+ if(value) {
+ drawBlock(nextCtx, x + 1, y + 1, nextPiece.color);
+ }
+ });
+ });
+ }
+ }
+
+ function isValidMove(piece, newX, newY) {
+ return piece.shape.every((row, dy) => {
+ return row.every((value, dx) => {
+ let x = newX + dx;
+ let y = newY + dy;
+ return (
+ value === 0 ||
+ (x >= 0 && x < BOARD_WIDTH && y < BOARD_HEIGHT && !board[y]?.[x])
+ );
+ });
+ });
+ }
+
+ function rotate(piece) {
+ const newShape = piece.shape[0].map((_, i) =>
+ piece.shape.map(row => row[i]).reverse()
+ );
+ return { ...piece, shape: newShape };
+ }
+
+ function mergePiece() {
+ currentPiece.shape.forEach((row, y) => {
+ row.forEach((value, x) => {
+ if(value) {
+ board[currentPiece.y + y][currentPiece.x + x] = currentPiece.color;
+ }
+ });
+ });
+ }
+
+ function clearLines() {
+ let linesCleared = 0;
+
+ for(let y = BOARD_HEIGHT - 1; y >= 0; y--) {
+ if(board[y].every(cell => cell)) {
+ board.splice(y, 1);
+ board.unshift(Array(BOARD_WIDTH).fill(0));
+ linesCleared++;
+ y++;
+ }
+ }
+
+ if(linesCleared > 0) {
+ lines += linesCleared;
+ score += [40, 100, 300, 1200][linesCleared - 1];
+ document.getElementById('score').textContent = score;
+ document.getElementById('lines').textContent = lines;
+ }
+ }
+
+ function gameLoop() {
+ if(currentPiece && isValidMove(currentPiece, currentPiece.x, currentPiece.y + 1)) {
+ currentPiece.y++;
+ } else {
+ if(currentPiece) {
+ mergePiece();
+ clearLines();
+ }
+ currentPiece = nextPiece || createPiece();
+ nextPiece = createPiece();
+ drawNextPiece();
+
+ if(!isValidMove(currentPiece, currentPiece.x, currentPiece.y)) {
+ alert('Game Over! Score: ' + score);
+ resetGame();
+ }
+ }
+
+ drawBoard();
+ requestAnimationFrame(gameLoop);
+ }
+
+ function resetGame() {
+ board = Array(BOARD_HEIGHT).fill().map(() => Array(BOARD_WIDTH).fill(0));
+ score = 0;
+ lines = 0;
+ document.getElementById('score').textContent = score;
+ document.getElementById('lines').textContent = lines;
+ }
+
+ // Controls
+ document.addEventListener('keydown', e => {
+ if(!currentPiece) return;
+
+ switch(e.key) {
+ case 'ArrowLeft':
+ if(isValidMove(currentPiece, currentPiece.x - 1, currentPiece.y)) {
+ currentPiece.x--;
+ drawBoard();
+ }
+ break;
+ case 'ArrowRight':
+ if(isValidMove(currentPiece, currentPiece.x + 1, currentPiece.y)) {
+ currentPiece.x++;
+ drawBoard();
+ }
+ break;
+ case 'ArrowDown':
+ if(isValidMove(currentPiece, currentPiece.x, currentPiece.y + 1)) {
+ currentPiece.y++;
+ drawBoard();
+ }
+ break;
+ case 'ArrowUp':
+ const rotated = rotate(currentPiece);
+ if(isValidMove(rotated, rotated.x, rotated.y)) {
+ currentPiece = rotated;
+ drawBoard();
+ }
+ break;
+ case ' ':
+ while(isValidMove(currentPiece, currentPiece.x, currentPiece.y + 1)) {
+ currentPiece.y++;
+ }
+ drawBoard();
+ break;
+ }
+ });
+
+ // Mobile controls
+ document.querySelectorAll('.control-btn').forEach(btn => {
+ btn.addEventListener('touchstart', (e) => {
+ e.preventDefault();
+ btn.dispatchEvent(new Event('click'));
+ });
+ });
+
+ document.querySelector('.left-btn').addEventListener('click', () => {
+ if(isValidMove(currentPiece, currentPiece.x - 1, currentPiece.y)) {
+ currentPiece.x--;
+ drawBoard();
+ }
+ });
+
+ document.querySelector('.right-btn').addEventListener('click', () => {
+ if(isValidMove(currentPiece, currentPiece.x + 1, currentPiece.y)) {
+ currentPiece.x++;
+ drawBoard();
+ }
+ });
+
+ document.querySelector('.rotate-btn').addEventListener('click', () => {
+ const rotated = rotate(currentPiece);
+ if(isValidMove(rotated, rotated.x, rotated.y)) {
+ currentPiece = rotated;
+ drawBoard();
+ }
+ });
+
+ document.querySelector('.down-btn').addEventListener('click', () => {
+ if(isValidMove(currentPiece, currentPiece.x, currentPiece.y + 1)) {
+ currentPiece.y++;
+ drawBoard();
+ }
+ });
+
+ // Start game
+ nextPiece = createPiece();
+ gameLoop();