Home What I do Work Playground Toolbox About Contact Shop ↗ DEEN

Playground · mini game

UFO Alert: fend off the saucers

Flying saucers attack the city. You aim, shoot down bombs, collect power-ups and hold out until the mothership. One HTML file from one prompt, with mouse, finger or keyboard.

Open in a new tab ↗ Download as a file

The prompt

Copy it and paste it into Claude, ChatGPT or Gemini. The AI answers with the complete code.

Build me a UFO defence game as one single HTML file, without external libraries.
- At the bottom centre there's a cannon in front of a city skyline. It aims wherever the mouse or the finger is. A click or the space bar fires, holding it down means continuous fire. You can also aim with the arrow keys.
- Flying saucers come in from above, the way everyone knows them: metal disc, glass dome with a little green alien, blinking lights around the rim. They fly in arcs and loops, not in rows.
- Three kinds: small fast scouts that dive at the cannon now and then, bombers that drop bombs, and big UFOs that split into two small ones when shot down. Bombs can be shot.
- The waves get harder. Every fifth wave brings a mothership with a health bar.
- UFOs that get shot down sometimes drop a power-up: shield, triple shot or slow motion. Whoever hits it gets it.
- Three lives. Quick kills in a row give a combo bonus. The high score is saved.
- Night sky with stars, explosions with sparks, a short shake on hits. Sounds with the Web Audio API, M turns them on and off.
- Esc or an ✕ at the top right quits. If the tab goes to the background, the game pauses.
- Runs on PC and phone, portrait and landscape.

How to run it

  1. Copy the code from the AI's answer (there is usually a copy button on the code block).
  2. Open a text editor, Notepad for example, paste the code and save it as ufo-alarm.html. The .html ending is what matters.
  3. Double-click the file. It opens in your browser, done. No server, no install.

Fixed afterwards

After the first test, these follow-ups were added. Same chat, simply as the next message:

After the game is over, falling bombs still cost lives, and a UFO crashing into the cannon gives points. Stop both.
The UFOs are too small on a PC. Make them about a third bigger.
There's a big murky patch of light under the UFOs. Turn it into a soft glow right under the disc.
The moon is a crescent made of two circles, and the dark circle sits visibly on the halo. Make a full moon with a few craters.
On the start screen the UFOs fly through the title. Let them circle below the text.
Power-ups come too often, you nearly always have all three. Make them rarer, at most one every 15 seconds. The mothership should take a lot more hits, and the combo should only go up to ×4.
The mothership's health bar sits on top of the score. Move it below. The mothership shouldn't fly over the buttons at the top right, and in portrait it should sit a bit lower.
After the game is over, the cannon is sometimes visible and sometimes not. It should stay destroyed.
On the phone the help line is too wide and gets cut off. Text that is too wide should shrink on its own.
Holding the mouse button and sweeping left and right shoots everything down without aiming. Add a heat gauge: continuous fire overheats the barrel after about 3 seconds, then nothing works for a moment. Make the fire rate a bit slower, and a missed shot should break the combo.
Include accuracy in the final score: at the end show shots, hit rate and a bonus that's higher the better you hit.

The code

This is the code that came out of the prompt and the follow-ups above (with Claude). Every AI builds it a little differently, that's normal. If your version doesn't run, paste the error message or a description back into the chat.

Show code (800 lines)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>UFO Alert</title>
<style>
  html, body { margin: 0; height: 100%; background: #05070d; overflow: hidden; }
  canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair;
           user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent; }
</style>
</head>
<body>
<canvas id="feld"></canvas>
<script>
const canvas = document.getElementById('feld');
const ctx = canvas.getContext('2d');

const TEXT = {
  titel: 'UFO Alert',
  unter: 'The saucers are coming. Keep the city clear.',
  start: 'Click, tap or press space to start',
  hilfe: 'Aim with mouse, finger or arrow keys · Click or hold to fire',
  hilfe2: 'Short bursts hit better: continuous fire overheats the barrel',
  ueberhitzt: 'Overheated!', schuesse: 'Shots', treffer: 'Hits', bonus: 'Bonus', gesamt: 'Total',
  welle: 'Wave', geschafft: 'cleared', mutter: 'Warning: mothership!',
  bestwert: 'Best', neu: 'New high score!', vorbei: 'Game over',
  erreicht: 'Reached: wave', nochmal: 'Again: click, tap or press space',
  abbrechen: 'Esc or ✕: quit · M: sound', pause: 'Paused', weiter: 'Tap or press a key to continue',
  liste: 'Your best rounds (this session)',
  schild: 'Shield', dreifach: 'Triple shot', zeitlupe: 'Slow motion'
};
const SPEICHER = 'ufo-alarm-bestwert', SITZUNG = 'ufo-alarm-runden', TON = 'ufo-alarm-ton';

// ---------- Grundgrößen: alles in Einheiten, die kürzere Bildschirmseite hat 100 ----------
let dpr, sw, sh, e, W, H, stadt = [];
let zustand = 'start', welle, punkte, bestwert, leben, combo, comboZeit, neuerBest;
let geschuetz, schuesse, ufos, bomben, extras, teile, texte, warteschlange, spawnZeit, banner, wackeln, zeit;
let zeiger = { x: 0, y: 0, aktiv: false }, feuer = false, tasten = { l: false, r: false, feuer: false };
let runden, letzteRunde = null, sperre = 0, demoZeit = 0, letztesExtra = -99;
let schussZahl = 0, trefferZahl = 0, bonus = 0, rohPunkte = 0;   // für die Endwertung
let sterne = [];

function laden(k, d) { try { const v = localStorage.getItem(k); return v === null ? d : v; } catch (x) { return d; } }
function speichern(k, v) { try { localStorage.setItem(k, String(v)); } catch (x) {} }
function ladeRunden() { try { return JSON.parse(sessionStorage.getItem(SITZUNG)) || []; } catch (x) { return []; } }
function rundeMerken() {
  if (punkte <= 0) { letzteRunde = null; return; }
  letzteRunde = Date.now() + Math.random();
  runden.push({ p: punkte, w: welle, id: letzteRunde });
  runden.sort((a, b) => b.p - a.p);
  runden = runden.slice(0, 5);
  try { sessionStorage.setItem(SITZUNG, JSON.stringify(runden)); } catch (x) {}
}

const zufall = (a, b) => a + Math.random() * (b - a);
const klemme = (v, a, b) => Math.max(a, Math.min(b, v));

function groesse() {
  dpr = Math.min(window.devicePixelRatio || 1, 2);
  sw = window.innerWidth; sh = window.innerHeight;
  canvas.width = Math.round(sw * dpr); canvas.height = Math.round(sh * dpr);
  e = Math.min(sw, sh) / 100;
  W = sw / e; H = sh / e;
  // Stadt-Silhouette und Sterne passend zur Breite
  stadt = []; let x = -2;
  while (x < W + 2) {
    const b = zufall(4, 10), h = zufall(5, 15) * (Math.abs(x + b / 2 - W / 2) < 12 ? 0.55 : 1);
    const fenster = [];
    for (let fy = 1.5; fy < h - 1; fy += 2.2) for (let fx = 1; fx < b - 1; fx += 1.8) if (Math.random() < 0.28) fenster.push([fx, fy, Math.random()]);
    stadt.push({ x, b, h, fenster }); x += b + zufall(0.3, 1.2);
  }
  sterne = [];
  for (let i = 0; i < Math.round(W * H / 55); i++) sterne.push({ x: zufall(0, W), y: zufall(0, H * 0.9), g: zufall(0.15, 0.55), p: zufall(0, 6.3) });
  if (geschuetz) { geschuetz.x = W / 2; geschuetz.y = H - 8; }
}
const px = x => x * e, py = y => y * e;

// ---------- Ton (Web Audio, ohne Dateien) ----------
let audio = null, tonAn = laden(TON, '1') === '1';
function ac() {
  if (!audio) { try { audio = new (window.AudioContext || window.webkitAudioContext)(); } catch (x) { return null; } }
  if (audio.state === 'suspended') audio.resume();
  return audio;
}
function piep(f, dauer, typ, laut, ziel) {
  if (!tonAn) return; const a = ac(); if (!a) return;
  const o = a.createOscillator(), g = a.createGain(), t = a.currentTime;
  o.type = typ || 'square'; o.frequency.setValueAtTime(f, t);
  if (ziel) o.frequency.exponentialRampToValueAtTime(ziel, t + dauer);
  g.gain.setValueAtTime(0.0001, t); g.gain.exponentialRampToValueAtTime(laut || 0.05, t + 0.01);
  g.gain.exponentialRampToValueAtTime(0.0001, t + dauer);
  o.connect(g).connect(a.destination); o.start(t); o.stop(t + dauer + 0.02);
}
let rauschPuffer = null;
function rauschen(dauer, laut, filter) {
  if (!tonAn) return; const a = ac(); if (!a) return;
  if (!rauschPuffer) {
    rauschPuffer = a.createBuffer(1, a.sampleRate, a.sampleRate);
    const d = rauschPuffer.getChannelData(0); for (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1;
  }
  const q = a.createBufferSource(), f = a.createBiquadFilter(), g = a.createGain(), t = a.currentTime;
  q.buffer = rauschPuffer; f.type = 'lowpass'; f.frequency.setValueAtTime(filter || 1200, t);
  f.frequency.exponentialRampToValueAtTime(80, t + dauer);
  g.gain.setValueAtTime(laut || 0.2, t); g.gain.exponentialRampToValueAtTime(0.0001, t + dauer);
  q.connect(f).connect(g).connect(a.destination); q.start(t); q.stop(t + dauer);
}
const klang = {
  schuss() { piep(880, 0.07, 'square', 0.018, 440); },
  treffer() { piep(300, 0.06, 'triangle', 0.04, 180); },
  boom(gross) { rauschen(gross ? 0.9 : 0.35, gross ? 0.35 : 0.16, gross ? 900 : 1600); piep(gross ? 90 : 160, gross ? 0.6 : 0.2, 'sawtooth', 0.04, 40); },
  bombe() { piep(520, 0.25, 'sine', 0.02, 260); },
  au() { rauschen(0.6, 0.3, 700); piep(140, 0.5, 'square', 0.05, 50); },
  extra() { [523, 659, 784, 1047].forEach((f, i) => setTimeout(() => piep(f, 0.12, 'triangle', 0.05), i * 70)); },
  welle() { [392, 523, 659].forEach((f, i) => setTimeout(() => piep(f, 0.18, 'triangle', 0.045), i * 110)); },
  alarm() { [0, 1, 2].forEach(i => setTimeout(() => piep(660, 0.22, 'sawtooth', 0.03, 330), i * 300)); }
};

// ---------- UFO-Sorten ----------
// r = halbe Breite, v = Tempo, hp = Treffer, p = Punkte, licht = Farben der Randlichter
const SORTE = {
  spaeher: { r: 5.4, v: 34, hp: 1, p: 100, metall: ['#e8edf2', '#8792a0', '#4a525e'], licht: ['#5ef2ff', '#ffffff'], kuppel: 'rgba(150,235,255,', strahl: '94,242,255' },
  bomber:  { r: 7.8, v: 20, hp: 2, p: 150, metall: ['#f0e6da', '#9c8f80', '#544a40'], licht: ['#ff7a3c', '#ffd34d'], kuppel: 'rgba(255,214,160,', strahl: '255,140,60' },
  teiler:  { r: 8.4, v: 24, hp: 3, p: 200, metall: ['#e6def0', '#8e82a3', '#4b4258'], licht: ['#c77dff', '#7ef0a0'], kuppel: 'rgba(210,170,255,', strahl: '190,120,255' },
  klein:   { r: 4.2, v: 42, hp: 1, p: 75,  metall: ['#e6def0', '#8e82a3', '#4b4258'], licht: ['#c77dff', '#ffffff'], kuppel: 'rgba(210,170,255,', strahl: '190,120,255' },
  mutter:  { r: 22,  v: 12, hp: 40, p: 2000, metall: ['#f2f4f6', '#9aa3ae', '#454c56'], licht: ['#ff4d6d', '#5ef2ff', '#ffd34d'], kuppel: 'rgba(150,235,255,', strahl: '255,80,110' }
};

function tempoFaktor() { return Math.min(1.6, 1 + (welle - 1) * 0.035); }
function zeitFaktor() { return geschuetz.zeitlupe > 0 ? 0.45 : 1; }

function neuesUfo(sorte, x, y) {
  const s = SORTE[sorte];
  const u = { sorte, s, x: x !== undefined ? x : zufall(10, W - 10), y: y !== undefined ? y : -s.r - 4,
              vx: 0, vy: 0, hp: s.hp, tx: 0, ty: 0, blitz: 0, phase: zufall(0, 6.3),
              bombeIn: zufall(1.2, 3), sturzIn: zufall(4, 9), modus: 'flug', rot: 0 };
  if (sorte === 'mutter') { u.hp = 70 + Math.floor(welle / 5 - 1) * 30; u.max = u.hp; u.y = -s.r; u.ty = Math.max(26, H * 0.2); u.tx = W / 2; u.spawnIn = 4; }
  else neuesZiel(u);
  ufos.push(u); return u;
}
function neuesZiel(u) {
  u.tx = zufall(u.s.r + 4, W - u.s.r - 4);
  u.ty = u.demo ? zufall(H * 0.58, H * 0.74) : zufall(u.s.r + 6, H * 0.52);
}

// ---------- Spielablauf ----------
function neuesSpiel() {
  welle = 0; punkte = 0; leben = 3; combo = 0; comboZeit = 0; neuerBest = false; wackeln = 0; zeit = 0; letztesExtra = -99;
  schussZahl = 0; trefferZahl = 0; bonus = 0; rohPunkte = 0;
  geschuetz = { x: W / 2, y: H - 8, winkel: 0, kuehl: 0, blitz: 0, schild: 0, dreifach: 0, zeitlupe: 0, unverwundbar: 0,
                hitze: 0, ueberhitzt: 0 };
  schuesse = []; ufos = []; bomben = []; extras = []; teile = []; texte = []; warteschlange = []; banner = null;
}
function starten() { neuesSpiel(); zustand = 'spiel'; ac(); naechsteWelle(); }

function naechsteWelle() {
  welle++;
  warteschlange = []; spawnZeit = 1.2;
  if (welle % 5 === 0) {
    warteschlange.push('mutter');
    banner = { text: TEXT.mutter, t: 2.6, farbe: '#ff6b81' }; klang.alarm();
  } else {
    const n = Math.min(6 + welle * 2, 26);
    for (let i = 0; i < n; i++) {
      const r = Math.random();
      if (welle >= 3 && r < 0.22) warteschlange.push('teiler');
      else if (welle >= 2 && r < 0.5) warteschlange.push('bomber');
      else warteschlange.push('spaeher');
    }
    banner = { text: `${TEXT.welle} ${welle}`, t: 2, farbe: '#e8f5e2' };
    if (welle > 1) klang.welle();
  }
}

function punkteFuer(p, x, y) {
  combo = comboZeit > 0 ? combo + 1 : 1; comboZeit = 1.3;
  const mal = Math.min(4, 1 + Math.floor((combo - 1) / 4));
  punkte += p * mal;
  texte.push({ x, y, t: 0.9, text: mal > 1 ? `+${p * mal}  ×${mal}` : `+${p}`, farbe: mal > 1 ? '#ffd34d' : '#d8ffc8' });
}

function explosion(x, y, groesse, farben) {
  const n = Math.round(14 + groesse * 3);
  for (let i = 0; i < n && teile.length < 500; i++) {
    const w = zufall(0, Math.PI * 2), v = zufall(8, 32) * (0.6 + groesse * 0.08);
    teile.push({ x, y, vx: Math.cos(w) * v, vy: Math.sin(w) * v, t: zufall(0.4, 0.9), max: 0.9, art: 'funke',
                 farbe: farben[i % farben.length], g: zufall(0.35, 0.8) });
  }
  for (let i = 0; i < Math.round(groesse) + 3 && teile.length < 500; i++) {
    const w = zufall(-Math.PI, 0), v = zufall(10, 22);
    teile.push({ x, y, vx: Math.cos(w) * v, vy: Math.sin(w) * v, t: zufall(0.8, 1.3), max: 1.3, art: 'splitter',
                 rot: zufall(0, 6), vr: zufall(-8, 8), g: zufall(0.6, 1.4) });
  }
  teile.push({ x, y, t: 0.45, max: 0.45, art: 'ring', g: groesse * 1.8 + 4 });
  wackeln = Math.max(wackeln, Math.min(1.6, 0.25 + groesse * 0.12));
}

function ufoWeg(u) {
  const s = u.s;
  explosion(u.x, u.y, s.r, [s.licht[0], '#ffffff', '#ffd34d']);
  klang.boom(u.sorte === 'mutter');
  punkteFuer(s.p, u.x, u.y - s.r);
  if (u.sorte === 'teiler') {
    for (const d of [-1, 1]) { const k = neuesUfo('klein', u.x + d * 3, u.y); k.vx = d * 30; k.vy = -12; }
  }
  if (u.sorte === 'mutter') { wackeln = 2; for (let i = 0; i < 4; i++) setTimeout(() => explosion(u.x + zufall(-12, 12), u.y + zufall(-3, 3), 5, ['#ff4d6d', '#ffd34d', '#ffffff']), i * 160); }
  if (u.sorte !== 'klein' && !extras.length && (u.sorte === 'mutter' || (zeit - letztesExtra > 15 && Math.random() < 0.06))) {
    letztesExtra = zeit;
    const art = ['schild', 'dreifach', 'zeitlupe'][Math.floor(Math.random() * 3)];
    extras.push({ x: u.x, y: u.y, art, phase: 0, t: 12 });
  }
}

function getroffen() {
  const g = geschuetz;
  if (zustand !== 'spiel' || g.schild > 0 || g.unverwundbar > 0) return;
  leben--; g.unverwundbar = 2.2;
  explosion(g.x, g.y - 2, 7, ['#ff6b6b', '#ffd34d', '#ffffff']); klang.au(); wackeln = 1.8;
  combo = 0; comboZeit = 0;
  if (leben <= 0) vorbei();
}

function quote() { return schussZahl ? trefferZahl / schussZahl : 0; }
function vorbei() {
  zustand = 'vorbei'; sperre = 1; feuer = false; tasten.feuer = false;
  // Treffsicherheit zählt: Bonus = halbe Punktzahl mal Trefferquote
  rohPunkte = punkte; bonus = Math.round(punkte * quote() * 0.5 / 10) * 10; punkte += bonus;
  if (punkte > bestwert) { bestwert = punkte; neuerBest = true; speichern(SPEICHER, bestwert); }
  rundeMerken();
}
function abbrechen() {
  if (zustand === 'spiel' || zustand === 'pause') {
    if (punkte > bestwert) { bestwert = punkte; speichern(SPEICHER, bestwert); }
    rundeMerken();
  }
  feuer = false; zustand = 'start'; demoStart();
}

// Startbildschirm: ein paar UFOs kreisen, das Geschütz schaut ihnen nach
function demoStart() {
  neuesSpiel(); welle = 1;
  ['spaeher', 'bomber', 'teiler', 'spaeher'].forEach((s, i) => { const u = neuesUfo(s, W * (0.2 + i * 0.2), H * zufall(0.58, 0.74)); u.demo = true; neuesZiel(u); });
}

// ---------- Eingabe ----------
function knopfX() { const g = Math.max(32, Math.min(sw, sh) * 0.065); return { x: sw - g / 2 - 12, y: g / 2 + 12, r: g / 2 }; }
function knopfTon() { const k = knopfX(); return { x: k.x - k.r * 2.6, y: k.y, r: k.r }; }
const auf = (k, ev) => Math.hypot(ev.clientX - k.x, ev.clientY - k.y) <= k.r + 6;
function tonUmschalten() { tonAn = !tonAn; speichern(TON, tonAn ? '1' : '0'); if (tonAn) { ac(); klang.treffer(); } }

canvas.addEventListener('pointerdown', ev => {
  ev.preventDefault();
  try { canvas.setPointerCapture(ev.pointerId); } catch (x) {}
  ac();
  if (auf(knopfTon(), ev)) { tonUmschalten(); return; }
  if ((zustand === 'spiel' || zustand === 'pause') && auf(knopfX(), ev)) { abbrechen(); return; }
  zeiger = { x: ev.clientX, y: ev.clientY, aktiv: true };
  if (zustand === 'start') starten();
  else if (zustand === 'pause') zustand = 'spiel';
  else if (zustand === 'vorbei') { if (sperre <= 0) starten(); }
  else feuer = true;
});
canvas.addEventListener('pointermove', ev => {
  if (ev.pointerType === 'mouse' || feuer) zeiger = { x: ev.clientX, y: ev.clientY, aktiv: true };
});
const loslassen = () => { feuer = false; };
canvas.addEventListener('pointerup', loslassen);
canvas.addEventListener('pointercancel', loslassen);
canvas.addEventListener('contextmenu', ev => ev.preventDefault());

window.addEventListener('keydown', ev => {
  const k = ev.key;
  if (k === ' ' || ev.code === 'Space') {
    ev.preventDefault(); ac();
    if (ev.repeat) return;
    if (zustand === 'start') starten();
    else if (zustand === 'pause') zustand = 'spiel';
    else if (zustand === 'vorbei') { if (sperre <= 0) starten(); }
    else tasten.feuer = true;
  }
  if (k === 'ArrowLeft' || k === 'a' || k === 'A') { tasten.l = true; zeiger.aktiv = false; ev.preventDefault(); }
  if (k === 'ArrowRight' || k === 'd' || k === 'D') { tasten.r = true; zeiger.aktiv = false; ev.preventDefault(); }
  if (k === 'm' || k === 'M') tonUmschalten();
  if (k === 'p' || k === 'P') { if (zustand === 'spiel') zustand = 'pause'; else if (zustand === 'pause') zustand = 'spiel'; }
  if (k === 'Escape' && zustand !== 'start') abbrechen();
});
window.addEventListener('keyup', ev => {
  const k = ev.key;
  if (k === ' ' || ev.code === 'Space') tasten.feuer = false;
  if (k === 'ArrowLeft' || k === 'a' || k === 'A') tasten.l = false;
  if (k === 'ArrowRight' || k === 'd' || k === 'D') tasten.r = false;
});
window.addEventListener('resize', groesse);
document.addEventListener('visibilitychange', () => { if (document.hidden && zustand === 'spiel') { zustand = 'pause'; feuer = false; tasten.feuer = false; } });
window.addEventListener('blur', () => { feuer = false; tasten.feuer = false; tasten.l = tasten.r = false; });

// ---------- Bewegung ----------
function zielwinkel() {
  const g = geschuetz;
  if (zeiger.aktiv) return klemme(Math.atan2(zeiger.x / e - g.x, -(zeiger.y / e - (g.y - 2))), -1.45, 1.45);
  return g.winkel;
}

function update(dt) {
  zeit += dt;
  for (const s of sterne) s.p += dt * 1.5;
  wackeln = Math.max(0, wackeln - dt * 2.5);
  if (zustand === 'vorbei') sperre -= dt;
  if (zustand === 'pause') return;
  const g = geschuetz;
  const z = zeitFaktor();

  // Geschütz
  if (zustand === 'spiel') {
    if (tasten.l) g.winkel = Math.max(-1.45, g.winkel - 2.4 * dt);
    else if (tasten.r) g.winkel = Math.min(1.45, g.winkel + 2.4 * dt);
    else { const zw = zielwinkel(); g.winkel += (zw - g.winkel) * Math.min(1, dt * 18); }
    g.kuehl -= dt; g.blitz = Math.max(0, g.blitz - dt * 8);
    g.ueberhitzt = Math.max(0, g.ueberhitzt - dt);
    g.hitze = Math.max(0, g.hitze - dt * (g.ueberhitzt > 0 ? 0.7 : 0.35));
    for (const k of ['schild', 'dreifach', 'zeitlupe', 'unverwundbar']) g[k] = Math.max(0, g[k] - dt);
    if ((feuer || tasten.feuer) && g.kuehl <= 0) schiessen();
    comboZeit = Math.max(0, comboZeit - dt);
  } else {
    // Startbildschirm: dem nächsten UFO nachschauen
    const u = ufos[0];
    if (u) { const zw = klemme(Math.atan2(u.x - g.x, -(u.y - g.y)), -1.45, 1.45); g.winkel += (zw - g.winkel) * Math.min(1, dt * 2); }
  }

  // Wellen nachschieben
  if (zustand === 'spiel') {
    if (warteschlange.length) {
      spawnZeit -= dt;
      if (spawnZeit <= 0) { neuesUfo(warteschlange.shift()); spawnZeit = Math.max(0.35, 1.05 - welle * 0.05); }
    } else if (!ufos.length && !banner) {
      punkte += 50 * welle;
      banner = { text: `${TEXT.welle} ${welle} ${TEXT.geschafft}`, t: 1.6, farbe: '#9fd88f', danach: true };
    }
  }
  if (banner) { banner.t -= dt; if (banner.t <= 0) { const weiter = banner.danach; banner = null; if (weiter && zustand === 'spiel') naechsteWelle(); } }

  // Schüsse
  for (const s of schuesse) { s.x += s.vx * dt; s.y += s.vy * dt; }
  for (const s of schuesse) if (!s.weg && (s.y <= -5 || s.x <= -5 || s.x >= W + 5)) { combo = 0; comboZeit = 0; }
  schuesse = schuesse.filter(s => s.y > -5 && s.x > -5 && s.x < W + 5 && !s.weg);

  // UFOs
  for (const u of ufos) {
    const s = u.s, v = s.v * tempoFaktor() * z;
    u.phase += dt * 3; u.blitz = Math.max(0, u.blitz - dt * 6);
    if (u.sorte === 'mutter') {
      if (u.y < u.ty) u.y += 10 * dt * z;
      u.x = W / 2 + Math.sin(zeit * 0.35) * (W / 2 - s.r - 4);
      if (zustand === 'spiel' && u.y >= u.ty - 1) {
        u.bombeIn -= dt * z;
        if (u.bombeIn <= 0) { for (const d of [-8, 0, 8]) bomben.push({ x: u.x + d, y: u.y + 4, vx: d * 0.8, vy: 20 }); klang.bombe(); u.bombeIn = Math.max(1.1, 2.2 - welle * 0.04); }
        u.spawnIn -= dt * z;
        if (u.spawnIn <= 0 && ufos.length < 8) { neuesUfo('spaeher', u.x, u.y + 4); u.spawnIn = 4; }
      }
      continue;
    }
    if (u.modus === 'sturz') {
      const dx = geschuetz.x - u.x, dy = geschuetz.y - 3 - u.y, d = Math.hypot(dx, dy) || 1;
      u.vx += (dx / d * 62 - u.vx) * Math.min(1, dt * 2.2); u.vy += (dy / d * 62 - u.vy) * Math.min(1, dt * 2.2);
      if (u.y > H - 24) u.zieht = true;
    } else {
      const dx = u.tx - u.x, dy = u.ty - u.y, d = Math.hypot(dx, dy);
      if (d < 5) neuesZiel(u);
      // weich zum Ziel lenken, dazu eine kleine Schleifen-Bewegung
      const sx = dx / (d || 1) * v + Math.cos(u.phase * 0.7) * v * 0.35, sy = dy / (d || 1) * v + Math.sin(u.phase) * v * 0.25;
      u.vx += (sx - u.vx) * Math.min(1, dt * 1.6); u.vy += (sy - u.vy) * Math.min(1, dt * 1.6);
    }
    u.x += u.vx * dt * (u.modus === 'sturz' ? z : 1); u.y += u.vy * dt * (u.modus === 'sturz' ? z : 1);
    u.rot = klemme(u.vx / 120, -0.35, 0.35);
    if (u.modus === 'sturz' && u.zieht && u.y > H - 14) { u.modus = 'flug'; u.zieht = false; neuesZiel(u); u.vy = -30; }
    if (u.demo || zustand !== 'spiel') continue;
    if (u.sorte === 'bomber') {
      u.bombeIn -= dt * z;
      if (u.bombeIn <= 0 && u.y > 0) {
        bomben.push({ x: u.x, y: u.y + 3, vx: klemme((geschuetz.x - u.x) * 0.12, -10, 10), vy: 16 + welle * 0.8 });
        klang.bombe(); u.bombeIn = zufall(2.2, 3.8) / Math.min(1.5, 1 + welle * 0.04);
      }
    }
    if ((u.sorte === 'spaeher' || u.sorte === 'klein') && welle >= 2 && u.modus === 'flug') {
      u.sturzIn -= dt * z;
      if (u.sturzIn <= 0 && u.y > 4) { u.modus = 'sturz'; u.sturzIn = zufall(6, 11); }
    }
    // Zusammenstoß mit dem Geschütz
    // Zusammenstoß mit dem Geschütz: das UFO ist hin, aber Punkte gibt es dafür nicht
    if (Math.hypot(u.x - geschuetz.x, u.y - (geschuetz.y - 2)) < s.r + 4) {
      u.weg = true; explosion(u.x, u.y, s.r, [s.licht[0], '#ffffff', '#ffd34d']); klang.boom(false); getroffen();
    }
  }
  ufos = ufos.filter(u => !u.weg);

  // Bomben
  for (const b of bomben) {
    b.x += b.vx * dt * z; b.y += b.vy * dt * z;
    if (Math.hypot(b.x - geschuetz.x, b.y - (geschuetz.y - 2)) < (geschuetz.schild > 0 ? 9 : 4.5)) {
      b.weg = true;
      if (geschuetz.schild > 0) { explosion(b.x, b.y, 1.5, ['#5ef2ff', '#ffffff']); klang.treffer(); } else getroffen();
    } else if (b.y > H - 5) { b.weg = true; explosion(b.x, H - 5, 1.2, ['#ff7a3c', '#ffd34d']); }
  }
  bomben = bomben.filter(b => !b.weg);

  // Extras
  for (const x of extras) { x.y += 11 * dt; x.phase += dt * 3; x.t -= dt; if (x.y > H - 6 || x.t <= 0) x.weg = true; }
  extras = extras.filter(x => !x.weg);

  // Treffer: Schüsse gegen Bomben, Extras und UFOs
  for (const s of schuesse) {
    for (const b of bomben) if (!b.weg && Math.hypot(s.x - b.x, s.y - b.y) < 2.6) {
      b.weg = s.weg = true; trefferZahl++; explosion(b.x, b.y, 1, ['#ff7a3c', '#ffd34d']); klang.treffer(); punkteFuer(25, b.x, b.y); break;
    }
    if (s.weg) continue;
    for (const x of extras) if (Math.hypot(s.x - x.x, s.y - x.y) < 4.2) {
      s.weg = x.weg = true; trefferZahl++; geschuetz[x.art] = x.art === 'zeitlupe' ? 6 : 10; klang.extra();
      texte.push({ x: x.x, y: x.y, t: 1.2, text: TEXT[x.art], farbe: '#5ef2ff' }); break;
    }
    if (s.weg) continue;
    for (const u of ufos) {
      if (u.weg) continue;
      const r = u.s.r;
      const dx = (s.x - u.x) / r, dy = (s.y - u.y) / (r * 0.55);
      if (dx * dx + dy * dy < 1.15) {
        s.weg = true; trefferZahl++; u.hp--; u.blitz = 1;
        if (u.hp <= 0) { u.weg = true; ufoWeg(u); } else klang.treffer();
        break;
      }
    }
  }
  bomben = bomben.filter(b => !b.weg); extras = extras.filter(x => !x.weg); ufos = ufos.filter(u => !u.weg);
  schuesse = schuesse.filter(s => !s.weg);

  // Teilchen und Texte
  for (const t of teile) {
    t.t -= dt;
    if (t.art !== 'ring') { t.vy += (t.art === 'splitter' ? 40 : 18) * dt; t.x += t.vx * dt; t.y += t.vy * dt; t.vx *= 0.985; }
    if (t.rot !== undefined) t.rot += t.vr * dt;
  }
  teile = teile.filter(t => t.t > 0);
  for (const t of texte) { t.t -= dt; t.y -= 9 * dt; }
  texte = texte.filter(t => t.t > 0);

  // Startbildschirm: UFOs bleiben im Bild
  if (zustand !== 'spiel') for (const u of ufos) if (u.y > H * 0.6) { u.modus = 'flug'; neuesZiel(u); }
}

function schiessen() {
  const g = geschuetz;
  if (g.ueberhitzt > 0) return;
  const richtungen = g.dreifach > 0 ? [-0.16, 0, 0.16] : [0];
  schussZahl += richtungen.length;
  // Jeder Schuss heizt das Rohr auf. Wer etwa 3 Sekunden am Stück feuert, überhitzt.
  g.hitze += g.dreifach > 0 ? 0.12 : 0.17;
  if (g.hitze >= 1) {
    g.hitze = 1; g.ueberhitzt = 1.4; feuer = false; tasten.feuer = false;
    piep(320, 0.5, 'sawtooth', 0.04, 90);
    texte.push({ x: g.x, y: g.y - 14, t: 1.2, text: TEXT.ueberhitzt, farbe: '#ff6b4a' });
  }
  for (const d of richtungen) {
    const w = g.winkel + d, l = 8.5;
    schuesse.push({ x: g.x + Math.sin(w) * l, y: g.y - 2 - Math.cos(w) * l, vx: Math.sin(w) * 150, vy: -Math.cos(w) * 150 });
  }
  g.kuehl = g.dreifach > 0 ? 0.26 : 0.24; g.blitz = 1; klang.schuss();
}

// ---------- Zeichnen ----------
function zeichneUfo(u) {
  const s = u.s, r = s.r * e, mutter = u.sorte === 'mutter';
  ctx.save();
  ctx.translate(px(u.x), py(u.y)); ctx.rotate(u.rot || 0);
  // weiches Leuchten direkt unter der Scheibe
  ctx.save(); ctx.scale(1, 0.32);
  const kg = ctx.createRadialGradient(0, r * 0.75, 0, 0, r * 0.75, r * 1.05);
  kg.addColorStop(0, `rgba(${s.strahl},0.38)`); kg.addColorStop(1, `rgba(${s.strahl},0)`);
  ctx.fillStyle = kg; ctx.beginPath(); ctx.arc(0, r * 0.75, r * 1.05, 0, Math.PI * 2); ctx.fill();
  ctx.restore();
  // Kuppel(n) mit Alien
  const kuppeln = mutter ? [-0.45, 0, 0.45] : [0];
  for (const kx of kuppeln) {
    const kr = mutter ? r * 0.2 : r * 0.46, ky = mutter ? -r * 0.13 : -r * 0.12, kxp = kx * r;
    ctx.save(); ctx.beginPath(); ctx.ellipse(kxp, ky, kr, kr * 0.95, 0, Math.PI, 0); ctx.closePath(); ctx.clip();
    ctx.fillStyle = s.kuppel + '0.22)'; ctx.fillRect(kxp - kr, ky - kr, kr * 2, kr);
    // Alien: grüner Kopf mit zwei großen Augen
    const ar = kr * 0.42, ay = ky - kr * 0.32 + Math.sin(u.phase * 1.3) * kr * 0.04;
    ctx.fillStyle = '#7ce35a'; ctx.beginPath(); ctx.ellipse(kxp, ay, ar, ar * 1.12, 0, 0, Math.PI * 2); ctx.fill();
    ctx.fillRect(kxp - ar * 0.35, ay + ar * 0.8, ar * 0.7, ar * 0.9);
    ctx.fillStyle = '#0b1408';
    for (const ex of [-1, 1]) { ctx.beginPath(); ctx.ellipse(kxp + ex * ar * 0.42, ay - ar * 0.05, ar * 0.26, ar * 0.38, ex * 0.35, 0, Math.PI * 2); ctx.fill(); }
    ctx.fillStyle = 'rgba(255,255,255,0.8)';
    for (const ex of [-1, 1]) { ctx.beginPath(); ctx.arc(kxp + ex * ar * 0.36, ay - ar * 0.2, ar * 0.08, 0, Math.PI * 2); ctx.fill(); }
    // Glanz auf der Kuppel
    const gl = ctx.createLinearGradient(kxp - kr, ky - kr, kxp + kr, ky);
    gl.addColorStop(0, 'rgba(255,255,255,0.45)'); gl.addColorStop(0.35, 'rgba(255,255,255,0.05)'); gl.addColorStop(1, s.kuppel + '0.25)');
    ctx.fillStyle = gl; ctx.fillRect(kxp - kr, ky - kr, kr * 2, kr);
    ctx.restore();
    ctx.strokeStyle = s.kuppel + '0.7)'; ctx.lineWidth = Math.max(1, r * 0.03);
    ctx.beginPath(); ctx.ellipse(kxp, ky, kr, kr * 0.95, 0, Math.PI, 0); ctx.stroke();
  }
  // Untere Schale
  ctx.fillStyle = s.metall[2];
  ctx.beginPath(); ctx.ellipse(0, r * 0.13, r * (mutter ? 0.75 : 0.55), r * 0.2, 0, 0, Math.PI); ctx.fill();
  // Scheibe
  const mg = ctx.createLinearGradient(0, -r * 0.25, 0, r * 0.25);
  mg.addColorStop(0, s.metall[0]); mg.addColorStop(0.55, s.metall[1]); mg.addColorStop(1, s.metall[2]);
  ctx.fillStyle = mg; ctx.beginPath(); ctx.ellipse(0, 0, r, r * (mutter ? 0.22 : 0.27), 0, 0, Math.PI * 2); ctx.fill();
  ctx.strokeStyle = 'rgba(0,0,0,0.35)'; ctx.lineWidth = Math.max(1, r * 0.035);
  ctx.beginPath(); ctx.ellipse(0, r * 0.02, r * 0.97, r * (mutter ? 0.12 : 0.15), 0, 0, Math.PI); ctx.stroke();
  // Randlichter, laufen reihum
  const n = mutter ? 15 : 7;
  for (let i = 0; i < n; i++) {
    const a = Math.PI * (0.08 + 0.84 * i / (n - 1));
    const lx = -Math.cos(a) * r * 0.86, ly = Math.sin(a) * r * (mutter ? 0.13 : 0.16) + r * 0.02;
    const an = (Math.sin(u.phase * 2.2 - i * 0.9) + 1) / 2;
    const f = s.licht[i % s.licht.length];
    ctx.globalAlpha = 0.35 + an * 0.65;
    const lr = r * (mutter ? 0.035 : 0.075);
    const lg = ctx.createRadialGradient(lx, ly, 0, lx, ly, lr * 3);
    lg.addColorStop(0, f); lg.addColorStop(1, 'rgba(0,0,0,0)');
    ctx.fillStyle = lg; ctx.beginPath(); ctx.arc(lx, ly, lr * 3, 0, Math.PI * 2); ctx.fill();
    ctx.fillStyle = '#fff'; ctx.beginPath(); ctx.arc(lx, ly, lr * 0.6, 0, Math.PI * 2); ctx.fill();
  }
  ctx.globalAlpha = 1;
  // Treffer-Blitz
  if (u.blitz > 0) {
    ctx.globalAlpha = u.blitz * 0.7; ctx.fillStyle = '#ffffff';
    ctx.beginPath(); ctx.ellipse(0, 0, r, r * 0.3, 0, 0, Math.PI * 2); ctx.fill(); ctx.globalAlpha = 1;
  }
  ctx.restore();
  // Lebensbalken: Mutterschiff groß, sonst klein bei mehr als einem Leben
  if (!mutter && u.s.hp > 1 && u.hp < u.s.hp) {
    ctx.fillStyle = 'rgba(255,255,255,0.18)'; ctx.fillRect(px(u.x) - r * 0.5, py(u.y) - r * 0.75, r, 3);
    ctx.fillStyle = s.licht[0]; ctx.fillRect(px(u.x) - r * 0.5, py(u.y) - r * 0.75, r * u.hp / u.s.hp, 3);
  }
}

function zeichneGeschuetz() {
  const g = geschuetz, x = px(g.x), y = py(g.y), r = 6 * e;
  if (zustand === 'vorbei') return;   // nach dem letzten Treffer ist das Geschütz zerstört
  if (g.unverwundbar > 0 && Math.floor(g.unverwundbar * 10) % 2 === 0) return;
  // Rohr
  ctx.save(); ctx.translate(x, y - 2 * e); ctx.rotate(g.winkel);
  const rg = ctx.createLinearGradient(-1.3 * e, 0, 1.3 * e, 0);
  rg.addColorStop(0, '#3d4650'); rg.addColorStop(0.5, '#c9d2da'); rg.addColorStop(1, '#3d4650');
  ctx.fillStyle = rg; ctx.fillRect(-1.2 * e, -8.5 * e, 2.4 * e, 7.5 * e);
  if (g.hitze > 0.45) { ctx.fillStyle = `rgba(255,70,40,${Math.min(0.85, (g.hitze - 0.45) * 1.6)})`; ctx.fillRect(-1.2 * e, -8.5 * e, 2.4 * e, 7.5 * e); }
  ctx.fillStyle = g.ueberhitzt > 0 ? '#ff5a3c' : '#5abe41'; ctx.fillRect(-1.45 * e, -8.8 * e, 2.9 * e, 1.1 * e);
  if (g.blitz > 0) {
    const fg = ctx.createRadialGradient(0, -9.5 * e, 0, 0, -9.5 * e, 3.5 * e * g.blitz);
    fg.addColorStop(0, 'rgba(255,255,220,0.95)'); fg.addColorStop(1, 'rgba(255,200,80,0)');
    ctx.fillStyle = fg; ctx.beginPath(); ctx.arc(0, -9.5 * e, 3.5 * e, 0, Math.PI * 2); ctx.fill();
  }
  ctx.restore();
  // Kuppel
  const kg = ctx.createLinearGradient(0, y - r, 0, y);
  kg.addColorStop(0, '#d9e1e8'); kg.addColorStop(0.6, '#6d7782'); kg.addColorStop(1, '#2b3138');
  ctx.fillStyle = kg; ctx.beginPath(); ctx.arc(x, y, r, Math.PI, 0); ctx.closePath(); ctx.fill();
  ctx.strokeStyle = '#5abe41'; ctx.lineWidth = Math.max(1.5, e * 0.35);
  ctx.beginPath(); ctx.arc(x, y, r * 0.72, Math.PI * 1.08, Math.PI * 1.92); ctx.stroke();
  ctx.fillStyle = '#1b2228'; ctx.fillRect(x - r * 1.35, y - e * 0.2, r * 2.7, e * 1.6);
  if (zustand === 'spiel' && (g.hitze > 0.02 || g.ueberhitzt > 0)) {
    const bw = r * 2.2, bx = x - bw / 2, by = y + e * 0.45, bh = Math.max(3, e * 0.5);
    ctx.fillStyle = 'rgba(255,255,255,0.12)'; ctx.fillRect(bx, by, bw, bh);
    const blink = g.ueberhitzt > 0 && Math.floor(zeit * 8) % 2 === 0;
    ctx.fillStyle = g.ueberhitzt > 0 ? (blink ? '#ff5a3c' : '#a8321f') : g.hitze > 0.7 ? '#ff7a3c' : g.hitze > 0.45 ? '#ffd34d' : '#7ef0a0';
    ctx.fillRect(bx, by, bw * g.hitze, bh);
  }
  // Schild
  if (g.schild > 0) {
    const a = g.schild < 2 ? (Math.sin(zeit * 20) + 1) / 2 : 1;
    ctx.strokeStyle = `rgba(94,242,255,${0.35 + 0.45 * a})`; ctx.lineWidth = 2;
    ctx.fillStyle = `rgba(94,242,255,${0.07 + 0.06 * a})`;
    ctx.beginPath(); ctx.arc(x, y - e, 9 * e, Math.PI, 0); ctx.closePath(); ctx.fill(); ctx.stroke();
  }
}

function zeichneHintergrund() {
  const hg = ctx.createLinearGradient(0, 0, 0, sh);
  hg.addColorStop(0, '#04060c'); hg.addColorStop(0.65, '#0a1020'); hg.addColorStop(1, '#12182a');
  ctx.fillStyle = hg; ctx.fillRect(0, 0, sw, sh);
  for (const s of sterne) {
    ctx.globalAlpha = s.g * (0.6 + 0.4 * Math.sin(s.p));
    ctx.fillStyle = '#dfe8ff'; ctx.fillRect(px(s.x), py(s.y), s.g > 0.45 ? 2 : 1.2, s.g > 0.45 ? 2 : 1.2);
  }
  ctx.globalAlpha = 1;
  // Mond mit Kratern
  const mx = px(W * 0.82), my = py(H * 0.16), mr = 5.5 * e;
  const hof = ctx.createRadialGradient(mx, my, mr * 0.8, mx, my, mr * 3.2);
  hof.addColorStop(0, 'rgba(220,232,255,0.14)'); hof.addColorStop(1, 'rgba(220,232,255,0)');
  ctx.fillStyle = hof; ctx.beginPath(); ctx.arc(mx, my, mr * 3.2, 0, Math.PI * 2); ctx.fill();
  const mond = ctx.createRadialGradient(mx - mr * 0.35, my - mr * 0.35, mr * 0.1, mx, my, mr);
  mond.addColorStop(0, '#f2f5fa'); mond.addColorStop(1, '#b9c3d2');
  ctx.fillStyle = mond; ctx.beginPath(); ctx.arc(mx, my, mr, 0, Math.PI * 2); ctx.fill();
  ctx.fillStyle = 'rgba(120,135,160,0.35)';
  for (const [kx, ky, kr] of [[-0.35, -0.2, 0.22], [0.3, 0.25, 0.16], [0.1, -0.45, 0.1], [-0.15, 0.45, 0.12], [0.45, -0.1, 0.08]]) {
    ctx.beginPath(); ctx.arc(mx + kx * mr, my + ky * mr, kr * mr, 0, Math.PI * 2); ctx.fill();
  }
}

function zeichneStadt() {
  const boden = py(H - 4);
  for (const h of stadt) {
    const x = px(h.x), y = py(H - 4 - h.h);
    ctx.fillStyle = '#0c1220'; ctx.fillRect(x, y, h.b * e, boden - y);
    for (const [fx, fy, z] of h.fenster) {
      ctx.fillStyle = `rgba(245,213,71,${0.25 + 0.35 * z})`;
      ctx.fillRect(x + fx * e, y + fy * e, Math.max(1.5, e * 0.8), Math.max(1.5, e * 0.9));
    }
  }
  ctx.fillStyle = '#070b14'; ctx.fillRect(0, boden, sw, sh - boden);
  ctx.fillStyle = 'rgba(90,190,65,0.55)'; ctx.fillRect(0, boden, sw, Math.max(1, e * 0.25));
}

function text(t, x, y, g, f, glow, align) {
  ctx.font = `700 ${g}px system-ui, -apple-system, "Segoe UI", sans-serif`;
  if (!align || align === 'center') {
    const breit = ctx.measureText(t).width, platz = sw * 0.94;
    if (breit > platz) { g = g * platz / breit; ctx.font = `700 ${g}px system-ui, -apple-system, "Segoe UI", sans-serif`; }
  }
  ctx.textAlign = align || 'center'; ctx.textBaseline = 'middle';
  ctx.shadowColor = f; ctx.shadowBlur = glow || 0; ctx.fillStyle = f; ctx.fillText(t, x, y); ctx.shadowBlur = 0;
}

function zeichneKnopf(k, art) {
  ctx.strokeStyle = 'rgba(200,225,255,0.65)'; ctx.lineWidth = 2;
  ctx.beginPath(); ctx.arc(k.x, k.y, k.r, 0, Math.PI * 2); ctx.stroke();
  const a = k.r * 0.4;
  ctx.beginPath();
  if (art === 'x') { ctx.moveTo(k.x - a, k.y - a); ctx.lineTo(k.x + a, k.y + a); ctx.moveTo(k.x + a, k.y - a); ctx.lineTo(k.x - a, k.y + a); ctx.stroke(); }
  else {
    ctx.fillStyle = 'rgba(200,225,255,0.85)';
    ctx.moveTo(k.x - a, k.y - a * 0.45); ctx.lineTo(k.x - a * 0.35, k.y - a * 0.45); ctx.lineTo(k.x + a * 0.25, k.y - a);
    ctx.lineTo(k.x + a * 0.25, k.y + a); ctx.lineTo(k.x - a * 0.35, k.y + a * 0.45); ctx.lineTo(k.x - a, k.y + a * 0.45); ctx.closePath(); ctx.fill();
    ctx.beginPath();
    if (tonAn) { ctx.arc(k.x + a * 0.35, k.y, a * 0.55, -0.9, 0.9); ctx.stroke(); }
    else { ctx.moveTo(k.x + a * 0.55, k.y - a * 0.4); ctx.lineTo(k.x + a * 1.1, k.y + a * 0.4); ctx.moveTo(k.x + a * 1.1, k.y - a * 0.4); ctx.lineTo(k.x + a * 0.55, k.y + a * 0.4); ctx.stroke(); }
  }
}

function zeichneListe(y, klein) {
  if (!runden.length) return y;
  text(TEXT.liste, sw / 2, y, klein * 0.75, '#8aa0c0', 0); y += klein * 1.25;
  runden.forEach((r, i) => {
    const jetzt = r.id === letzteRunde, f = jetzt ? '#ffffff' : '#a9c4e8', glow = jetzt ? 10 : 0;
    text(`${i + 1}.`, sw / 2 - klein * 1.6, y, klein * 0.9, f, glow, 'right');
    text(String(r.p), sw / 2 - klein * 1.1, y, klein * 0.9, f, glow, 'left');
    text(`${TEXT.welle} ${r.w}`, sw / 2 + klein * 1.9, y, klein * 0.75, '#7d8fae', 0, 'left');
    if (jetzt) text('◀', sw / 2 + klein * 6.2, y, klein * 0.8, f, glow, 'left');
    y += klein * 1.15;
  });
  return y;
}

function zeichnen() {
  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
  zeichneHintergrund();
  const wx = wackeln > 0 ? zufall(-1, 1) * wackeln * e * 0.8 : 0, wy = wackeln > 0 ? zufall(-1, 1) * wackeln * e * 0.8 : 0;
  ctx.save(); ctx.translate(wx, wy);
  zeichneStadt();
  for (const u of ufos) zeichneUfo(u);
  // Bomben
  for (const b of bomben) {
    const bx = px(b.x), by = py(b.y), br = 1.6 * e, p = (Math.sin(zeit * 18) + 1) / 2;
    const bg = ctx.createRadialGradient(bx, by, 0, bx, by, br * 2.6);
    bg.addColorStop(0, 'rgba(255,240,180,1)'); bg.addColorStop(0.35, 'rgba(255,122,60,0.9)'); bg.addColorStop(1, 'rgba(255,60,40,0)');
    ctx.fillStyle = bg; ctx.beginPath(); ctx.arc(bx, by, br * (2.2 + p * 0.6), 0, Math.PI * 2); ctx.fill();
  }
  // Extras
  for (const x of extras) {
    const ex = px(x.x), ey = py(x.y) + Math.sin(x.phase) * e * 0.6, er = 3.2 * e;
    const farbe = { schild: '94,242,255', dreifach: '255,160,60', zeitlupe: '199,125,255' }[x.art];
    const eg = ctx.createRadialGradient(ex, ey, 0, ex, ey, er * 2);
    eg.addColorStop(0, `rgba(${farbe},0.45)`); eg.addColorStop(1, `rgba(${farbe},0)`);
    ctx.fillStyle = eg; ctx.beginPath(); ctx.arc(ex, ey, er * 2, 0, Math.PI * 2); ctx.fill();
    ctx.strokeStyle = `rgb(${farbe})`; ctx.lineWidth = 2; ctx.fillStyle = 'rgba(5,8,16,0.85)';
    ctx.beginPath(); ctx.arc(ex, ey, er, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
    ctx.fillStyle = `rgb(${farbe})`; ctx.strokeStyle = `rgb(${farbe})`; const a = er * 0.5;
    ctx.beginPath();
    if (x.art === 'schild') { ctx.moveTo(ex, ey - a); ctx.lineTo(ex + a * 0.85, ey - a * 0.55); ctx.lineTo(ex + a * 0.7, ey + a * 0.3); ctx.lineTo(ex, ey + a); ctx.lineTo(ex - a * 0.7, ey + a * 0.3); ctx.lineTo(ex - a * 0.85, ey - a * 0.55); ctx.closePath(); ctx.fill(); }
    else if (x.art === 'dreifach') { for (const d of [-0.5, 0, 0.5]) { ctx.moveTo(ex, ey + a); ctx.lineTo(ex + d * a * 1.6, ey - a); } ctx.stroke(); }
    else { ctx.moveTo(ex - a * 0.7, ey - a); ctx.lineTo(ex + a * 0.7, ey - a); ctx.lineTo(ex - a * 0.7, ey + a); ctx.lineTo(ex + a * 0.7, ey + a); ctx.closePath(); ctx.stroke(); }
  }
  // Schüsse
  ctx.lineCap = 'round';
  for (const s of schuesse) {
    const x = px(s.x), y = py(s.y), l = 0.018;
    ctx.strokeStyle = 'rgba(126,240,160,0.35)'; ctx.lineWidth = Math.max(3, e * 1.1);
    ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x - s.vx * l * e, y - s.vy * l * e); ctx.stroke();
    ctx.strokeStyle = '#eafff0'; ctx.lineWidth = Math.max(1.5, e * 0.4);
    ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x - s.vx * l * e * 0.7, y - s.vy * l * e * 0.7); ctx.stroke();
  }
  zeichneGeschuetz();
  // Teilchen
  for (const t of teile) {
    const a = Math.max(0, t.t / t.max);
    if (t.art === 'ring') {
      ctx.strokeStyle = `rgba(255,255,255,${a * 0.5})`; ctx.lineWidth = 2;
      ctx.beginPath(); ctx.arc(px(t.x), py(t.y), (1 - a) * t.g * e, 0, Math.PI * 2); ctx.stroke();
    } else if (t.art === 'splitter') {
      ctx.save(); ctx.translate(px(t.x), py(t.y)); ctx.rotate(t.rot); ctx.globalAlpha = a;
      ctx.fillStyle = '#9aa3ae'; ctx.fillRect(-t.g * e / 2, -t.g * e / 4, t.g * e, t.g * e / 2); ctx.restore(); ctx.globalAlpha = 1;
    } else {
      ctx.globalAlpha = a; ctx.fillStyle = t.farbe;
      ctx.beginPath(); ctx.arc(px(t.x), py(t.y), t.g * e * (0.5 + a * 0.5), 0, Math.PI * 2); ctx.fill(); ctx.globalAlpha = 1;
    }
  }
  const klein = Math.max(13, Math.min(sh * 0.032, sw * 0.042));
  for (const t of texte) { ctx.globalAlpha = Math.min(1, t.t * 2); text(t.text, px(t.x), py(t.y), klein * 0.95, t.farbe, 8); ctx.globalAlpha = 1; }
  ctx.restore();

  const gross = Math.min(sh * 0.12, sw * 0.2);
  zeichneKnopf(knopfTon(), 'ton');
  if (zustand === 'start') {
    text(TEXT.titel, sw / 2, sh * 0.2, gross * 0.85, '#ffffff', 22);
    text(TEXT.unter, sw / 2, sh * 0.2 + gross * 0.62, klein * 1.05, '#a9c4e8', 0);
    text(TEXT.start, sw / 2, sh * 0.2 + gross * 0.62 + klein * 2, klein, '#7ef0a0', 6);
    text(TEXT.hilfe, sw / 2, sh * 0.2 + gross * 0.62 + klein * 3.4, klein * 0.75, '#7d8fae', 0);
    text(TEXT.hilfe2, sw / 2, sh * 0.2 + gross * 0.62 + klein * 4.4, klein * 0.75, '#7d8fae', 0);
    let y = sh * 0.2 + gross * 0.62 + klein * 6;
    if (bestwert > 0) { text(`${TEXT.bestwert}: ${bestwert}`, sw / 2, y, klein * 0.85, '#a9c4e8', 0); y += klein * 1.8; }
    zeichneListe(y, klein * 0.85);
    return;
  }
  // Anzeige oben
  text(String(punkte), sw / 2, sh * 0.09, gross * 0.8, 'rgba(255,255,255,0.95)', 18);
  const mal = Math.min(4, 1 + Math.floor((combo - 1) / 4));
  if (comboZeit > 0 && mal > 1) text(`×${mal}`, sw / 2 + gross * 1.1, sh * 0.09, klein * 1.3, '#ffd34d', 10, 'left');
  text(`${TEXT.bestwert}: ${Math.max(bestwert, punkte)}`, sw / 2, sh * 0.09 + gross * 0.55, klein * 0.8, '#8aa0c0', 0);
  text(`${TEXT.welle} ${welle}`, 14, 22, klein * 0.95, '#a9c4e8', 0, 'left');
  for (let i = 0; i < leben; i++) {
    const lx = 22 + i * klein * 1.5, ly = 22 + klein * 1.5;
    ctx.fillStyle = '#5abe41'; ctx.beginPath(); ctx.arc(lx, ly + klein * 0.3, klein * 0.45, Math.PI, 0); ctx.fill();
    ctx.fillRect(lx - klein * 0.1, ly - klein * 0.45, klein * 0.2, klein * 0.5);
  }
  // aktive Extras als kleine Balken
  let ey = 22 + klein * 2.8;
  for (const [art, max, f] of [['schild', 10, '#5ef2ff'], ['dreifach', 10, '#ffa03c'], ['zeitlupe', 6, '#c77dff']]) {
    if (geschuetz[art] > 0) {
      text(TEXT[art], 14, ey, klein * 0.7, f, 0, 'left');
      ctx.fillStyle = 'rgba(255,255,255,0.15)'; ctx.fillRect(14, ey + klein * 0.55, klein * 5, 3);
      ctx.fillStyle = f; ctx.fillRect(14, ey + klein * 0.55, klein * 5 * geschuetz[art] / max, 3);
      ey += klein * 1.5;
    }
  }
  // Lebensbalken des Mutterschiffs unter der Anzeige
  const m = ufos.find(u => u.sorte === 'mutter');
  if (m && zustand === 'spiel') {
    const bw = Math.min(sw * 0.5, 360), bx = sw / 2 - bw / 2, by = sh * 0.09 + gross * 0.55 + klein * 2.2;
    ctx.fillStyle = 'rgba(255,255,255,0.12)'; ctx.fillRect(bx, by, bw, 6);
    ctx.fillStyle = '#ff4d6d'; ctx.fillRect(bx, by, bw * Math.max(0, m.hp) / m.max, 6);
  }
  if (zustand === 'spiel' || zustand === 'pause') {
    zeichneKnopf(knopfX(), 'x');
    text(TEXT.abbrechen, sw / 2, sh * 0.09 + gross * 0.55 + klein * 1.2, klein * 0.68, '#5d6f8c', 0);
  }
  if (banner) {
    ctx.globalAlpha = Math.min(1, banner.t * 2.5);
    text(banner.text, sw / 2, sh * 0.42, klein * 2, banner.farbe, 16);
    ctx.globalAlpha = 1;
  }
  if (zustand === 'pause') {
    ctx.fillStyle = 'rgba(3,5,10,0.6)'; ctx.fillRect(0, 0, sw, sh);
    text(TEXT.pause, sw / 2, sh * 0.42, klein * 2.4, '#ffffff', 16);
    text(TEXT.weiter, sw / 2, sh * 0.42 + klein * 2.4, klein, '#a9c4e8', 0);
  }
  if (zustand === 'vorbei') {
    const oben = sh * 0.26;
    const hoch = klein * (10.4 + (neuerBest ? 1.6 : 0) + (runden.length ? 1.9 + runden.length * 1.15 : 0));
    ctx.fillStyle = 'rgba(3,5,10,0.78)'; ctx.fillRect(0, oben, sw, hoch);
    let y = oben + klein * 1.7;
    text(TEXT.vorbei, sw / 2, y, klein * 2, '#ffffff', 16); y += klein * 1.9;
    text(`${TEXT.erreicht} ${welle} · ${rohPunkte}`, sw / 2, y, klein * 0.95, '#a9c4e8', 0); y += klein * 1.5;
    text(`${TEXT.schuesse} ${schussZahl} · ${TEXT.treffer} ${Math.round(quote() * 100)} % · ${TEXT.bonus} +${bonus}`, sw / 2, y, klein * 0.9, '#a9c4e8', 0); y += klein * 1.7;
    text(`${TEXT.gesamt}: ${punkte}`, sw / 2, y, klein * 1.4, '#ffffff', 10); y += klein * 1.9;
    if (neuerBest) { text(TEXT.neu, sw / 2, y, klein, '#ffd34d', 10); y += klein * 1.6; }
    if (runden.length) y = zeichneListe(y + klein * 0.3, klein) + klein * 0.3;
    if (sperre <= 0) text(TEXT.nochmal, sw / 2, y + klein * 0.4, klein * 0.9, '#7ef0a0', 0);
  }
}

let zuletzt = performance.now();
function schleife(jetzt) {
  const dt = Math.min(0.05, (jetzt - zuletzt) / 1000);
  zuletzt = jetzt;
  update(dt);
  zeichnen();
  requestAnimationFrame(schleife);
}

groesse();
bestwert = parseInt(laden(SPEICHER, '0'), 10) || 0;
runden = ladeRunden();
demoStart();
zustand = 'start';
requestAnimationFrame(schleife);
</script>
</body>
</html>

Download as a file

Keep building

A few ideas for your next message in the same chat:

Now and then, let a comet cross the sky that gives extra points if you hit it.
Let the mothership get angry in the second half of its health bar: faster, with more bombs.
Add a fourth kind of UFO that turns invisible and only shows up briefly.
Build a high score list with three letters for the name, saved in the browser.

← All games

↑ Back to top