DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Flappy Bird Game with JavaScript

Flappy Bird is the one-button game that defined mobile gaming: tap to flap, dodge the pipes, and try not to rage-quit. It is a great beginner project because it teaches gravity, collision detection, and a requestAnimationFrame loop — in about 100 lines.

The bird has a vertical position and a velocity. Gravity pulls it down every frame; a tap sets the velocity upward. That is the whole physics model:

const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
let bird = { y: 200, velocity: 0 };
const GRAVITY = 0.5;
const FLAP = -9;

function updateBird() {
  bird.velocity += GRAVITY;
  bird.y += bird.velocity;
}

document.addEventListener("keydown", (e) => {
  if (e.code === "Space") bird.velocity = FLAP;
});
canvas.addEventListener("click", () => (bird.velocity = FLAP));
Enter fullscreen mode Exit fullscreen mode

Pipes spawn on the right and move left. Each pipe pair has a gap at a random height, and the gap is the only safe passage. A new pair appears every 90 frames:

let pipes = [];
let frame = 0;

function spawnPipes() {
  const gapY = 80 + Math.random() * 200;
  pipes.push({ x: canvas.width, gapY, gap: 120, passed: false });
}

function updatePipes() {
  frame++;
  if (frame % 90 === 0) spawnPipes();
  pipes.forEach((pipe) => (pipe.x -= 2));
  pipes = pipes.filter((pipe) => pipe.x > -60);
}
Enter fullscreen mode Exit fullscreen mode

Collision is two checks: the bird hits the ground or the ceiling, or it overlaps a pipe outside the gap. The score goes up each time a pipe passes the bird's x position:

function checkCollision() {
  if (bird.y < 0 || bird.y > canvas.height - 20) return true;
  return pipes.some((pipe) => {
    const inPipeX = 40 > pipe.x - 30 && 40 < pipe.x + 60;
    if (!inPipeX) return false;
    return bird.y < pipe.gapY || bird.y > pipe.gapY + pipe.gap;
  });
}

function updateScore() {
  pipes.forEach((pipe) => {
    if (!pipe.passed && pipe.x + 60 < 40) {
      pipe.passed = true;
      score++;
    }
  });
}
Enter fullscreen mode Exit fullscreen mode

The game loop ties it together: update the bird, move the pipes, check collisions, and draw everything — sky, pipes, bird, and score — every frame:

function loop() {
  updateBird();
  updatePipes();
  updateScore();
  if (checkCollision()) {
    alert(`Game over! Score: ${score}`);
    document.location.reload();
    return;
  }
  draw();
  requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
Enter fullscreen mode Exit fullscreen mode

Two tuning details make it feel like the real game. First, the flap strength and gravity need to balance so the bird rises about one pipe-gap per tap — too strong and the game is trivial, too weak and it is impossible. Second, cap the fall speed so the bird does not accelerate into an uncontrollable dive.

Ways to extend it: add a medal system (bronze at 10, silver at 20, gold at 30), add day/night backgrounds that change with the score, add a best-score display from localStorage, or add a gentle flap animation that tilts the bird up when rising and down when falling. The gravity-plus-obstacle loop is the same one behind endless runners and dodging games, so it transfers directly.

Top comments (0)