o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Write an HTML code to build a website which has a Flappy Bird game. The game should be…
- User
Write an HTML code to build a website which has a Flappy Bird game. The game should be playable both on the desktop version of web browser and the mobile version of web browser.
- Assistant
Certainly! Here's an HTML code that creates a simple Flappy Bird game playable on both desktop and mobile browsers. The game uses HTML5 Canvas and JavaScript for the game logic and rendering.
<!DOCTYPE html> <html> <head> <title>Flappy Bird Game</title> <style> /* CSS Styles */ body { margin: 0; padding: 0; overflow: hidden; background-color: #70c5ce; /* Background sky color */ } canvas { display: block; margin: 0 auto; background-color: #70c5ce; /* Match the background */ } </style> </head> <body> <canvas id="gameCanvas" width="400" height="600"></canvas> <script> // JavaScript code // Get canvas and context const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); let frames = 0; // Bird object const bird = { x: 50, y: 150, width: 34, height: 26, gravity: 0.25, lift: 4.6, velocity: 0, draw: function() { ctx.fillStyle = '#FF0'; ctx.fillRect(this.x, this.y, this.width, this.height); }, flap: function() { this.velocity = -this.lift; }, update: function() { this.velocity += this.gravity; this.y += this.velocity; } }; // Pipes object const pipes = { position: [], width: 52, height: canvas.height, gap: 120, minHeight: 50, maxHeight: canvas.height - 200, speed: 2, draw: function() { ctx.fillStyle = '#0F0'; for (let i = 0; i < this.position.length; i++) { let p = this.position[i]; // Upper pipe ctx.fillRect(p.x, p.y, this.width, p.height); // Lower pipe ctx.fillRect(p.x, p.y + p.height + this.gap, this.width, canvas.height - (p.y + p.height + this.gap)); } }, update: function() { if (frames % 150 === 0) { // Randomize pipe height let pipeHeight = Math.floor(Math.random() * (this.maxHeight - this.minHeight + 1) + this.minHeight); this.position.push({ x: canvas.width, y: 0, height: pipeHeight }); } for (let i = 0; i < this.position.length; i++) { let p = this.position[i]; p.x -= this.speed; // If the pipe is off the screen, remove it if (p.x + this.width <= 0) { this.position.shift(); } // Collision detection // Upper pipe if (bird.x < p.x + this.width && bird.x + bird.width > p.x && bird.y < p.y + p.height) { // Game over resetGame(); } // Lower pipe if (bird.x < p.x + this.width && bird.x + bird.width > p.x && bird.y + bird.height > p.y + p.height + this.gap) { // Game over resetGame(); } } } }; // Draw function function draw() { ctx.fillStyle = '#70c5ce'; // Sky color ctx.fillRect(0, 0, canvas.width, canvas.height); bird.draw(); pipes.draw(); } // Update function function update() { bird.update(); pipes.update(); // Ground collision if (bird.y + bird.height >= canvas.height || bird.y <= 0) { resetGame(); } } // Game loop function loop() { update(); draw(); frames++; requestAnimationFrame(loop); } // Start the game loop(); // Event listeners for controls // Desktop: spacebar or mouse click document.addEventListener('keydown', function(e) { if (e.code === 'Space') { bird.flap(); } }); document.addEventListener('click', function() { bird.flap(); }); // Mobile: touch document.addEventListener('touchstart', function() { bird.flap(); }); // Reset game function function resetGame() { // Simple reset by reloading the page alert('Game Over!'); document.location.reload(); } </script> </body> </html>How to Use the Code:
- Copy and Paste: Copy the entire code above and paste it into a new text file.
- Save the File: Save the file with an
.htmlextension, for example,flappy_bird.html. - Open in Browser: Open the saved HTML file in a web browser (Chrome, Firefox, Safari, etc.).
Controls:
- Desktop:
- Press the Spacebar or click with the mouse to make the bird flap.
- Mobile:
- Tap anywhere on the screen to make the bird flap.
Game Mechanics:
- The bird will constantly fall due to gravity.
- Flapping causes the bird to move upward.
- Pipes will appear from the right side of the screen at random heights.
- Avoid colliding with the pipes and the edges of the canvas.
Customizations:
- Pipe Gap: Adjust
gapin thepipesobject to change the space between pipes. - Gravity and Lift: Modify
gravityandliftin thebirdobject to change the game's difficulty. - Pipe Speed: Change
speedin thepipesobject for faster or slower pipe movement.
Note: This is a simplified version of the Flappy Bird game intended for educational purposes. It uses basic shapes instead of images for simplicity. For a more advanced version, you can replace the shapes with images and add scoring.
Feel free to customize and enhance the game to your liking. Enjoy playing!