Skip to content
TheChatArchiveThe Chat Archive
grok-4-0709· xAIHugging Face LMArena Human Preference 140K

create a snake game using html css and javascript in a single html file with a unique…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-235b-a22b-no-thinking’s answer.
  1. User

    create a snake game using html css and javascript in a single html file with a unique twist

  2. Assistant
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Snake Game with Portal Twist</title>
        <style>
            body {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                margin: 0;
                background-color: #f0f0f0;
                font-family: Arial, sans-serif;
            }
            #game-container {
                text-align: center;
            }
            #canvas {
                border: 2px solid #333;
                background-color: #fff;
                box-shadow: 0 0 10px rgba(0,0,0,0.2);
            }
            #score {
                font-size: 24px;
                margin-bottom: 10px;
            }
            #instructions {
                font-size: 16px;
                margin-top: 10px;
                color: #666;
            }
        </style>
    </head>
    <body>
        <div id="game-container">
            <div id="score">Score: 0</div>
            <canvas id="canvas" width="400" height="400"></canvas>
            <div id="instructions">Use arrow keys to move. Eat red food. Blue portals teleport you!</div>
        </div>
    
        <script>
            const canvas = document.getElementById('canvas');
            const ctx = canvas.getContext('2d');
            const scoreElement = document.getElementById('score');
    
            // Game settings
            const gridSize = 20; // Each cell is 20x20 pixels
            const tileCount = canvas.width / gridSize; // 20x20 grid
    
            // Snake initial position and properties
            let snake = [{ x: 10, y: 10 }];
            let dx = 1; // Direction: right
            let dy = 0;
            let foodX, foodY;
            let score = 0;
            let gameOver = false;
    
            // Portal positions (unique twist: two portals that teleport the snake)
            let portal1 = { x: 5, y: 5 };
            let portal2 = { x: 15, y: 15 };
    
            // Generate initial food
            generateFood();
    
            // Keyboard controls
            document.addEventListener('keydown', (e) => {
                if (e.key === 'ArrowUp' && dy !== 1) { dx = 0; dy = -1; }
                if (e.key === 'ArrowDown' && dy !== -1) { dx = 0; dy = 1; }
                if (e.key === 'ArrowLeft' && dx !== 1) { dx = -1; dy = 0; }
                if (e.key === 'ArrowRight' && dx !== -1) { dx = 1; dy = 0; }
            });
    
            // Game loop
            function gameLoop() {
                if (gameOver) {
                    ctx.fillStyle = 'red';
                    ctx.font = '30px Arial';
                    ctx.fillText('Game Over!', canvas.width / 4, canvas.height / 2);
                    return;
                }
    
                requestAnimationFrame(gameLoop);
                if (++frame % 10 !== 0) return; // Control speed
    
                update();
                draw();
            }
    
            let frame = 0;
    
            function update() {
                // Move snake
                let head = { x: snake[0].x + dx, y: snake[0].y + dy };
    
                // Check for portal teleport
                if (head.x === portal1.x && head.y === portal1.y) {
                    head = { x: portal2.x, y: portal2.y };
                } else if (head.x === portal2.x && head.y === portal2.y) {
                    head = { x: portal1.x, y: portal1.y };
                }
    
                // Wall collision (no wrap-around for twist focus)
                if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
                    gameOver = true;
                    return;
                }
    
                // Self collision
                for (let i = 0; i < snake.length; i++) {
                    if (head.x === snake[i].x && head.y === snake[i].y) {
                        gameOver = true;
                        return;
                    }
                }
    
                // Eat food
                if (head.x === foodX && head.y === foodY) {
                    score++;
                    scoreElement.textContent = `Score: ${score}`;
                    generateFood();
                    // Grow snake
                    snake.unshift(head);
                    // Occasionally move portals for added twist
                    if (score % 3 === 0) {
                        generatePortals();
                    }
                } else {
                    // Move snake without growing
                    snake.unshift(head);
                    snake.pop();
                }
            }
    
            function draw() {
                // Clear canvas
                ctx.clearRect(0, 0, canvas.width, canvas.height);
    
                // Draw food
                ctx.fillStyle = 'red';
                ctx.fillRect(foodX * gridSize, foodY * gridSize, gridSize, gridSize);
    
                // Draw portals (blue circles for visual)
                ctx.fillStyle = 'blue';
                ctx.beginPath();
                ctx.arc(portal1.x * gridSize + gridSize / 2, portal1.y * gridSize + gridSize / 2, gridSize / 2, 0, 2 * Math.PI);
                ctx.fill();
                ctx.beginPath();
                ctx.arc(portal2.x * gridSize + gridSize / 2, portal2.y * gridSize + gridSize / 2, gridSize / 2, 0, 2 * Math.PI);
                ctx.fill();
    
                // Draw snake
                ctx.fillStyle = 'green';
                snake.forEach(segment => {
                    ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
                });
            }
    
            function generateFood() {
                foodX = Math.floor(Math.random() * tileCount);
                foodY = Math.floor(Math.random() * tileCount);
                // Ensure food not on snake or portals
                while (isOnSnake(foodX, foodY) || isOnPortal(foodX, foodY)) {
                    foodX = Math.floor(Math.random() * tileCount);
                    foodY = Math.floor(Math.random() * tileCount);
                }
            }
    
            function generatePortals() {
                portal1.x = Math.floor(Math.random() * tileCount);
                portal1.y = Math.floor(Math.random() * tileCount);
                portal2.x = Math.floor(Math.random() * tileCount);
                portal2.y = Math.floor(Math.random() * tileCount);
                // Ensure portals not on each other, snake, or food
                while ((portal1.x === portal2.x && portal1.y === portal2.y) ||
                       isOnSnake(portal1.x, portal1.y) || isOnSnake(portal2.x, portal2.y) ||
                       (foodX === portal1.x && foodY === portal1.y) || (foodX === portal2.x && foodY === portal2.y)) {
                    portal1.x = Math.floor(Math.random() * tileCount);
                    portal1.y = Math.floor(Math.random() * tileCount);
                    portal2.x = Math.floor(Math.random() * tileCount);
                    portal2.y = Math.floor(Math.random() * tileCount);
                }
            }
    
            function isOnSnake(x, y) {
                return snake.some(segment => segment.x === x && segment.y === y);
            }
    
            function isOnPortal(x, y) {
                return (x === portal1.x && y === portal1.y) || (x === portal2.x && y === portal2.y);
            }
    
            // Start the game
            gameLoop();
        </script>
    </body>
    </html>
    

    Explanation:

    • Classic Snake Mechanics: The snake moves on a grid, eats red food to grow and score points, and dies on wall or self-collision.
    • Unique Twist: Blue portals (wormholes) on the board. Entering one teleports the snake to the other. Portals relocate every 3 points for added challenge and surprise.
    • How to Play: Use arrow keys. The game runs in the browser. Refresh to restart.
    • Single File: Everything is in this HTML file, with CSS in <style> and JS in <script>.

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