Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

make fully functional angry bird game. use shape instead of png. give me full html code,…

  1. User

    make fully functional angry bird game. use shape instead of png. give me full html code, all in one big html

  2. Assistant

    Sure! Below is the full HTML code for a simple Angry Birds-style game implemented using HTML5 Canvas and JavaScript. The game uses shapes instead of PNG images. You can copy and paste this code into a single HTML file and run it in your browser.

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Angry Birds Clone with Shapes</title>
        <style>
            body {
                margin: 0;
                overflow: hidden;
            }
            #gameCanvas {
                background-color: #87CEEB; /* Sky blue background */
                display: block;
            }
        </style>
    </head>
    <body>
    <canvas id="gameCanvas"></canvas>
    
    <script>
    // Get the canvas and context
    const canvas = document.getElementById('gameCanvas');
    const context = canvas.getContext('2d');
    
    // Set canvas dimensions
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    
    // Game variables
    let isMouseDown = false;
    let slingX = 150; // Slingshot position
    let slingY = canvas.height - 150;
    let bird = {
        x: slingX,
        y: slingY,
        radius: 20,
        color: 'red',
        vx: 0,
        vy: 0,
        isReleased: false,
    };
    
    let targets = []; // Array to hold target blocks
    
    // Create targets
    function createTargets() {
        let startX = canvas.width - 300;
        let startY = canvas.height - 200;
        for (let i = 0; i < 3; i++) {
            targets.push({
                x: startX + i * 60,
                y: startY,
                width: 50,
                height: 50,
                color: 'green',
                hit: false,
            });
        }
    }
    
    // Initialize targets
    createTargets();
    
    // Mouse events for slingshot interaction
    canvas.addEventListener('mousedown', function(e) {
        let rect = canvas.getBoundingClientRect();
        let mousePos = {
            x: e.clientX - rect.left,
            y: e.clientY - rect.top,
        };
        let dx = mousePos.x - bird.x;
        let dy = mousePos.y - bird.y;
        if (Math.sqrt(dx * dx + dy * dy) < bird.radius) {
            isMouseDown = true;
        }
    });
    
    canvas.addEventListener('mousemove', function(e) {
        if (isMouseDown && !bird.isReleased) {
            let rect = canvas.getBoundingClientRect();
            let mousePos = {
                x: e.clientX - rect.left,
                y: e.clientY - rect.top,
            };
            bird.x = mousePos.x;
            bird.y = mousePos.y;
        }
    });
    
    canvas.addEventListener('mouseup', function(e) {
        if (isMouseDown && !bird.isReleased) {
            bird.vx = (slingX - bird.x) / 10;
            bird.vy = (slingY - bird.y) / 10;
            bird.isReleased = true;
        }
        isMouseDown = false;
    });
    
    // Game loop
    function gameLoop() {
        requestAnimationFrame(gameLoop);
        update();
        draw();
    }
    
    // Update game state
    function update() {
        if (bird.isReleased) {
            bird.vy += 0.5; // Gravity
            bird.x += bird.vx;
            bird.y += bird.vy;
        }
    
        // Check collisions with ground
        if (bird.y + bird.radius > canvas.height) {
            bird.y = canvas.height - bird.radius;
            bird.vy *= -0.5; // Bounce effect
            bird.vx *= 0.5;
        }
    
        // Check collisions with targets
        targets.forEach(function(target) {
            if (!target.hit && bird.x + bird.radius > target.x && bird.x - bird.radius < target.x + target.width
                && bird.y + bird.radius > target.y && bird.y - bird.radius < target.y + target.height) {
                target.hit = true;
            }
        });
    }
    
    // Draw everything
    function draw() {
        // Clear canvas
        context.clearRect(0, 0, canvas.width, canvas.height);
    
        // Draw slingshot
        drawSlingshot();
    
        // Draw bird
        context.beginPath();
        context.arc(bird.x, bird.y, bird.radius, 0, Math.PI * 2);
        context.fillStyle = bird.color;
        context.fill();
        context.closePath();
    
        // Draw targets
        targets.forEach(function(target) {
            if (!target.hit) {
                context.fillStyle = target.color;
                context.fillRect(target.x, target.y, target.width, target.height);
            }
        });
    }
    
    // Draw slingshot
    function drawSlingshot() {
        // Base
        context.fillStyle = 'saddlebrown';
        context.fillRect(slingX - 10, slingY, 20, 50);
    
        // Band
        if (isMouseDown && !bird.isReleased) {
            context.beginPath();
            context.moveTo(slingX, slingY);
            context.lineTo(bird.x, bird.y);
            context.strokeStyle = 'black';
            context.lineWidth = 5;
            context.stroke();
            context.closePath();
        }
    }
    
    // Start the game loop
    gameLoop();
    
    // Adjust canvas size on window resize
    window.addEventListener('resize', function() {
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        slingY = canvas.height - 150;
    });
    
    </script>
    </body>
    </html>
    

    Explanation:

    • Canvas Setup: The <canvas> element is used to create a drawing area on the web page. The canvas dimensions are set to the full window size.
    • Game Elements:
      • Bird: Represented as a red circle that can be dragged and released. Its movement is affected by gravity.
      • Slingshot: A simple rectangle and line to simulate the slingshot mechanism.
      • Targets: Green rectangles positioned on the right side of the screen for the bird to hit.
    • Interaction:
      • Mouse Events: mousedown, mousemove, and mouseup events allow the player to control the bird with the mouse.
      • Physics: Basic gravity and collision detection are implemented to simulate the bird's flight and interaction with the targets and ground.
    • Game Loop: The gameLoop function updates the game state and renders the elements on each frame using requestAnimationFrame.

    How to Run the Game:

    1. Copy the entire code above into a new text file.
    2. Save the file with an .html extension, for example, angry_birds_clone.html.
    3. Open the HTML file in a modern web browser (like Chrome, Firefox, or Edge).
    4. Click and drag the red bird backward from the slingshot and release to launch it towards the green targets.

    Note: This is a simplified version of the Angry Birds game to demonstrate basic concepts using shapes. For a more advanced implementation, you'd need to incorporate a physics engine like Matter.js and add more detailed interactions and animations.

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