Skip to content
TheChatArchiveThe Chat Archive
o1-mini-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

    Creating a fully functional Angry Birds-style game in a single HTML file is quite an ambitious task, especially considering the complexity involved in physics calculations, collision detection, and game state management. However, I can provide you with a simplified version that captures the essence of the game using HTML5 Canvas and JavaScript. This version will include:

    • A slingshot to launch the bird.
    • Basic physics for projectile motion.
    • Simple blocks as structures to knock down.
    • Basic collision detection.

    Please note that this is a simplified version and can be further enhanced with additional features like multiple levels, scoring, different types of blocks, etc.

    Here's the complete HTML code:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Simple Angry Birds Clone</title>
        <style>
            body {
                margin: 0;
                overflow: hidden;
                background-color: #87CEEB; /* Sky blue background */
            }
            canvas {
                display: block;
                background-color: #87CEEB;
            }
        </style>
    </head>
    <body>
    <canvas id="gameCanvas"></canvas>
    
    <script>
        // Get Canvas and Context
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
    
        // Set Canvas Size
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
    
        // Variables
        let isDragging = false;
        let mouse = { x: 0, y: 0 };
        let slingshot = { x: 150, y: canvas.height - 150 };
        let bird = { x: slingshot.x, y: slingshot.y, radius: 20, color: 'red', isLaunched: false, velocity: { x: 0, y: 0 } };
        const gravity = 0.5;
        const blocks = [];
        let ground = { x: 0, y: canvas.height - 50, width: canvas.width, height: 50 };
    
        // Create some blocks
        function createBlocks() {
            // Simple structure
            blocks.push({ x: 800, y: canvas.height - 100, width: 50, height: 50, color: 'brown', isHit: false });
            blocks.push({ x: 850, y: canvas.height - 100, width: 50, height: 50, color: 'brown', isHit: false });
            blocks.push({ x: 925, y: canvas.height - 150, width: 50, height: 100, color: 'brown', isHit: false });
        }
    
        createBlocks();
    
        // Mouse Events
        canvas.addEventListener('mousedown', (e) => {
            const rect = canvas.getBoundingClientRect();
            const dist = Math.hypot(e.clientX - rect.left - bird.x, e.clientY - rect.top - bird.y);
            if (dist <= bird.radius) {
                isDragging = true;
            }
        });
    
        canvas.addEventListener('mousemove', (e) => {
            if (isDragging) {
                const rect = canvas.getBoundingClientRect();
                mouse.x = e.clientX - rect.left;
                mouse.y = e.clientY - rect.top;
                bird.x = mouse.x;
                bird.y = mouse.y;
            }
        });
    
        canvas.addEventListener('mouseup', () => {
            if (isDragging) {
                isDragging = false;
                bird.isLaunched = true;
                const dx = slingshot.x - bird.x;
                const dy = slingshot.y - bird.y;
                const power = 0.2;
                bird.velocity.x = dx * power;
                bird.velocity.y = dy * power;
            }
        });
    
        // Collision Detection
        function checkCollision(circle, rect) {
            // Find the closest point to the circle within the rectangle
            let closestX = clamp(circle.x, rect.x, rect.x + rect.width);
            let closestY = clamp(circle.y, rect.y, rect.y + rect.height);
    
            // Calculate the distance between the circle's center and this closest point
            let distanceX = circle.x - closestX;
            let distanceY = circle.y - closestY;
    
            // If the distance is less than the circle's radius, there's a collision
            let distanceSquared = (distanceX * distanceX) + (distanceY * distanceY);
            return distanceSquared < (circle.radius * circle.radius);
        }
    
        function clamp(value, min, max) {
            return Math.max(min, Math.min(max, value));
        }
    
        // Update Function
        function update() {
            if (bird.isLaunched) {
                bird.velocity.y += gravity;
                bird.x += bird.velocity.x;
                bird.y += bird.velocity.y;
    
                // Check collision with ground
                if (bird.y + bird.radius > ground.y) {
                    bird.y = ground.y - bird.radius;
                    bird.velocity.y *= -0.5;
                    bird.velocity.x *= 0.5;
                }
    
                // Check collision with blocks
                blocks.forEach(block => {
                    if (!block.isHit && checkCollision(bird, block)) {
                        block.isHit = true;
                        bird.velocity.x *= -0.7;
                        bird.velocity.y *= -0.7;
                    }
                });
    
                // Reset if bird goes off-screen
                if (bird.x < -bird.radius || bird.x > canvas.width + bird.radius || bird.y < -bird.radius) {
                    resetBird();
                }
            }
        }
    
        // Reset Bird
        function resetBird() {
            bird.x = slingshot.x;
            bird.y = slingshot.y;
            bird.velocity.x = 0;
            bird.velocity.y = 0;
            bird.isLaunched = false;
        }
    
        // Draw Function
        function draw() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
    
            // Draw ground
            ctx.fillStyle = 'green';
            ctx.fillRect(ground.x, ground.y, ground.width, ground.height);
    
            // Draw slingshot
            ctx.beginPath();
            ctx.moveTo(slingshot.x - 20, slingshot.y + 20);
            ctx.lineTo(slingshot.x + 20, slingshot.y + 20);
            ctx.strokeStyle = 'sienna';
            ctx.lineWidth = 10;
            ctx.stroke();
    
            // Draw elastic band
            if (isDragging || bird.isLaunched) {
                ctx.beginPath();
                ctx.moveTo(slingshot.x - 20, slingshot.y + 20);
                ctx.lineTo(bird.x, bird.y);
                ctx.lineTo(slingshot.x + 20, slingshot.y + 20);
                ctx.strokeStyle = 'rgba(139,69,19,0.7)';
                ctx.lineWidth = 5;
                ctx.stroke();
            }
    
            // Draw bird
            ctx.beginPath();
            ctx.arc(bird.x, bird.y, bird.radius, 0, Math.PI * 2);
            ctx.fillStyle = bird.color;
            ctx.fill();
            ctx.strokeStyle = 'black';
            ctx.stroke();
    
            // Draw blocks
            blocks.forEach(block => {
                if (!block.isHit) {
                    ctx.fillStyle = block.color;
                    ctx.fillRect(block.x, block.y, block.width, block.height);
                    ctx.strokeStyle = 'black';
                    ctx.strokeRect(block.x, block.y, block.width, block.height);
                }
            });
        }
    
        // Game Loop
        function loop() {
            update();
            draw();
            requestAnimationFrame(loop);
        }
    
        loop();
    
        // Handle Window Resize
        window.addEventListener('resize', () => {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            ground.y = canvas.height - 50;
        });
    </script>
    </body>
    </html>
    

    Explanation of the Code

    1. HTML Structure

    • Canvas Element: The <canvas> element with the ID gameCanvas is where all the drawing will take place.
    • Styles: The body has no margin and hidden overflow to make the canvas occupy the full window. The canvas has a sky-blue background to mimic the Angry Birds sky.

    2. JavaScript Components

    • Canvas Setup: The canvas is set to fill the entire browser window.

    • Game Variables:

      • Slingshot: Positioned near the bottom-left of the screen.
      • Bird: Represents the projectile. It starts at the slingshot position and can be dragged and launched.
      • Gravity: A constant to simulate gravitational pull.
      • Blocks: An array of block objects that the bird can collide with and destroy.
      • Ground: A green rectangle representing the ground.
    • Creating Blocks: A simple structure is created using blocks positioned to form a basic structure.

    • Mouse Events:

      • mousedown: Checks if the bird is clicked and starts dragging.
      • mousemove: Updates the bird's position while dragging.
      • mouseup: Launches the bird by calculating the velocity based on the drag distance.
    • Collision Detection: A simple circle-rectangle collision detection function checks if the bird hits any of the blocks.

    • Update Function: Updates the bird's position based on its velocity and applies gravity. It also handles collisions with the ground and blocks. If the bird goes off-screen, it resets to the slingshot.

    • Draw Function: Clears the canvas and redraws all game elements:

      • Ground: A green rectangle.
      • Slingshot: A simple line to represent the slingshot.
      • Elastic Band: Drawn when dragging or after launching.
      • Bird: Drawn as a red circle.
      • Blocks: Drawn as brown rectangles if they haven't been hit.
    • Game Loop: Continuously updates and draws the game using requestAnimationFrame.

    • Window Resize Handling: Adjusts the canvas size and ground position when the window is resized.

    Enhancements and Further Development

    This is a basic implementation and can be enhanced in several ways:

    1. Multiple Levels: Create different levels with varying block arrangements.
    2. Scoring System: Add points for each block destroyed.
    3. Multiple Birds: Allow multiple birds per level.
    4. Different Block Types: Introduce blocks with different properties (e.g., wood, stone).
    5. Animations and Effects: Add animations when blocks are hit or destroyed.
    6. Sound Effects: Incorporate sounds for launching, collisions, and destruction.

    Running the Game

    1. Save the Code: Copy the above HTML code and save it in a file named index.html.
    2. Open in Browser: Open the index.html file in any modern web browser.
    3. Play:
      • Click and drag the red bird to stretch the slingshot.
      • Release the mouse button to launch the bird.
      • Aim to hit the brown blocks to knock them down.

    Feel free to modify and expand the game as per your requirements. Enjoy building your Angry Birds-inspired game!

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