Models 44

Gemini 3.1 Flash Lite

by GoogleReasoningClean
Open in new window

Running live in a sandboxed frame. Click inside it before using the keyboard.

How it went

Total time
7.7 s
Cost
$0.0047
Output tokens
3,020
Spent thinking
132 tokens
Speed
446.1 tok/s
Console errors
0
First token after
939 ms
First visible text after
939 ms
Prompt tokens
507
Code size
8.8 KB
Finish reason
stop
Served by
Google
Model id
google/gemini-3.1-flash-lite
Reasoning sent
{"effort":"low"}
Max output
65,536 tokens
Generated
Nov 12, 2026

What it was asked

The prompt
Build a polished Snake game in a single HTML file using vanilla JavaScript and a <canvas>. Requirements: - A grid of at least 20×20 cells, centered on the page. - Arrow keys and WASD control the snake. Quick successive key presses must both be honored, but the snake can never reverse onto itself. - Eating food adds 10 points and grows the snake by one cell; new food never spawns on the snake. - Game over when the snake hits a wall or itself, with an overlay showing the score; Space or Enter restarts, P pauses. - Visual polish: smooth movement, a snake with a distinct head and eyes facing its direction, and a small particle burst when eating. - Display the current score and the best score of the session.
System prompt, sent with every prompt
You are generating a self-contained web demo. It will be rendered automatically in a sandboxed iframe and compared side by side with the output of other AI models given the exact same task.

Output rules:
- Reply with exactly ONE fenced code block containing the complete file: ```html (or ```svg when the task asks for an SVG). No text before or after the code block.
- Everything must be inline: CSS in <style>, JavaScript in <script>. No external files, images, fonts or network requests, except the CDN URLs explicitly allowed by the task.
- Do not use localStorage, sessionStorage, cookies, IndexedDB, alert(), confirm(), prompt() or window.open(): they are blocked in the sandbox and will throw.
- The page must look right in a 1280×800 viewport and adapt gracefully down to 375px wide. No horizontal scrollbar.
- Something meaningful must be visible on first paint: a screenshot is taken automatically about 3 seconds after load, without any interaction.
- Everything that makes the demo interesting must be visible within the first seconds: nothing hidden behind a high score, a later level or a long play session.
- Games must start in a self-playing demo mode (a simple AI plays) behind a "Click or press a key to start" message, so the game can be judged in action without playing.
- The iframe only receives keyboard events after the user clicks it: if the demo uses the keyboard, display a clear "Click or press a key to start" message.
- The code must run with zero errors in the console of the latest Chrome.

Code run-1.html · 287 lines

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Polished Snake</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background: #1a1a2e;
            color: #e94560;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            overflow: hidden;
        }

        #game-container {
            position: relative;
            box-shadow: 0 0 20px rgba(0,0,0,0.5);
            border: 4px solid #16213e;
            background: #0f3460;
            border-radius: 8px;
        }

        canvas {
            display: block;
            background: #0f3460;
            cursor: pointer;
        }

        #ui-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            background: rgba(15, 52, 96, 0.85);
            color: white;
            z-index: 10;
        }

        #score-board {
            position: absolute;
            top: -40px;
            width: 100%;
            display: flex;
            justify-content: space-between;
            font-weight: bold;
            font-size: 1.2rem;
            color: #fff;
        }

        h1 { margin: 0 0 10px 0; }
        .hidden { display: none !important; }
        .key-hint { margin-top: 10px; font-size: 0.9rem; opacity: 0.8; }
    </style>
</head>
<body>

    <div id="game-container">
        <div id="score-board">
            <span>Score: <span id="current-score">0</span></span>
            <span>Best: <span id="best-score">0</span></span>
        </div>
        <canvas id="gameCanvas" width="400" height="400"></canvas>
        <div id="ui-overlay">
            <h1 id="status-title">SNAKE</h1>
            <p id="status-desc">Click or press any key to start</p>
        </div>
    </div>

<script>
    const canvas = document.getElementById('gameCanvas');
    const ctx = canvas.getContext('2d');
    const scoreEl = document.getElementById('current-score');
    const bestEl = document.getElementById('best-score');
    const overlay = document.getElementById('ui-overlay');
    const titleEl = document.getElementById('status-title');

    // Game Config
    const GRID_SIZE = 20;
    const TILE_COUNT = canvas.width / GRID_SIZE;
    const SNAKE_SPEED = 100; // ms per tick

    // State
    let snake = [{x: 10, y: 10}];
    let food = {x: 5, y: 5};
    let dx = 1;
    let dy = 0;
    let nextDx = 1;
    let nextDy = 0;
    let score = 0;
    let bestScore = 0;
    let particles = [];
    let gameLoop;
    let isPaused = false;
    let isRunning = false;
    let isDemoMode = true;

    // Listeners
    document.addEventListener('keydown', handleKey);
    canvas.addEventListener('click', startGame);

    function handleKey(e) {
        if (!isRunning) {
            startGame();
            return;
        }

        const key = e.key.toLowerCase();
        if (key === 'p') {
            togglePause();
            return;
        }
        if (key === ' ' || key === 'enter') {
            if (isPaused) togglePause();
            return;
        }

        // Prevent reversing
        if ((key === 'arrowup' || key === 'w') && dy !== 1) { nextDx = 0; nextDy = -1; }
        else if ((key === 'arrowdown' || key === 's') && dy !== -1) { nextDx = 0; nextDy = 1; }
        else if ((key === 'arrowleft' || key === 'a') && dx !== 1) { nextDx = -1; nextDy = 0; }
        else if ((key === 'arrowright' || key === 'd') && dx !== -1) { nextDx = 1; nextDy = 0; }
    }

    function togglePause() {
        if (isPaused) {
            isPaused = false;
            overlay.classList.add('hidden');
        } else {
            isPaused = true;
            overlay.classList.remove('hidden');
            titleEl.innerText = "PAUSED";
        }
    }

    function startGame() {
        if (isRunning && !isPaused) return;
        if (isPaused) { togglePause(); return; }
        
        // Reset
        snake = [{x: 10, y: 10}, {x: 9, y: 10}, {x: 8, y: 10}];
        dx = 1; dy = 0;
        nextDx = 1; nextDy = 0;
        score = 0;
        particles = [];
        isRunning = true;
        isDemoMode = false;
        scoreEl.innerText = 0;
        overlay.classList.add('hidden');
        
        spawnFood();
        if (gameLoop) clearInterval(gameLoop);
        gameLoop = setInterval(update, SNAKE_SPEED);
    }

    function spawnFood() {
        let valid = false;
        while (!valid) {
            food = {
                x: Math.floor(Math.random() * TILE_COUNT),
                y: Math.floor(Math.random() * TILE_COUNT)
            };
            valid = !snake.some(segment => segment.x === food.x && segment.y === food.y);
        }
    }

    function createParticles(x, y) {
        for(let i = 0; i < 8; i++) {
            particles.push({
                x: (x * GRID_SIZE) + GRID_SIZE/2,
                y: (y * GRID_SIZE) + GRID_SIZE/2,
                vx: (Math.random() - 0.5) * 4,
                vy: (Math.random() - 0.5) * 4,
                life: 1.0
            });
        }
    }

    function update() {
        if (isPaused) return;

        // AI Demo Logic
        if (isDemoMode) {
            // Very simple AI: chase food
            if (snake[0].x < food.x && dx !== -1) { nextDx = 1; nextDy = 0; }
            else if (snake[0].x > food.x && dx !== 1) { nextDx = -1; nextDy = 0; }
            else if (snake[0].y < food.y && dy !== -1) { nextDx = 0; nextDy = 1; }
            else if (snake[0].y > food.y && dy !== 1) { nextDx = 0; nextDy = -1; }
        }

        dx = nextDx;
        dy = nextDy;

        const head = { x: snake[0].x + dx, y: snake[0].y + dy };

        // Collision detection
        if (head.x < 0 || head.x >= TILE_COUNT || head.y < 0 || head.y >= TILE_COUNT || 
            snake.some(seg => seg.x === head.x && seg.y === head.y)) {
            gameOver();
            return;
        }

        snake.unshift(head);

        // Eating
        if (head.x === food.x && head.y === food.y) {
            score += 10;
            scoreEl.innerText = score;
            if (score > bestScore) {
                bestScore = score;
                bestEl.innerText = bestScore;
            }
            createParticles(head.x, head.y);
            spawnFood();
        } else {
            snake.pop();
        }

        // Particle update
        particles.forEach((p, i) => {
            p.x += p.vx;
            p.y += p.vy;
            p.life -= 0.05;
            if (p.life <= 0) particles.splice(i, 1);
        });

        draw();
    }

    function gameOver() {
        clearInterval(gameLoop);
        isRunning = false;
        overlay.classList.remove('hidden');
        titleEl.innerText = "GAME OVER";
        document.getElementById('status-desc').innerText = "Click to Restart";
    }

    function draw() {
        // Clear
        ctx.fillStyle = '#0f3460';
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        // Draw Food
        ctx.fillStyle = '#e94560';
        ctx.beginPath();
        ctx.arc((food.x * GRID_SIZE) + GRID_SIZE/2, (food.y * GRID_SIZE) + GRID_SIZE/2, GRID_SIZE/3, 0, Math.PI*2);
        ctx.fill();

        // Draw Snake
        snake.forEach((part, index) => {
            ctx.fillStyle = index === 0 ? '#4ecca3' : '#45b7af';
            ctx.fillRect(part.x * GRID_SIZE + 1, part.y * GRID_SIZE + 1, GRID_SIZE - 2, GRID_SIZE - 2);
            
            // Head eyes
            if (index === 0) {
                ctx.fillStyle = '#1a1a2e';
                const eyeSize = 3;
                let offset = 4;
                if (dx === 1) ctx.fillRect(part.x * GRID_SIZE + 12, part.y * GRID_SIZE + offset, eyeSize, eyeSize), ctx.fillRect(part.x * GRID_SIZE + 12, part.y * GRID_SIZE + 12, eyeSize, eyeSize);
                else if (dx === -1) ctx.fillRect(part.x * GRID_SIZE + 4, part.y * GRID_SIZE + offset, eyeSize, eyeSize), ctx.fillRect(part.x * GRID_SIZE + 4, part.y * GRID_SIZE + 12, eyeSize, eyeSize);
                else if (dy === 1) ctx.fillRect(part.x * GRID_SIZE + offset, part.y * GRID_SIZE + 12, eyeSize, eyeSize), ctx.fillRect(part.x * GRID_SIZE + 12, part.y * GRID_SIZE + 12, eyeSize, eyeSize);
                else ctx.fillRect(part.x * GRID_SIZE + offset, part.y * GRID_SIZE + 4, eyeSize, eyeSize), ctx.fillRect(part.x * GRID_SIZE + 12, part.y * GRID_SIZE + 4, eyeSize, eyeSize);
            }
        });

        // Draw Particles
        particles.forEach(p => {
            ctx.fillStyle = `rgba(233, 69, 96, ${p.life})`;
            ctx.fillRect(p.x, p.y, 3, 3);
        });
    }

    // Initial draw to show something before start
    draw();
</script>
</body>
</html>