Skip to content
The Chat Archive
amazon-nova-experimental-chat-05-14 LMArena Human Preference 140K

Create a fun platformer game in HTML/CSS/Javascript that I can control with the arrow…

  1. User

    Create a fun platformer game in HTML/CSS/Javascript that I can control with the arrow keys, make it fun and creative with lots of fun obstacles along the way

  2. Assistant

    Certainly! Below is a simple yet fun platformer game created using HTML, CSS, and JavaScript. You can control the character with the arrow keys. The game features various obstacles and collectibles to make it more engaging.

    Index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Fun Platformer Game</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <canvas id="gameCanvas" width="800" height="600"></canvas>
        <script src="game.js" defer></script>
    </body>
    </html>
    

    styles.css

    body {
        margin: 0;
        overflow: hidden;
        background-color: #87CEEB;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
    }
    
    canvas {
        border: 2px solid #000;
    }
    

    game.js

    const canvas = document.getElementById('gameCanvas');
    const ctx = canvas.getContext('2d');
    
    // Game variables
    const gravity = 0.9;
    const friction = 0.85;
    let gameFrame = 0;
    let score = 0;
    
    // Player
    const player = {
        x: 50,
        y: canvas.height - 150,
        width: 50,
        height: 50,
        color: 'red',
        speed: 5,
        velY: 0,
        jumping: false,
        grounded: false
    };
    
    // Keys
    const keys = {
        ArrowUp: { pressed: false },
        ArrowDown: { pressed: false },
        ArrowLeft: { pressed: false },
        ArrowRight: { pressed: false }
    };
    
    // Platforms
    const platforms = [
        {x: 0, y: canvas.height - 50, width: canvas.width, height: 50, color: 'green'},
        {x: 200, y: canvas.height - 200, width: 200, height: 50, color: 'brown'},
        {x: 500, y: canvas.height - 350, width: 200, height: 50, color: 'purple'},
        {x: 100, y: canvas.height - 500, width: 200, height: 50, color: 'orange'}
    ];
    
    // Obstacles
    const obstacles = [
        {x: 400, y: canvas.height - 250, width: 50, height: 50, color: 'black'},
        {x: 600, y: canvas.height - 400, width: 50, height: 50, color: 'black'},
    ];
    
    // Collectibles
    const collectibles = [
        {x: 300, y: canvas.height - 450, width: 20, height: 20, color: 'yellow'},
        {x: 700, y: canvas.height - 550, width: 20, height: 20, color: 'yellow'},
    ];
    
    function drawRectangle(x, y, width, height, color) {
        ctx.fillStyle = color;
        ctx.fillRect(x, y, width, height);
    }
    
    function drawPlayer() {
        drawRectangle(player.x, player.y, player.width, player.height, player.color);
    }
    
    function drawPlatforms() {
        platforms.forEach(platform => {
            drawRectangle(platform.x, platform.y, platform.width, platform.height, platform.color);
        });
    }
    
    function drawObstacles() {
        obstacles.forEach(obstacle => {
            drawRectangle(obstacle.x, obstacle.y, obstacle.width, obstacle.height, obstacle.color);
        });
    }
    
    function drawCollectibles() {
        collectibles.forEach(collectible => {
            drawRectangle(collectible.x, collectible.y, collectible.width, collectible.height, collectible.color);
        });
    }
    
    function checkCollision(rect1, rect2) {
        return rect1.x < rect2.x + rect2.width &&
               rect1.x + rect1.width > rect2.x &&
               rect1.y < rect2.y + rect2.height &&
               rect1.y + rect1.height > rect2.y;
    }
    
    function updatePlayer() {
        // Apply gravity
        if (!player.grounded) {
            player.velY += gravity;
        } else {
            player.velY = 0;
        }
    	
        // Move player
        if (keys.ArrowLeft.pressed && player.x - player.speed > 0) {
            player.x -= player.speed;
        }
        if (keys.ArrowRight.pressed && player.x + player.speed + player.width < canvas.width) {
            player.x += player.speed;
        }
        if (keys.ArrowUp.pressed && player.grounded) {
            player.velY = -15;
            player.jumping = true;
            player.grounded = false;
        }
    
        player.y += player.velY;
    
        // Check for platform collision
        player.grounded = false;
        platforms.forEach(platform => {
            if (checkCollision(player, platform)) {
                player.grounded = true;
                player.y = platform.y - player.height;
            }
        });
    	
    	// Check for obstacle collision
        obstacles.forEach(obstacle => {
            if (checkCollision(player, obstacle)) {
                alert("Game Over! Your score: " + score);
                location.reload();
            }
        });
    	
    	// Check for collectible collision
        for (let i = 0; i < collectibles.length; i++) {
            if (checkCollision(player, collectibles[i])) {
                score += 10;
                collectibles.splice(i, 1);
                break;
            }
        }
    }
    
    function animate() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        drawPlatforms();
        drawObstacles();
        drawCollectibles();
        drawPlayer();
        updatePlayer();
    	
        // Display score
        ctx.fillStyle = 'black';
        ctx.font = '20px Arial';
        ctx.fillText(`Score: ${score}`, 10, 30);
    
        gameFrame++;
        requestAnimationFrame(animate);
    }
    
    // Key event listeners
    document.addEventListener('keydown', ({key}) => {
        switch (key) {
            case 'ArrowUp':
                keys.ArrowUp.pressed = true;
                break;
            case 'ArrowDown':
                keys.ArrowDown.pressed = true;
                break;
            case 'ArrowLeft':
                keys.ArrowRight.pressed = false;
                keys.ArrowLeft.pressed = true;
                break;
            case 'ArrowRight':
                keys.ArrowLeft.pressed = false;
                keys.ArrowRight.pressed = true;
                break;
        }
    });
    
    document.addEventListener('keyup', ({key}) => {
        switch (key) {
            case 'ArrowUp':
                keys.ArrowUp.pressed = false;
                break;
            case 'ArrowDown':
                keys.ArrowDown.pressed = false;
                break;
            case 'ArrowLeft':
                keys.ArrowLeft.pressed = false;
                break;
            case 'ArrowRight':
                keys.ArrowRight.pressed = false;
                break;
        }
    });
    
    animate();
    
    

    Explanation:

    1. HTML (index.html):

      • A simple HTML structure with a <canvas> element where the game will be rendered.
      • Links to the styles.css for styling and game.js for the game logic.
    2. CSS (styles.css):

      • Basic styling to centre the canvas and remove scrollbars.
      • A border is added to the canvas for better visibility.
    3. JavaScript (game.js):

      • Game Variables:
        • gravity, friction, gameFrame, and score to manage game physics and state.
      • Player Object:
        • Defines the player's properties like position, size, colour, speed, and velocity.
      • Keys Object:
        • Tracks the state (pressed or not) of the arrow keys.
      • Platforms, Obstacles, and Collectibles Arrays:
        • Define various platforms, obstacles, and collectibles with their positions, sizes, and colours.
      • Drawing Functions:
        • drawRectangle: Draws a rectangle on the canvas.
        • drawPlayer, drawPlatforms, drawObstacles, and drawCollectibles: Utilise drawRectangle to draw their respective objects.
      • checkCollision Function:
        • Checks if two rectangles collide.
      • updatePlayer Function:
        • Updates the player's position based on key presses and applies gravity.
        • Checks for collisions with platforms, obstacles, and collectibles.
      • animate Function:
        • The main game loop that clears the canvas, draws all game objects, updates the player, and displays the score.
        • Uses requestAnimationFrame to create a smooth animation loop.
      • Key Event Listeners:
        • Listen for keydown and keyup events to update the keys object, controlling the player's movement.

    To play the game, open the index.html file in a web browser. Use the arrow keys to move the player around, avoid obstacles, and collect yellow squares to increase your score. If the player touches a black obstacle the game will end and reload.

  3. User

    Put it all in one HTML file

  4. Assistant

    Certainly! Below is the complete game code consolidated into a single HTML file.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Fun Platformer Game</title>
        <style>
            body {
                margin: 0;
                overflow: hidden;
                background-color: #87CEEB;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
            }
    
            canvas {
                border: 2px solid #000;
            }
        </style>
    </head>
    <body>
        <canvas id="gameCanvas" width="800" height="600"></canvas>
        <script>
            const canvas = document.getElementById('gameCanvas');
            const ctx = canvas.getContext('2d');
    
            // Game variables
            const gravity = 0.9;
            const friction = 0.85;
            let gameFrame = 0;
            let score = 0;
    
            // Player
            const player = {
                x: 50,
                y: canvas.height - 150,
                width: 50,
                height: 50,
                color: 'red',
                speed: 5,
                velY: 0,
                jumping: false,
                grounded: false
            };
    
            // Keys
            const keys = {
                ArrowUp: { pressed: false },
                ArrowDown: { pressed: false },
                ArrowLeft: { pressed: false },
                ArrowRight: { pressed: false }
            };
    
            // Platforms
            const platforms = [
                {x: 0, y: canvas.height - 50, width: canvas.width, height: 50, color: 'green'},
                {x: 200, y: canvas.height - 200, width: 200, height: 50, color: 'brown'},
                {x: 500, y: canvas.height - 350, width: 200, height: 50, color: 'purple'},
                {x: 100, y: canvas.height - 500, width: 200, height: 50, color: 'orange'}
            ];
    
            // Obstacles
            const obstacles = [
                {x: 400, y: canvas.height - 250, width: 50, height: 50, color: 'black'},
                {x: 600, y: canvas.height - 400, width: 50, height: 50, color: 'black'},
            ];
    
            // Collectibles
            const collectibles = [
                {x: 300, y: canvas.height - 450, width: 20, height: 20, color: 'yellow'},
                {x: 700, y: canvas.height - 550, width: 20, height: 20, color: 'yellow'},
            ];
    
            function drawRectangle(x, y, width, height, color) {
                ctx.fillStyle = color;
                ctx.fillRect(x, y, width, height);
            }
    
            function drawPlayer() {
                drawRectangle(player.x, player.y, player.width, player.height, player.color);
            }
    
            function drawPlatforms() {
                platforms.forEach(platform => {
                    drawRectangle(platform.x, platform.y, platform.width, platform.height, platform.color);
                });
            }
    
            function drawObstacles() {
                obstacles.forEach(obstacle => {
                    drawRectangle(obstacle.x, obstacle.y, obstacle.width, obstacle.height, obstacle.color);
                });
            }
    
            function drawCollectibles() {
                collectibles.forEach(collectible => {
                    drawRectangle(collectible.x, collectible.y, collectible.width, collectible.height, collectible.color);
                });
            }
    
            function checkCollision(rect1, rect2) {
                return rect1.x < rect2.x + rect2.width &&
                       rect1.x + rect1.width > rect2.x &&
                       rect1.y < rect2.y + rect2.height &&
                       rect1.y + rect1.height > rect2.y;
            }
    
            function updatePlayer() {
                // Apply gravity
                if (!player.grounded) {
                    player.velY += gravity;
                } else {
                    player.velY = 0;
                }
                
                // Move player
                if (keys.ArrowLeft.pressed && player.x - player.speed > 0) {
                    player.x -= player.speed;
                }
                if (keys.ArrowRight.pressed && player.x + player.speed + player.width < canvas.width) {
                    player.x += player.speed;
                }
                if (keys.ArrowUp.pressed && player.grounded) {
                    player.velY = -15;
                    player.jumping = true;
                    player.grounded = false;
                }
    
                player.y += player.velY;
    
                // Check for platform collision
                player.grounded = false;
                platforms.forEach(platform => {
                    if (checkCollision(player, platform)) {
                        player.grounded = true;
                        player.y = platform.y - player.height;
                    }
                });
                
                // Check for obstacle collision
                obstacles.forEach(obstacle => {
                    if (checkCollision(player, obstacle)) {
                        alert("Game Over! Your score: " + score);
                        location.reload();
                    }
                });
                
                // Check for collectible collision
                for (let i = 0; i < collectibles.length; i++) {
                    if (checkCollision(player, collectibles[i])) {
                        score += 10;
                        collectibles.splice(i, 1);
                        break;
                    }
                }
            }
    
            function animate() {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                drawPlatforms();
                drawObstacles();
                drawCollectibles();
                drawPlayer();
                updatePlayer();
                
                // Display score
                ctx.fillStyle = 'black';
                ctx.font = '20px Arial';
                ctx.fillText(`Score: ${score}`, 10, 30);
    
                gameFrame++;
                requestAnimationFrame(animate);
            }
    
            // Key event listeners
            document.addEventListener('keydown', ({key}) => {
                switch (key) {
                    case 'ArrowUp':
                        keys.ArrowUp.pressed = true;
                        break;
                    case 'ArrowDown':
                        keys.ArrowDown.pressed = true;
                        break;
                    case 'ArrowLeft':
                        keys.ArrowRight.pressed = false;
                        keys.ArrowLeft.pressed = true;
                        break;
                    case 'ArrowRight':
                        keys.ArrowLeft.pressed = false;
                        keys.ArrowRight.pressed = true;
                        break;
                }
            });
    
            document.addEventListener('keyup', ({key}) => {
                switch (key) {
                    case 'ArrowUp':
                        keys.ArrowUp.pressed = false;
                        break;
                    case 'ArrowDown':
                        keys.ArrowDown.pressed = false;
                        break;
                    case 'ArrowLeft':
                        keys.ArrowLeft.pressed = false;
                        break;
                    case 'ArrowRight':
                        keys.ArrowRight.pressed = false;
                        break;
                }
            });
    
            animate();
        </script>
    </body>
    </html>
    

    How to Play

    1. Open the HTML file in a web browser.
    2. Use the arrow keys to move the player:
      • Up Arrow to jump.
      • Left Arrow to move left.
      • Right Arrow to move right.
    3. Avoid the black obstacles.
    4. Collect the yellow squares to increase your score.
    5. If you touch an obstacle the game will end, and the page will reload.

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