Write a flappy bird game in HTML Canvas API with JavaScript. Give me the full code in a…
- User
Write a flappy bird game in HTML Canvas API with JavaScript. Give me the full code in a single code block.
- Assistant
Certainly! Below is the complete code for a simple Flappy Bird game implemented using the HTML Canvas API and JavaScript. This code creates a playable game where the player controls a bird that must navigate through gaps between moving pipes. The game includes basic physics for gravity and collision detection.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <meta content="width=device-width, initial-scale=1.0" name="viewport"/> <title>Flappy Bird</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #70c5ce; } canvas { border: 2px solid #000; } </style> </head> <body> <canvas height="480" id="gameCanvas" width="320"></canvas> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); // Game variables const bird = { x: 50, y: 150, width: 20, height: 20, gravity: 0.6, lift: -15, velocity: 0 }; const pipes = []; const pipeWidth = 50; const pipeGap = 100; const pipeSpeed = 2; let frame = 0; let score = 0; let gameOver = false; // Load images const birdImg = new Image(); birdImg.src = 'https://i.imgur.com/4NZ6uLY.png'; const pipeTopImg = new Image(); pipeTopImg.src = 'https://i.imgur.com/5x4vL3T.png'; const pipeBottomImg = new Image(); pipeBottomImg.src = 'https://i.imgur.com/5x4vL3T.png'; // Event listener for bird jump document.addEventListener('keydown', (e) => { if (e.code === 'Space') { bird.velocity = bird.lift; } }); // Game loop function update() { if (gameOver) { ctx.fillStyle = 'black'; ctx.font = '30px Arial'; ctx.fillText('Game Over', canvas.width / 2 - 70, canvas.height / 2); ctx.font = '20px Arial'; ctx.fillText('Score: ' + score, canvas.width / 2 - 30, canvas.height / 2 + 30); return; } // Clear canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw bird ctx.drawImage(birdImg, bird.x, bird.y, bird.width, bird.height); // Bird physics bird.velocity += bird.gravity; bird.y += bird.velocity; // Prevent bird from going off-screen if (bird.y + bird.height > canvas.height) { bird.y = canvas.height - bird.height; bird.velocity = 0; gameOver = true; } if (bird.y < 0) { bird.y = 0; bird.velocity = 0; } // Generate pipes if (frame % 90 === 0) { const pipeY = Math.floor(Math.random() * (canvas.height - pipeGap - 20)) + 10; pipes.push({ x: canvas.width, y: pipeY, width: pipeWidth, height: canvas.height - pipeY }); } // Move and draw pipes for (let i = pipes.length - 1; i >= 0; i--) { pipes[i].x -= pipeSpeed; // Draw top pipe ctx.drawImage(pipeTopImg, pipes[i].x, 0, pipes[i].width, pipes[i].y); // Draw bottom pipe ctx.drawImage(pipeBottomImg, pipes[i].x, pipes[i].y + pipeGap, pipes[i].width, pipes[i].height - pipeGap); // Check for collision if ( bird.x < pipes[i].x + pipes[i].width && bird.x + bird.width > pipes[i].x && (bird.y < pipes[i].y || bird.y + bird.height > pipes[i].y + pipeGap) ) { gameOver = true; } // Remove off-screen pipes and update score if (pipes[i].x + pipes[i].width < 0) { pipes.splice(i, 1); score++; } } // Draw score ctx.fillStyle = 'black'; ctx.font = '20px Arial'; ctx.fillText('Score: ' + score, 10, 20); frame++; requestAnimationFrame(update); } // Start the game update(); </script> </body> </html>Instructions:
-
Save the Code: Copy the entire code block above and save it as an HTML file, for example,
flappy_bird.html. -
Open in Browser: Open the saved HTML file in your web browser. The game should load and be ready to play.
-
Gameplay:
- Press the Spacebar to make the bird jump.
- Navigate the bird through the gaps between the pipes.
- The game ends if the bird collides with a pipe or the ground.
Notes:
-
The game uses external images for the bird and pipes. Ensure you have an active internet connection to load these images. If you prefer to use local images, download the images from the provided URLs and update the
srcattributes in the code to point to your local files. -
This implementation is a basic version of Flappy Bird. You can enhance it by adding features such as sound effects, improved graphics, and a restart functionality after the game ends.
Enjoy your game!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
sonar-reasoning-pro-highCompare side by side →
sonar-reasoningCompare side by side →
gemini-2.5-pro-exp-03-25-groundingCompare side by side →
sonar-reasoning-pro-highCompare side by side →
gemini-2.5-pro-exp-03-25-groundingCompare side by side →
sonar-reasoning-pro-highCompare side by side →
sonar-pro-highCompare side by side →