I'll help you create a simple Flappy Bird remake using HTML5 Canvas and JavaScript that works on mobile devices. Here's the code:
html Copy code
<!DOCTYPE html>
<html>
<head>
<title>Flappy Bird</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #333;
touch-action: none;
}
canvas {
border: 2px solid black;
max-width: 100%;
max-height: 100vh;
background: #70c5ce;
}
</style>
</head>
<body>
<canvas id="game"></canvas>
<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
canvas.width = 320;
canvas.height = 480;
const bird = {
x: 50,
y: canvas.height/2,
velocity: 0,
gravity: 0.5,
jump: -8,
size: 20
};
const pipes = [];
const pipeWidth = 50;
const pipeGap = 150;
const pipeSpacing = 200;
let score = 0;
let gameOver = false;
function createPipe() {
const minHeight = 50;
const maxHeight = canvas.height - pipeGap - minHeight;
const height = Math.floor(Math.random() * (maxHeight - minHeight)) + minHeight;
pipes.push({
x: canvas.width,
topHeight: height,
passed: false
});
}
function drawBird() {
ctx.fillStyle = '#f4dc26';
ctx.beginPath();
ctx.arc(bird.x, bird.y, bird.size, 0, Math.PI * 2);
ctx.fill();
}
function drawPipes() {
ctx.fillStyle = '#2ecc71';
pipes.forEach(pipe => {
// Top pipe
ctx.fillRect(pipe.x, 0, pipeWidth, pipe.topHeight);
// Bottom pipe
ctx.fillRect(pipe.x, pipe.topHeight + pipeGap, pipeWidth,
canvas.height - (pipe.topHeight + pipeGap));
});
}
function drawScore() {
ctx.fillStyle = 'white';
ctx.font = '24px Arial';
ctx.fillText(`Score: ${score}`, 10, 30);
}
function checkCollision(pipe) {
const birdRight = bird.x + bird.size;
const birdLeft = bird.x - bird.size;
const birdTop = bird.y - bird.size;
const birdBottom = bird.y + bird.size;
return (
birdRight > pipe.x &&
birdLeft < pipe.x + pipeWidth &&
(birdTop < pipe.topHeight ||
birdBottom > pipe.topHeight + pipeGap)
);
}
function update() {
if (gameOver) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'white';
ctx.font = '48px Arial';
ctx.fillText('Game Over!', canvas.width/2 - 100, canvas.height/2);
ctx.font = '24px Arial';
ctx.fillText('Tap to restart', canvas.width/2 - 60, canvas.height/2 + 40);
return;
}
bird.velocity += bird.gravity;
bird.y += bird.velocity;
if (bird.y + bird.size > canvas.height || bird.y - bird.size < 0) {
gameOver = true;
}
if (pipes.length === 0 ||
canvas.width - pipes[pipes.length-1].x >= pipeSpacing) {
createPipe();
}
pipes.forEach(pipe => {
pipe.x -= 2;
if (!pipe.passed && pipe.x + pipeWidth < bird.x) {
pipe.passed = true;
score++;
}
if (checkCollision(pipe)) {
gameOver = true;
}
});
pipes.splice(0, pipes.findIndex(pipe => pipe.x + pipeWidth >= 0));
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPipes();
drawBird();
drawScore();
requestAnimationFrame(update);
}
function jump() {
if (gameOver) {
bird.y = canvas.height/2;
bird.velocity = 0;
pipes.length = 0;
score = 0;
gameOver = false;
update();
return;
}
bird.velocity = bird.jump;
}
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
jump();
});
canvas.addEventListener('click', jump);
update();
</script>
</body>
</html>
You can save this code as an HTML file and open it in a mobile browser to play. Would you like me to explain any particular part of the code in more detail?