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.
technical case study
Game • Browser arcade
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.
Menu with three levels, the record for each one saved in the browser and the rules in four lines.
A round on the hardest level: a hit with a particle burst, an alien being beamed up by the ship and the golden one about to escape.
End of the round: score, bonus for lives left, record and stats for kills, accuracy, best combo and reflex.
On a phone, the game responds to touch and accepts a few extra pixels around the target.
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.
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.
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.
Ready to go live on the games subdomain.
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.
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.
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.
No database: records, the selected level and the sound preference live in the browser's localStorage.
If localStorage is blocked, as in private mode, the game carries on normally, just without saving records.
No login, online leaderboard or data collection; the challenge only shares a text with the score and the game link.
Labels for screen readers, keyboard shortcuts (P to pause, M for sound) and reduced animations for people who ask for less motion.
The first version, built as a study: four HTML pages and a fixed interval between mosquitoes.
A single page, a requestAnimationFrame loop, its own clock and automatic pause.
Three real levels, a combo multiplier, a golden alien and a helmeted alien.
Effects generated with Web Audio and shots on pointerdown, with a larger touch area on phones.
Portuguese and English versions, ready for the games subdomain.
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 };
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();
}
// 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);
}
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();
}
A complete game in three files that loads fast because it doesn't download a single image or sound.
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.