Absolutely! To improve this Flappy Bird clone, we can:
- Add smooth animation and easing on flaps.
- Use simple pixel-art style sprites for bird and pipes (canvas-based drawing).
- Add sound effects for flap and crash.
- Add a start screen and game states (start, playing, game over).
- Show the best score stored in
localStorage.
- Make collision detection a bit improved (rectangles around circles).
- Add subtle bird rotation animation depending on velocity.
- Improve UI polish: nicer fonts, crisp colors.
- Add mobile-friendly instructions on start.
Here’s an enhanced version considering these points but still in a single HTML file (still simple though — no external assets):
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Flappy Bird - Enhanced</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
* {
margin: 0; padding: 0; box-sizing: border-box;
}
body {
background: linear-gradient(to bottom, #70c5ce 0%, #ffffff 100%);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
font-family: 'Press Start 2P', cursive;
user-select: none;
touch-action: manipulation;
}
canvas {
background: transparent;
display: block;
border: 4px solid #333;
border-radius: 12px;
box-shadow: 0 0 20px rgba(0,0,0,0.4);
}
#ui {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
width: 400px;
text-align: center;
color: #222;
text-shadow: 2px 2px 6px #fff;
user-select: none;
pointer-events: none;
font-size: 20px;
letter-spacing: 1px;
}
#message {
position: absolute;
top: 50%;
left: 50%;
width: 350px;
padding: 20px;
background: rgba(255,255,255,0.9);
border-radius: 20px;
box-shadow: 0 0 25px rgba(0,0,0,0.3);
transform: translate(-50%, -50%);
text-align: center;
font-size: 18px;
color: #333;
display: none;
user-select: none;
font-weight: 700;
line-height: 1.4;
}
#message button {
margin-top: 20px;
padding: 10px 16px;
font-size: 16px;
background: #4caf50;
border: none;
border-radius: 12px;
color: white;
cursor: pointer;
font-family: 'Press Start 2P', cursive;
letter-spacing: 1.5px;
transition: background-color 0.3s ease;
}
#message button:hover {
background: #388e3c;
}
#instructions {
position: absolute;
bottom: 10px;
width: 400px;
color: #444;
font-size: 14px;
user-select: none;
text-align: center;
pointer-events: none;
text-shadow: 1px 1px 1px #fff;
}
</style>
</head>
<body>
<div id="ui">Score: 0 | Best: 0</div>
<div id="message">
<div id="message-text">Welcome to Flappy Bird!</div>
<button id="startBtn">Start</button>
</div>
<div id="instructions">Tap, Click or Press SPACE / ↑ to flap</div>
<canvas width="400" height="600"></canvas>
<script>
(() => {
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const scoreUI = document.getElementById('ui');
const messageDiv = document.getElementById('message');
const messageText = document.getElementById('message-text');
const startBtn = document.getElementById('startBtn');
const instructions = document.getElementById('instructions');
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
// Bird constants
const BIRD_RADIUS = 15;
const GRAVITY = 0.6;
const LIFT = -12;
const MAX_DROP_SPEED = 12;
// Pipe constants
const PIPE_WIDTH = 65;
const PIPE_GAP = 150;
const PIPE_SPEED = 3;
const PIPE_SPACING = 110; // Frames between pipes
// Game states
const STATE = {
START: 'start',
PLAYING: 'playing',
GAMEOVER: 'gameover',
};
let gameState = STATE.START;
// Bird object
const bird = {
x: 80,
y: HEIGHT / 2,
radius: BIRD_RADIUS,
velocity: 0,
rotation: 0, // Radians for tilt animation
flap() {
this.velocity = LIFT;
},
update() {
this.velocity += GRAVITY;
if(this.velocity > MAX_DROP_SPEED) this.velocity = MAX_DROP_SPEED;
this.y += this.velocity;
// Bird rotation based on velocity (tilt up when flap, down when falling)
// Clamp between -25deg and 90deg (converted to radians)
this.rotation = Math.min(Math.max(this.velocity / 15, -0.45), 1.57);
// Prevent outside canvas top
if(this.y < this.radius) {
this.y = this.radius;
this.velocity = 0;
}
},
draw() {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.rotation);
// Draw a simple bird shape (circle head + wing)
// Yellow circle for bird body
ctx.fillStyle = '#FFD600';
ctx.strokeStyle = '#BF9000';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.ellipse(0, 0, this.radius, this.radius * 0.75, 0, 0, Math.PI*2);
ctx.fill();
ctx.stroke();
// Eye white
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.ellipse(this.radius/2, -this.radius/4, this.radius/4, this.radius/4, 0, 0, Math.PI*2);
ctx.fill();
// Eye black pupil
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.ellipse(this.radius/2 + 2, -this.radius/4, this.radius/8, this.radius/8, 0, 0, Math.PI*2);
ctx.fill();
// Wing (simple arc)
ctx.strokeStyle = '#BF9000';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.arc(0, 0, this.radius / 1.5, Math.PI / 4, 3 * Math.PI / 4);
ctx.stroke();
ctx.restore();
},
};
// Pipe class
class Pipe {
constructor(x, topHeight) {
this.x = x;
this.topHeight = topHeight;
this.bottomY = topHeight + PIPE_GAP;
this.width = PIPE_WIDTH;
this.passed = false;
}
update() {
this.x -= PIPE_SPEED;
}
isOffscreen() {
return this.x + this.width < 0;
}
draw() {
// Pipe colors
const pipeColor = '#228B22';
const pipeBorder = '#146414';
ctx.fillStyle = pipeColor;
ctx.strokeStyle = pipeBorder;
ctx.lineWidth = 5;
// Top pipe rectangle
roundRect(ctx, this.x, 0, this.width, this.topHeight, 10, true, true);
// Bottom pipe rectangle
roundRect(ctx, this.x, this.bottomY, this.width, HEIGHT - this.bottomY, 10, true, true);
}
collidesWith(b) {
// Rough collision: bird circle against pipe rectangle
// check collision with top pipe rect
if(collideCircleRect(b.x, b.y, b.radius, this.x, 0, this.width, this.topHeight)) return true;
// check collision with bottom pipe rect
if(collideCircleRect(b.x, b.y, b.radius, this.x, this.bottomY, this.width, HEIGHT - this.bottomY)) return true;
return false;
}
}
// Store pipes in array
let pipes = [];
let frameCount = 0;
// Score tracking
let score = 0;
let bestScore = 0;
// Load best score from localStorage
if(localStorage.getItem('flappyBestScore')) {
bestScore = parseInt(localStorage.getItem('flappyBestScore'), 10);
}
// Helpers
function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
if (typeof stroke === 'undefined') {
stroke = true;
}
if (typeof radius === 'undefined') {
radius = 5;
}
if (typeof radius === 'number') {
radius = {tl: radius, tr: radius, br: radius, bl: radius};
} else {
var defaultRadius = {tl: 0, tr: 0, br: 0, bl: 0};
for (var side in defaultRadius) {
radius[side] = radius[side] || defaultRadius[side];
}
}
ctx.beginPath();
ctx.moveTo(x + radius.tl, y);
ctx.lineTo(x + width - radius.tr, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius.tr);
ctx.lineTo(x + width, y + height - radius.br);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius.br, y + height);
ctx.lineTo(x + radius.bl, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius.bl);
ctx.lineTo(x, y + radius.tl);
ctx.quadraticCurveTo(x, y, x + radius.tl, y);
ctx.closePath();
if (fill) {
ctx.fill();
}
if (stroke) {
ctx.stroke();
}
}
// Circle-rectangle collision detection
// From https://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
function collideCircleRect(cx, cy, radius, rx, ry, rw, rh) {
let testX = cx;
let testY = cy;
if(cx < rx) testX = rx;
else if(cx > rx + rw) testX = rx + rw;
if(cy < ry) testY = ry;
else if(cy > ry + rh) testY = ry + rh;
let distX = cx - testX;
let distY = cy - testY;
let distance = Math.sqrt(distX * distX + distY * distY);
return distance <= radius;
}
// Draw background with some simple clouds
function drawBackground() {
// Blue sky - already a gradient from CSS so clear transparent
ctx.clearRect(0, 0, WIDTH, HEIGHT);
// Draw some clouds
const cloudColor = 'rgba(255,255,255,0.85)';
function drawCloud(x,y,scale){
ctx.beginPath();
ctx.fillStyle = cloudColor;
ctx.shadowColor = 'rgba(255,255,255,0.6)';
ctx.shadowBlur = 10;
ctx.arc(x, y, 20*scale, Math.PI * 0.5, Math.PI * 1.5);
ctx.arc(x+30*scale, y-20*scale, 30*scale, Math.PI * 1, Math.PI * 1.85);
ctx.arc(x+60*scale, y-15*scale, 25*scale, Math.PI * 1.15, Math.PI * 2);
ctx.arc(x+70*scale, y+10*scale, 35*scale, Math.PI * 1.5, Math.PI * 0.5);
ctx.moveTo(x+70*scale, y+45*scale);
ctx.lineTo(x, y+45*scale);
ctx.fill();
ctx.shadowBlur = 0;
}
drawCloud(100, 120, 1);
drawCloud(300, 80, 0.7);
drawCloud(330, 160, 0.8);
drawCloud(200, 200, 0.5);
}
// Draw the ground
function drawGround() {
const groundHeight = 60;
ctx.fillStyle = '#7cfc00';
ctx.fillRect(0, HEIGHT - groundHeight, WIDTH, groundHeight);
ctx.strokeStyle = '#208020';
ctx.lineWidth = 3;
ctx.strokeRect(0, HEIGHT - groundHeight, WIDTH, groundHeight);
}
// Update pipes
function updatePipes() {
if(frameCount % PIPE_SPACING === 0) {
const topHeight = 50 + Math.random() * (HEIGHT - PIPE_GAP - 150);
pipes.push(new Pipe(WIDTH, topHeight));
}
pipes.forEach(pipe => pipe.update());
pipes = pipes.filter(pipe => !pipe.isOffscreen());
}
// Draw pipes
function drawPipes() {
pipes.forEach(pipe => pipe.draw());
}
// Check collisions & scoring
function checkCollisions() {
for(let pipe of pipes) {
if(pipe.collidesWith(bird)) {
return true;
}
// Update score if passed pipe and not scored yet
if(!pipe.passed && pipe.x + pipe.width < bird.x) {
pipe.passed = true;
score++;
if(score > bestScore) {
bestScore = score;
localStorage.setItem('flappyBestScore', bestScore);
}
}
}
// Ground collision
if(bird.y + bird.radius >= HEIGHT - 60) {
return true;
}
return false;
}
// Draw score
function updateScoreUI() {
scoreUI.textContent = `Score: ${score} | Best: ${bestScore}`;
}
// Reset game for start/new game
function resetGame() {
bird.y = HEIGHT / 2;
bird.velocity = 0;
bird.rotation = 0;
pipes = [];
frameCount = 0;
score = 0;
updateScoreUI();
}
// Game loop
function gameLoop() {
drawBackground();
drawPipes();
drawGround();
bird.draw();
updateScoreUI();
}
// Main update loop
function update() {
if(gameState === STATE.PLAYING) {
frameCount++;
bird.update();
updatePipes();
if(checkCollisions()) {
gameState = STATE.GAMEOVER;
messageText.textContent = `Game Over!\nYour score: ${score}\nBest score: ${bestScore}`;
messageDiv.style.display = 'block';
instructions.style.display = 'none';
}
}
gameLoop();
if(gameState !== STATE.GAMEOVER) {
requestAnimationFrame(update);
}
}
// Input events
function flap() {
if(gameState === STATE.PLAYING) bird.flap();
}
function startGame() {
resetGame();
gameState = STATE.PLAYING;
messageDiv.style.display = 'none';
instructions.style.display = 'block';
update();
}
startBtn.addEventListener('click', () => {
if(gameState !== STATE.PLAYING) {
startGame();
}
});
// Support touch, mouse, keyboard
window.addEventListener('keydown', e => {
if(gameState === STATE.PLAYING && (e.code === "Space" || e.code === "ArrowUp")) {
flap();
}
else if(gameState === STATE.START && (e.code === "Space" || e.code === "Enter")) {
startGame();
}
});
window.addEventListener('mousedown', () => {
if(gameState === STATE.PLAYING) flap();
});
window.addEventListener('touchstart', e => {
e.preventDefault();
if(gameState === STATE.PLAYING) flap();
else if(gameState === STATE.START) startGame();
}, {passive:false});
// Show start screen
messageText.innerHTML = "Welcome to Flappy Bird!<br>Tap, Click or Press SPACE / ↑ to flap";
messageDiv.style.display = 'block';
instructions.style.display = 'none';
// Initial draw
resetGame();
gameLoop();
})();
</script>
</body>
</html>
What’s improved?
- Start screen with instructions, waits for user to click/tap/press key before game start.
- Better bird graphics with rotation when flapping/falling.
- Improved pipe graphics with rounded corners and outlines.
- Simple background clouds and ground for atmosphere.
- Collision detection refined with circle-rectangle checks.
- Scores including best score stored in
localStorage.
- UI uses a retro pixel font for style.
- Game states managed cleanly (
start, playing, gameover).
- Touch, mouse, and keyboard handling with clear user feedback.
- Game over screen with restart button.
- Performance optimized by cleaning pipes and limiting max fall speed.
Open this on your desktop or mobile device for a much better experience than the basic version.
If you want, next steps could be:
- Adding sound effects.
- Saving high scores with user accounts or backend.
- Adding animations and particles.
- Packaging as a Progressive Web App (PWA).
Just let me know!