Skip to content
TheChatArchiveThe Chat Archive
Amazon Novaamazon.nova-pro-v1:0· AmazonHugging Face LMArena Human Preference 140K

create a simple snake game in a html file. Make it look as simple as possible

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Coherecommand-a-03-2025’s answer.
  1. User

    create a simple snake game in a html file. Make it look as simple as possible

  2. Amazon Nova Assistant

    Certainly! Below is a simple implementation of a Snake game in HTML, CSS, and JavaScript. This version is designed to be minimalistic and easy to understand.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Simple Snake Game</title>
        <style>
            body {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                margin: 0;
                background-color: #f0f0f0;
            }
            #game-container {
                display: grid;
                grid-template-columns: repeat(20, 20px);
                grid-template-rows: repeat(20, 20px);
            }
            .cell {
                width: 20px;
                height: 20px;
            }
            .snake {
                background-color: green;
            }
            .food {
                background-color: red;
            }
        </style>
    </head>
    <body>
        <div id="game-container"></div>
    
        <script>
            const gridSize = 20;
            let snake = [{ x: 10, y: 10 }];
            let food = { x: 5, y: 5 };
            let direction = { x: 0, y: 0 };
            let gameOver = false;
    
            function createGrid() {
                const gameContainer = document.getElementById('game-container');
                gameContainer.innerHTML = '';
                for (let i = 0; i < gridSize * gridSize; i++) {
                    const cell = document.createElement('div');
                    cell.classList.add('cell');
                    gameContainer.appendChild(cell);
                }
            }
    
            function draw() {
                const gameContainer = document.getElementById('game-container');
                gameContainer.innerHTML = '';
                snake.forEach(segment => {
                    const cell = document.createElement('div');
                    cell.classList.add('cell', 'snake');
                    cell.style.gridColumn = segment.x + 1;
                    cell.style.gridRow = segment.y + 1;
                    gameContainer.appendChild(cell);
                });
    
                const foodCell = document.createElement('div');
                foodCell.classList.add('cell', 'food');
                foodCell.style.gridColumn = food.x + 1;
                foodCell.style.gridRow = food.y + 1;
                gameContainer.appendChild(foodCell);
            }
    
            function update() {
                if (gameOver) return;
    
                let newHead = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
    
                if (newHead.x === food.x && newHead.y === food.y) {
                    snake.unshift(newHead);
                    food = {
                        x: Math.floor(Math.random() * gridSize),
                        y: Math.floor(Math.random() * gridSize)
                    };
                } else {
                    snake.pop();
                    snake.unshift(newHead);
                }
    
                if (newHead.x < 0 || newHead.x >= gridSize || newHead.y < 0 || newHead.y >= gridSize || snake.slice(1).some(segment => segment.x === newHead.x && segment.y === newHead.y)) {
                    gameOver = true;
                    alert('Game Over!');
                    return;
                }
    
                draw();
            }
    
            function changeDirection(event) {
                const keyPressed = event.key;
                const goingUp = direction.y === -1;
                const goingDown = direction.y === 1;
                const goingRight = direction.x === 1;
                const goingLeft = direction.x === -1;
    
                if (keyPressed === 'ArrowUp' && !goingDown) {
                    direction = { x: 0, y: -1 };
                } else if (keyPressed === 'ArrowDown' && !goingUp) {
                    direction = { x: 0, y: 1 };
                } else if (keyPressed === 'ArrowLeft' && !goingRight) {
                    direction = { x: -1, y: 0 };
                } else if (keyPressed === 'ArrowRight' && !goingLeft) {
                    direction = { x: 1, y: 0 };
                }
            }
    
            document.addEventListener('keydown', changeDirection);
            setInterval(update, 200);
            createGrid();
            draw();
        </script>
    </body>
    </html>
    

    Explanation:

    1. HTML Structure:

      • A simple <div> with the ID game-container to hold the grid.
    2. CSS:

      • Basic styling to center the game and create a grid layout.
    3. JavaScript:

      • Initializes the snake and food positions.
      • Creates the grid and draws the snake and food.
      • Updates the snake's position based on keyboard input.
      • Checks for collisions with the walls or itself to end the game.

    Copy and paste this code into an HTML file and open it in your browser to play the game. Use the arrow keys to control the snake.

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