technical case study

Game • Browser arcade

Mata Alien

A remake of Mata Mosquito, one of my first JavaScript projects: the crew of the site's UFO has landed, and you have to zap each one before the ship beams it back up. Pure JavaScript, with no images or libraries.

Game design, front-end and sound Playable Remake of a study project
JavaScript HTML CSS SVG Web Audio PHP
executive summary

Problem

The original Mata Mosquito, built while I was learning JavaScript, had four HTML pages, levels that changed nothing and only worked well with a mouse.

Solution

I rewrote the game from scratch on a single page, with the aliens from the site's UFO, three real levels, combos, special aliens, sounds generated in the browser and touch support.

Result

A quick arcade game that runs in any browser without downloading a single image or sound, with saved records and versions in Portuguese and English.

Current stage

Ready to go live on the games subdomain.

context

A first project, rebuilt with what I learned later

Mata Mosquito was one of my first JavaScript projects: click the mosquito before it disappears. Mata Alien keeps the idea (3 lives and holding out until time runs out) and fixes what didn't work: the selected level didn't change the interval, “Normal” and “normal” never matched, there were 4 lives instead of 3 and “Restart” came back without a level. The targets are now the crew of the UFO that flies around the portfolio.

goals and constraints

Goals

  • Keep the original idea: hit before it escapes, 3 lives and 30 seconds
  • Levels that really change pace, count and movement
  • Work as well with touch as with a mouse
  • No media files: aliens in SVG and sounds generated on the fly
  • Give reasons to play again: combos, records and challenges by link

Constraints

  • A single page, with no libraries and no images
  • Smooth animation on phones, without freezing the page
  • Browsers only allow audio after the first tap
  • Pause automatically when the tab loses focus
solution architecture

Front-end

index.php holds the menu, pause and end screens; game.css holds the visuals. Aliens, ship and icons are reused SVG symbols, and the variations (green, golden, helmeted) swap colors through CSS variables.

Back-end

game.js runs everything in a single requestAnimationFrame loop with its own clock: pausing freezes even each alien's lifetime. Aliens move only with transform, and particles live on a separate canvas.

Database

No database: records, the selected level and the sound preference live in the browser's localStorage.

security and safeguards

Works without storage

If localStorage is blocked, as in private mode, the game carries on normally, just without saving records.

Nothing leaves the browser

No login, online leaderboard or data collection; the challenge only shares a text with the score and the game link.

Accessible

Labels for screen readers, keyboard shortcuts (P to pause, M for sound) and reduced animations for people who ask for less motion.

process
01

Mata Mosquito

The first version, built as a study: four HTML pages and a fixed interval between mosquitoes.

02

Rewrite from scratch

A single page, a requestAnimationFrame loop, its own clock and automatic pause.

03

New rules

Three real levels, a combo multiplier, a golden alien and a helmeted alien.

04

Sound and touch

Effects generated with Web Audio and shots on pointerdown, with a larger touch area on phones.

05

Launch

Portuguese and English versions, ready for the games subdomain.

behind the scenes
mata-alien/game.js JavaScript lines 80–98
  const DURATION = 30000;
  const MAX_LIVES = 3;
  const LIFE_BONUS = 250;
  const COUNTDOWN_STEP = 600;

  const LEVELS = {
    facil:   { spawn: [1300, 900], ttl: [2400, 1700], maxAlive: 1, drift: 0,  gold: 0.06, armored: 0 },
    dificil: { spawn: [950, 620],  ttl: [1900, 1300], maxAlive: 3, drift: 40, gold: 0.07, armored: 0.14 },
    mori:    { spawn: [680, 420],  ttl: [1350, 950],  maxAlive: 4, drift: 95, gold: 0.08, armored: 0.18 }
  };

  // os três tamanhos do original (50, 70 e 90 px), um pouco maiores; menor vale mais
  const SIZES = [
    { px: 54, pts: 150 },
    { px: 72, pts: 100 },
    { px: 92, pts: 75 }
  ];

  const POINTS = { gold: 500, armored: 250 };
The rules in one place: duration, lives, the pace of each level and how much each type of alien is worth.
mata-alien/game.js JavaScript lines 566–615
  function spawn(progress) {
    const cfg = game.cfg;
    const roll = Math.random();
    const kind = roll < cfg.gold ? 'gold' : roll < cfg.gold + cfg.armored ? 'armored' : 'normal';

    let sizeIndex = Math.floor(Math.random() * 3);
    if (kind === 'gold') sizeIndex = Math.floor(Math.random() * 2);
    if (kind === 'armored') sizeIndex = 1 + Math.floor(Math.random() * 2);

    const def = SIZES[sizeIndex];
    const size = Math.round(def.px * scale);

    let ttl = lerp(cfg.ttl[0], cfg.ttl[1], progress);
    if (kind === 'gold') ttl *= 0.7;
    if (kind === 'armored') ttl *= 1.35;

    const spot = findSpot(size);
    const el = TEMPLATES[kind].cloneNode(true);
    el.classList.add('alien--' + kind);
    if (Math.random() < 0.5) el.classList.add('flip');
    el.style.setProperty('--size', size + 'px');

    const alien = {
      el,
      kind,
      size,
      x: spot.x,
      y: spot.y,
      vx: 0,
      vy: 0,
      born: game.t,
      ttl,
      hp: kind === 'armored' ? 2 : 1,
      pts: POINTS[kind] || def.pts,
      warned: false
    };

    if (cfg.drift) {
      const angle = Math.random() * Math.PI * 2;
      const speed = cfg.drift * rand(0.6, 1.3) * scale * (0.7 + progress * 0.6);
      alien.vx = Math.cos(angle) * speed;
      alien.vy = Math.sin(angle) * speed;
    }

    place(alien);
    byElement.set(el, alien);
    stage.appendChild(el);
    game.aliens.push(alien);
    sfx.spawn();
  }
Each alien appears in a free spot on the screen; its type, size and time before escaping depend on the level and on how much of the round has passed.
mata-alien/game.js JavaScript lines 642–677
  // no toque o dedo cobre o alvo: aceita alguns pixels a mais em volta
  function alienAt(x, y, target, pointerType) {
    const direct = target.closest && target.closest('.alien');
    if (direct && byElement.has(direct)) {
      const alien = byElement.get(direct);
      if (game.aliens.includes(alien)) return alien;
    }

    const slack = pointerType === 'mouse' ? 4 : 16;
    let found = null;
    let bestDistance = Infinity;

    for (const alien of game.aliens) {
      const [cx, cy] = center(alien);
      const distance = Math.hypot(cx - x, cy - y) - alien.size / 2;
      if (distance <= slack && distance < bestDistance) {
        bestDistance = distance;
        found = alien;
      }
    }

    return found;
  }

  function shoot(event) {
    if (game.state !== 'playing') return;
    if (event.button > 0) return;

    event.preventDefault();
    sfx.unlock();
    game.shots++;

    const alien = alienAt(event.clientX, event.clientY, event.target, event.pointerType);
    if (alien) hit(alien);
    else miss(event.clientX, event.clientY);
  }
With touch, the finger covers the target, so a shot accepts a few extra pixels around it; with a mouse, the tolerance is smaller.
mata-alien/game.js JavaScript lines 695–725
  function kill(alien) {
    removeAlien(alien);

    const before = multiplier();
    game.kills++;
    game.combo++;
    game.bestCombo = Math.max(game.bestCombo, game.combo);
    // relógio do jogo + o que passou desde o último quadro: reflexo em ms, não em quadros
    const now = game.t + Math.min(performance.now() - last, 50);
    game.fastest = Math.min(game.fastest, now - alien.born);

    const mult = multiplier();
    const pts = alien.pts * mult;
    game.score += pts;

    const [cx, cy] = center(alien);
    const gold = alien.kind === 'gold';
    fx.burst(cx, cy, gold ? '#ffd36b' : '#8be28f', gold ? 26 : 16, alien.size / 76);
    popup('+' + fmt(pts), cx, cy, gold ? 'gold' : '');
    if (mult > before) popup(T.combo(mult), cx, cy - alien.size * 0.7, 'combo');

    alien.el.classList.add('is-dead');
    removeLater(alien.el, 260);

    if (gold) sfx.gold();
    else sfx.zap();

    // um respiro antes do próximo (no fácil é um de cada vez)
    game.nextSpawn = Math.max(game.nextSpawn, game.t + 220);
    renderScore();
  }
On a kill, the combo multiplies the points, the reflex is measured in milliseconds by the game clock and the golden one gets a bigger burst.
impact

Results

A complete game in three files that loads fast because it doesn't download a single image or sound.

Learnings

Rebuilding an old project shows progress in a concrete way: the same goals, now with well-defined state, a single game loop and care for touch, sound and accessibility.