Every project / Games

🐤 Flappy Square

Tap to flap through the pipes. Gravity is not your friend.

Flappy Square running
Language
HTML, CSS & JavaScript
Level
Beginner
Category
Games
▶ Try it & remix

What you'll learn

  • Gravity & velocity
  • Spawning objects
  • Collision boxes
  • Click & key input
  • High scores

How it works

  • Every frame, gravity adds to the bird's falling speed. A flap sets the speed to a negative number, which means up.
  • A new pipe appears every 95 frames at a random height and slides left. Pipes that leave the screen are removed.
  • Each frame checks the bird's box against every pipe and the ground. Touching one ends the run.
  • Getting past a pipe scores a point. Your best score is saved with localStorage.

Knobs to turn

These settings are near the top of the code. Remix it, change one, and watch what happens. The preview updates as you type.

SettingStarts atFileWhat it does
GRAVITY0.45game.jshow fast you fall
FLAP-7.5game.jshow hard a flap pushes you up
PIPE_SPEED2.4game.jshow fast pipes slide left
PIPE_GAP150game.jsspace to fly through
PIPE_EVERY95game.jsframes between new pipes
GROUND40game.jsheight of the ground

Files

FileLines
index.html13the page: what shows up
game.js150what it does
style.css16how it looks

Challenges

Each one is a bit harder than the last. Teachers: these make good assignment instructions.

  1. Change GRAVITY and FLAP and see how the game feels.
  2. Make the gaps between pipes get smaller as your score goes up.
  3. Draw the bird as a circle with an eye instead of a square.
  4. Add coins between some pipes that give bonus points.
  5. Add a day/night cycle: fade the sky color every 10 points.