Mini games

Flyer

One button, thread the gaps. The reachable band is derived from the physics, not guessed.

ArcadeEndless
0Score
0Personal best

Space or ↑ to flap · P to pause · tap anywhere on the field

Thread the gaps

Tap the field or press Space to flap. Gravity does the rest. One point per gap cleared.

You start with a moment of grace before the first obstacle.
Run over0

Gaps cleared.

Tap or press Space to fly again

Paused

The loop is stopped, not just hidden. Nothing is running while this panel is up.

Tap or press P to resume

One button, thread the gaps. Gravity pulls, a tap lifts. The gap positions are derived from the physics constants rather than guessed, so a reachable gap is always reachable, and the simulation runs on a fixed step so a 144Hz monitor does not make it easier.

How it works

The whole game is a few hundred lines of TypeScript drawing into one canvas. Most of those lines are ordinary. The four decisions below are the ones that decide whether it feels like a game or like a bug, so they are the ones worth writing down.

The physics does not run on the frame rate

The obvious way to write this loop is to take the delta between two requestAnimationFrame callbacks and integrate with it directly: vy += g * dt; y += vy * dt. That is semi-implicit Euler, and it is stable, but it is not the same simulation at every frame rate. Run the algebra and the closed form for a fall from rest with a fixed frame time h is y(t) = ½·g·t·(t + h). The h term is the frame rate leaking into the physics.

With this game's gravity of 1400 units per second squared, one second of falling covers 711.7 units at 60Hz and 704.9 units at 144Hz. That is a difference of 6.8 units in the first second alone, on a play field only 360 units tall, and it compounds: every flap arc is slightly flatter and every fall slightly shorter on the faster monitor. A gap that is tight on a laptop is comfortable on a 144Hz display, and the leaderboard stops comparing like with like. The same bug in the other direction is what made a generation of DOS games unplayable on faster hardware.

So the physics gets its own clock. Time from the frame is poured into an accumulator, and the accumulator is drained in fixed slices of 1/120 s. Every machine integrates exactly the same sequence of steps, and rendering only decides how many of them happen before the next paint. The delta is clamped twice, before and after it enters the accumulator, because a backgrounded tab or a long garbage collection hands back a delta measured in seconds: without the clamp the while loop would queue several hundred steps, take longer than a frame to run, arrive even later, and queue more. That is the spiral of death, and one line prevents it.

function flLoop(ts){
  flRaf = 0
  if (!flLast) flLast = ts
  let dt = (ts - flLast) / 1000
  flLast = ts
  if (!(dt > 0)) dt = 0
  // Clamp the frame delta before it ever reaches the accumulator. A backgrounded tab or a
  // long GC pause hands back a delta of seconds, and without this the while loop below
  // would queue hundreds of steps, take longer than a frame, and fall further behind.
  if (dt > FL_MAXACC) dt = FL_MAXACC
  flAcc += dt
  if (flAcc > FL_MAXACC) flAcc = FL_MAXACC
  while (flAcc >= FL_STEP){ flStep(FL_STEP); flAcc -= FL_STEP }
  flRender(flAcc / FL_STEP)
  // Idle out instead of spinning: a finished or paused run has nothing left to animate.
  if (flRunning && !((flG.mode === 'over' || flG.mode === 'pause') && flG.shake <= 0))
    flRaf = requestAnimationFrame(flLoop)
}

The leftover fraction of a step is handed to the renderer as an interpolation factor, so the avatar and the obstacles are drawn between their last two physics positions rather than snapped to the most recent one. Without that, a 1/120 s simulation on a 60Hz screen judders, because every other paint shows a state the display never asked for.

Collision is swept, not sampled

Testing where the avatar is once per frame answers the wrong question. What matters is whether anything lies on the path between where it was and where it now is. If the step is long enough, a solid object thinner than that path sits entirely between two samples and the avatar passes straight through it. This is tunnelling, and it is the reason a fast bullet in a naive engine goes through a wall while a slow one does not.

Honest accounting for this game: at the fixed 1/120 s step the avatar moves at most about 5.5 units per step, and the obstacles are 46 units wide, so tunnelling is not reachable with the shipped constants. The sweep is load bearing in two other places. The first is the accumulator clamp, where a stalled tab lets one rendered frame advance the world by up to 0.25 s, or 155 units of vertical travel and 57 units of horizontal, more than an obstacle's own width. The second is the future: raise the speed ceiling or widen the step and a point test starts lying long before anyone thinks to re-check the collision code.

The sweep runs in the obstacle's frame of reference. The avatar holds a fixed x while the world slides left, so relative to an obstacle it moves right and vertically at the same time. Grow the obstacle rectangle by the avatar's half extents (a Minkowski sum, which turns a box-versus-box problem into a segment-versus-box one), then clip the segment against each pair of slabs and see whether any interval survives.

function flSweep(p0x, p0y, p1x, p1y, hw, hh){
  const d = [p1x - p0x, p1y - p0y], p = [p0x, p0y], h = [hw, hh]
  let t0 = 0, t1 = 1
  for (let a = 0; a < 2; a++){
    if (Math.abs(d[a]) < 1e-9){ if (p[a] < -h[a] || p[a] > h[a]) return false; continue }
    let e0 = (-h[a] - p[a]) / d[a], e1 = (h[a] - p[a]) / d[a]
    if (e0 > e1){ const t = e0; e0 = e1; e1 = t }
    if (e0 > t0) t0 = e0
    if (e1 < t1) t1 = e1
    if (t0 > t1) return false
  }
  return true
}

The avatar's box is also inset by 3 units on every side before the sum. Players read a collision as unfair well before the pixels actually touch, so the forgiving box is a design choice, not a rounding error.

The gap band comes out of the constants

Randomising the next gap's height is easy and wrong. Sooner or later the generator produces two consecutive gaps further apart than the avatar can physically travel in the time between them, the run ends through no fault of the player, and the game feels arbitrary. So the band is derived rather than chosen.

A flap sets vertical velocity to -420 rather than adding to it, which makes the arithmetic clean. One flap gains 420² / (2 × 1400) = 63 units before gravity wins. Flapping again the moment velocity returns to zero makes the cycle 420 / 1400 = 0.3 s long, so the sustained climb rate is 63 / 0.3 = 210 units per second, which is exactly half the flap impulse. Falling is bounded instead by the terminal velocity of 620 units per second, which over the same interval covers more than twice the distance, so climbing is the binding direction and the band is sized on it.

Gap centres are 210 units apart, so the player has 210 / speed seconds between them: 1.4 s at the speed floor of 150, and 0.91 s at the ceiling of 230. The reachable climb is therefore 294 units at the floor and 192 at the ceiling. Sampling inside 55 per cent of that leaves room for an imperfect cadence, and it means the band tightens on its own as the game speeds up, without a second difficulty curve to tune.

function flBand(speed){
  const T = FL_SPACING / speed
  const up = (Math.abs(FL_FLAP) / 2) * T * 0.55
  const tv = FL_VMAX / FL_GRAV                                  // seconds to terminal velocity
  const fall = T <= tv ? 0.5 * FL_GRAV * T * T
                       : 0.5 * FL_VMAX * tv + FL_VMAX * (T - tv)
  return { up: up, down: Math.min(fall * 0.55, up * 1.5) }
}

Difficulty otherwise ramps in two clamped lines: the gap narrows by 2.5 units per point down to a floor of 88, and the scroll speed rises by 3 units per point up to a ceiling of 230. Both floors exist so that a long run gets harder and then stays hard, rather than becoming impossible.

Input is state, not an event

A keydown handler that calls the flap directly puts the impulse wherever in the frame the browser happened to deliver the event, which is somewhere between two physics steps. Worse, a press that arrives a few milliseconds before a step is easy to lose entirely if the handler writes to a variable the step has already read. Players notice: it reads as the game dropping inputs, and it is unfalsifiable from the outside, which makes it a miserable bug to be sent.

So the handler does not flap. It records that a flap was asked for, with a timestamp, and the next physics step consumes it. A press up to 120 ms early still lands on the step that follows. Anything older than that is dropped rather than fired late, which matters when the loop has been idle: the tab was hidden, the run was paused, and a press from before the pause should not launch the avatar on resume.

The same buffer is what makes the first flap start the run, so pressing early at the ready screen is never wasted. The ready state is also invulnerable for 0.8 s and the first obstacle is placed 430 units ahead of the avatar, which is a little under three seconds of run-in at the starting speed.

The parts that are not interesting but are still load bearing

  • Pooling. Twelve obstacles are allocated once at init and recycled by flipping a live flag when they leave the left edge. Nothing is allocated during a run, so there is nothing for the garbage collector to collect mid-flight, and no collection pause to show up as a stutter.
  • Device pixel ratio. The canvas backing store is sized to cssPixels × devicePixelRatio, capped at 3, and the context transform folds the ratio together with the world scale. Everything else in the game is written in world units, where the play field is always 360 units tall, so the physics never notices a resize and the same run plays identically on a phone and a desktop. A wider viewport simply sees further ahead.
  • Stopping properly. A loop left running after you leave would burn battery for nothing. Leaving the page cancels the one outstanding frame handle and unbinds every input listener. There are no timers in the game at all, so there is nothing else to cancel. The loop also idles itself out when a run is over or paused, and visibilitychange pauses rather than letting a hidden tab kill the player.
  • Reduced motion. With prefers-reduced-motion set, the death shake and the idle bob are skipped. The game itself is unchanged, because making it unplayable would be a worse answer than making it still.

Leaderboard

More games

All games

What should we call you?

Your name goes on the leaderboard of every game here. It stays in this browser.