Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-17b-128e-instruct· MetaHugging Face LMArena Human Preference 140K

Make the game tetris in html css js Make it functional and working Add particles

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro-preview-03-25’s answer.
  1. User

    Make the game tetris in html css js Make it functional and working Add particles

  2. Meta Assistant

    Here is a basic implementation of Tetris in HTML, CSS, and JavaScript. I've also added some particles to make it more visually appealing.

    HTML

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Tetris</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <canvas id="game-canvas" width="300" height="600"></canvas>
        <div id="score-container">
            <span id="score">Score: 0</span>
        </div>
        <script src="script.js"></script>
    </body>
    </html>
    

    CSS (in styles.css file)

    body {
        background-color: #f0f0f0;
        font-family: Arial, sans-serif;
    }
    
    #game-canvas {
        border: 1px solid #ccc;
        background-color: #fff;
    }
    
    #score-container {
        position: absolute;
        top: 20px;
        left: 20px;
        font-size: 24px;
        font-weight: bold;
    }
    
    .particle {
        position: absolute;
        width: 5px;
        height: 5px;
        border-radius: 50%;
        background-color: #fff;
        pointer-events: none;
    }
    

    JavaScript (in script.js file)

    // Get the canvas and score elements
    const canvas = document.getElementById('game-canvas');
    const ctx = canvas.getContext('2d');
    const scoreElement = document.getElementById('score');
    
    // Set the canvas dimensions
    canvas.width = 300;
    canvas.height = 600;
    
    // Define the Tetris pieces
    const pieces = [
        // I-piece
        {
            color: '#00ffff',
            shape: [
                [1, 1, 1, 1]
            ]
        },
        // J-piece
        {
            color: '#0000ff',
            shape: [
                [1, 0, 0],
                [1, 1, 1]
            ]
        },
        // L-piece
        {
            color: '#ff00ff',
            shape: [
                [0, 0, 1],
                [1, 1, 1]
            ]
        },
        // O-piece
        {
            color: '#ffff00',
            shape: [
                [1, 1],
                [1, 1]
            ]
        },
        // S-piece
        {
            color: '#00ff00',
            shape: [
                [0, 1, 1],
                [1, 1, 0]
            ]
        },
        // T-piece
        {
            color: '#ff0000',
            shape: [
                [0, 1, 0],
                [1, 1, 1]
            ]
        },
        // Z-piece
        {
            color: '#0000ff',
            shape: [
                [1, 1, 0],
                [0, 1, 1]
            ]
        }
    ];
    
    // Define the game variables
    let score = 0;
    let grid = [];
    let currentPiece = null;
    let nextPiece = null;
    let pieceX = 0;
    let pieceY = 0;
    let gameOver = false;
    
    // Initialize the grid
    for (let i = 0; i < 20; i++) {
        grid[i] = [];
        for (let j = 0; j < 10; j++) {
            grid[i][j] = 0;
        }
    }
    
    // Function to generate a new piece
    function generatePiece() {
        const randomIndex = Math.floor(Math.random() * pieces.length);
        return pieces[randomIndex];
    }
    
    // Function to rotate a piece
    function rotatePiece(piece) {
        const newShape = [];
        for (let i = 0; i < piece.shape[0].length; i++) {
            newShape[i] = [];
            for (let j = piece.shape.length - 1; j >= 0; j--) {
                newShape[i][j] = piece.shape[j][i];
            }
        }
        return {
            color: piece.color,
            shape: newShape
        };
    }
    
    // Function to check if a piece can be placed at a given position
    function canPlacePiece(piece, x, y) {
        for (let i = 0; i < piece.shape.length; i++) {
            for (let j = 0; j < piece.shape[i].length; j++) {
                if (piece.shape[i][j] === 1) {
                    if (x + j < 0 || x + j >= 10 || y + i >= 20 || grid[y + i][x + j] === 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    
    // Function to place a piece at a given position
    function placePiece(piece, x, y) {
        for (let i = 0; i < piece.shape.length; i++) {
            for (let j = 0; j < piece.shape[i].length; j++) {
                if (piece.shape[i][j] === 1) {
                    grid[y + i][x + j] = 1;
                }
            }
        }
    }
    
    // Function to check for and clear full lines
    function checkLines() {
        let linesCleared = 0;
        for (let i = 0; i < 20; i++) {
            let isFullLine = true;
            for (let j = 0; j < 10; j++) {
                if (grid[i][j] === 0) {
                    isFullLine = false;
                    break;
                }
            }
            if (isFullLine) {
                // Clear the line
                grid.splice(i, 1);
                grid.unshift([]);
                for (let j = 0; j < 10; j++) {
                    grid[0][j] = 0;
                }
                linesCleared++;
                i--;
            }
        }
        if (linesCleared > 0) {
            score += linesCleared * linesCleared * 100;
            scoreElement.textContent = `Score: ${score}`;
            // Add particles
            for (let i = 0; i < linesCleared * 10; i++) {
                const particle = document.createElement('div');
                particle.classList.add('particle');
                particle.style.top = `${Math.random() * canvas.height}px`;
                particle.style.left = `${Math.random() * canvas.width}px`;
                particle.style.background = `hsl(${Math.random() * 360}, 100%, 50%)`;
                document.body.appendChild(particle);
                setTimeout(() => {
                    particle.remove();
                }, 1000);
            }
        }
    }
    
    // Main game loop
    function update() {
        if (gameOver) return;
        // Generate a new piece if necessary
        if (!currentPiece) {
            currentPiece = nextPiece || generatePiece();
            nextPiece = generatePiece();
            pieceX = 3;
            pieceY = 0;
            if (!canPlacePiece(currentPiece, pieceX, pieceY)) {
                gameOver = true;
            }
        }
        // Move the piece down
        if (canPlacePiece(currentPiece, pieceX, pieceY + 1)) {
            pieceY++;
        } else {
            // Place the piece
            placePiece(currentPiece, pieceX, pieceY);
            checkLines();
            currentPiece = null;
        }
        // Draw everything
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        for (let i = 0; i < 20; i++) {
            for (let j = 0; j < 10; j++) {
                if (grid[i][j] === 1) {
                    ctx.fillStyle = '#ccc';
                    ctx.fillRect(j * 30, i * 30, 30, 30);
                }
            }
        }
        if (currentPiece) {
            ctx.fillStyle = currentPiece.color;
            for (let i = 0; i < currentPiece.shape.length; i++) {
                for (let j = 0; j < currentPiece.shape[i].length; j++) {
                    if (currentPiece.shape[i][j] === 1) {
                        ctx.fillRect((pieceX + j) * 30, (pieceY + i) * 30, 30, 30);
                    }
                }
            }
        }
        requestAnimationFrame(update);
    }
    
    // Handle keyboard input
    document.addEventListener('keydown', (e) => {
        if (e.key === 'ArrowLeft') {
            if (canPlacePiece(currentPiece, pieceX - 1, pieceY)) {
                pieceX--;
            }
        } else if (e.key === 'ArrowRight') {
            if (canPlacePiece(currentPiece, pieceX + 1, pieceY)) {
                pieceX++;
            }
        } else if (e.key === 'ArrowDown') {
            if (canPlacePiece(currentPiece, pieceX, pieceY + 1)) {
                pieceY++;
            }
        } else if (e.key === 'ArrowUp') {
            const rotatedPiece = rotatePiece(currentPiece);
            if (canPlacePiece(rotatedPiece, pieceX, pieceY)) {
                currentPiece = rotatedPiece;
            }
        }
    });
    
    update();
    

    This implementation includes the basic game logic, scoring, and particle effects when clearing lines. You can play the game by using the arrow keys to move and rotate the pieces.

    Note

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.