Files
apmanageur/jeux/spaceinvaders.html
T

866 lines
27 KiB
HTML

<!DOCTYPE html>
<html lang="fr" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Space Invaders - AP Manageur Arcade</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; user-select: none; }
:root, html[data-theme="dark"] {
--bg: #071326;
--card-bg: #0d1d36;
--panel-box: #132746;
--border: #223c68;
--text: #eef4ff;
--muted: #9aa8c5;
--blue: #5ca7ff;
--blue-btn-text: #071326;
--canvas-bg: #020713;
--cannon-color: #34d399;
--laser-color: #38bdf8;
--bomb-color: #ff6b6b;
--shield-color: #22c55e;
--ufo-color: #f43f5e;
}
html[data-theme="light"] {
--bg: #f2f5fa;
--card-bg: #ffffff;
--panel-box: #e9eef7;
--border: #d2dcec;
--text: #15203a;
--muted: #5b6b8c;
--blue: #1e6ef0;
--blue-btn-text: #ffffff;
--canvas-bg: #0f172a;
--cannon-color: #10b981;
--laser-color: #0284c7;
--bomb-color: #ef4444;
--shield-color: #16a34a;
--ufo-color: #e11d48;
}
html[data-theme="winxp"] {
--bg: #ece9d8;
--card-bg: #ece9d8;
--panel-box: #ffffff;
--border: #7f9db9;
--text: #000000;
--muted: #444444;
--blue: #0055ea;
--blue-btn-text: #ffffff;
--canvas-bg: #000000;
--cannon-color: #00ff00;
--laser-color: #ffffff;
--bomb-color: #ff0000;
--shield-color: #00cc00;
--ufo-color: #ff00ff;
font-family: 'Tahoma', sans-serif !important;
}
html[data-theme="macos"][data-macos-mode="light"],
html[data-theme="macos"]:not([data-macos-mode="dark"]) {
--bg: #f5f5f7;
--card-bg: #ffffff;
--panel-box: #f2f2f7;
--border: rgba(0, 0, 0, 0.12);
--text: #1d1d1f;
--muted: #86868b;
--blue: #0071e3;
--blue-btn-text: #ffffff;
--canvas-bg: #1c1c1e;
--cannon-color: #34c759;
--laser-color: #5ac8fa;
--bomb-color: #ff3b30;
--shield-color: #30d158;
--ufo-color: #ff2d55;
}
html[data-theme="macos"][data-macos-mode="dark"] {
--bg: #1e1e1e;
--card-bg: #2d2d2d;
--panel-box: #3a3a3c;
--border: rgba(255, 255, 255, 0.15);
--text: #f5f5f7;
--muted: #98989d;
--blue: #0a84ff;
--blue-btn-text: #ffffff;
--canvas-bg: #000000;
--cannon-color: #30d158;
--laser-color: #64d2ff;
--bomb-color: #ff453a;
--shield-color: #32d74b;
--ufo-color: #ff375f;
}
html[data-theme="futuristic"] {
--bg: #050811;
--card-bg: #091124;
--panel-box: #0f1c3a;
--border: #00f0ff;
--text: #e6f6ff;
--muted: #64b5f6;
--blue: #00f0ff;
--blue-btn-text: #050811;
--canvas-bg: #020713;
--cannon-color: #00f0ff;
--laser-color: #38bdf8;
--bomb-color: #f43f5e;
--shield-color: #00f0ff;
--ufo-color: #e879f9;
}
html[data-theme="hacker"] {
--bg: #000000;
--card-bg: #051206;
--panel-box: #0a1f0c;
--border: #00ff41;
--text: #00ff41;
--muted: #008f11;
--blue: #00ff41;
--blue-btn-text: #000000;
--canvas-bg: #000000;
--cannon-color: #00ff41;
--laser-color: #a3e635;
--bomb-color: #ff0033;
--shield-color: #00ff41;
--ufo-color: #00ff41;
font-family: 'Courier New', monospace !important;
}
body {
background: var(--bg);
color: var(--text);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
overflow-x: hidden;
padding: 4px;
transition: background 0.3s ease, color 0.3s ease;
}
.game-card {
background: var(--card-bg);
border: 1px solid var(--border);
border-radius: 14px;
padding: 10px 12px;
box-shadow: 0 10px 30px rgba(0,0,0,0.5), 0 0 20px rgba(92,167,255,0.12);
display: flex;
flex-direction: column;
align-items: center;
max-width: 580px;
width: 100%;
}
.header-bar {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
margin-bottom: 10px;
padding: 6px 14px;
background: var(--panel-box);
border-radius: 8px;
border: 1px solid var(--border);
font-size: 13px;
}
.scores-wrap {
display: flex;
gap: 16px;
font-weight: 700;
font-family: monospace;
font-size: 14px;
}
.score-val { color: var(--blue); }
.lives-val { color: var(--cannon-color); font-size: 15px; }
.canvas-wrap {
position: relative;
background: var(--canvas-bg);
border: 2px solid var(--border);
border-radius: 10px;
overflow: hidden;
box-shadow: inset 0 0 20px rgba(0,0,0,0.7);
}
canvas {
display: block;
}
.overlay {
position: absolute;
inset: 0;
background: rgba(3, 7, 18, 0.88);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
z-index: 10;
backdrop-filter: blur(3px);
}
.overlay h2 {
font-size: 26px;
color: #ef4444;
letter-spacing: 2px;
text-transform: uppercase;
}
.overlay h2.win { color: #32d583; }
.btn {
background: var(--blue);
color: var(--blue-btn-text);
border: 0;
padding: 8px 22px;
border-radius: 6px;
font-weight: 700;
font-size: 13px;
cursor: pointer;
transition: all 0.15s ease;
}
.btn:hover { opacity: 0.9; transform: translateY(-1px); }
.touch-controls {
display: flex;
gap: 12px;
margin-top: 10px;
justify-content: center;
width: 100%;
max-width: 360px;
}
.touch-btn {
flex: 1;
height: 44px;
background: var(--panel-box);
border: 1px solid var(--border);
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
color: var(--text);
cursor: pointer;
user-select: none;
}
.touch-btn.fire {
background: var(--blue);
color: var(--blue-btn-text);
font-weight: 700;
font-size: 14px;
}
.touch-btn:active {
transform: scale(0.96);
}
.controls-hint {
margin-top: 8px;
font-size: 11px;
color: var(--muted);
text-align: center;
}
</style>
</head>
<body>
<div class="game-card">
<div class="header-bar">
<div>👾 <strong>Space Invaders</strong></div>
<div class="scores-wrap">
<div>SCORE: <span id="scoreEl" class="score-val">0000</span></div>
<div>HI: <span id="hiScoreEl" class="score-val">0000</span></div>
<div id="livesEl" class="lives-val">🚀🚀🚀</div>
</div>
</div>
<div class="canvas-wrap">
<canvas id="siCanvas" width="520" height="420"></canvas>
<div id="gameOverlay" class="overlay" style="display:none">
<h2 id="overlayTitle">GAME OVER</h2>
<div id="overlayScore" style="color:var(--blue);font-weight:700"></div>
<button id="overlayBtn" class="btn" onclick="initGame()">REJOUER</button>
</div>
</div>
<div class="touch-controls">
<div class="touch-btn" id="btnLeft">⬅️</div>
<div class="touch-btn fire" id="btnFire">🔥 TIR</div>
<div class="touch-btn" id="btnRight">➡️</div>
</div>
<div id="siInstructions" class="controls-hint">
⬅️ ➡️ ou A/D pour vous déplacer, Espace pour tirer. Détruisez tous les envahisseurs avant qu'ils n'atteignent le sol !
</div>
</div>
<script>
// Theme Management
function setTheme(theme, macosMode) {
if (!theme) theme = 'dark';
document.documentElement.dataset.theme = theme;
if (theme === 'macos' && macosMode) {
document.documentElement.dataset.macosMode = macosMode;
} else {
delete document.documentElement.dataset.macosMode;
}
}
const urlParams = new URLSearchParams(window.location.search);
setTheme(urlParams.get('theme') || 'dark', urlParams.get('macosMode'));
const I18N_SI = {
fr: {
game_over: "GAME OVER",
level_cleared: "VICTOIRE ! VAGUE DÉTRUITE 🎉",
replay_btn: "REJOUER",
next_level_btn: "VAGUE SUIVANTE ➡️",
instructions: "⬅️ ➡️ ou A/D pour vous déplacer, Espace pour tirer. Détruisez tous les envahisseurs avant qu'ils n'atteignent le sol !"
},
en: {
game_over: "GAME OVER",
level_cleared: "VICTORY! WAVE CLEARED 🎉",
replay_btn: "PLAY AGAIN",
next_level_btn: "NEXT WAVE ➡️",
instructions: "⬅️ ➡️ or A/D to move, Space to fire. Destroy all invaders before they land on Earth!"
}
};
let currentSiLang = 'fr';
function applySiLang(lang) {
currentSiLang = (lang && (lang.startsWith('en') ? 'en' : 'fr')) || 'fr';
const dict = I18N_SI[currentSiLang] || I18N_SI.fr;
const el = document.getElementById('siInstructions');
if (el) el.textContent = dict.instructions;
}
applySiLang(urlParams.get('lang'));
window.addEventListener('message', e => {
if (e.data && e.data.type === 'themeChange') setTheme(e.data.theme, e.data.macosMode);
if (e.data && e.data.type === 'langChange') applySiLang(e.data.lang);
});
// Sound Synthesizer
let audioCtx = null;
let marchPitchIndex = 0;
const MARCH_FREQS = [160, 140, 120, 100];
function playSound(type) {
try {
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
if (audioCtx.state === 'suspended') audioCtx.resume();
const now = audioCtx.currentTime;
if (type === 'laser') {
const osc = audioCtx.createOscillator();
const g = audioCtx.createGain();
osc.connect(g); g.connect(audioCtx.destination);
osc.type = 'square';
osc.frequency.setValueAtTime(880, now);
osc.frequency.exponentialRampToValueAtTime(220, now + 0.12);
g.gain.setValueAtTime(0.06, now);
g.gain.exponentialRampToValueAtTime(0.001, now + 0.12);
osc.start(now); osc.stop(now + 0.12);
} else if (type === 'invader_hit') {
const osc = audioCtx.createOscillator();
const g = audioCtx.createGain();
osc.connect(g); g.connect(audioCtx.destination);
osc.type = 'sawtooth';
osc.frequency.setValueAtTime(280, now);
osc.frequency.linearRampToValueAtTime(60, now + 0.15);
g.gain.setValueAtTime(0.08, now);
g.gain.exponentialRampToValueAtTime(0.001, now + 0.15);
osc.start(now); osc.stop(now + 0.15);
} else if (type === 'march') {
const osc = audioCtx.createOscillator();
const g = audioCtx.createGain();
osc.connect(g); g.connect(audioCtx.destination);
osc.type = 'sawtooth';
const f = MARCH_FREQS[marchPitchIndex];
marchPitchIndex = (marchPitchIndex + 1) % MARCH_FREQS.length;
osc.frequency.setValueAtTime(f, now);
g.gain.setValueAtTime(0.06, now);
g.gain.exponentialRampToValueAtTime(0.001, now + 0.08);
osc.start(now); osc.stop(now + 0.08);
} else if (type === 'player_hit') {
const osc = audioCtx.createOscillator();
const g = audioCtx.createGain();
osc.connect(g); g.connect(audioCtx.destination);
osc.type = 'sawtooth';
osc.frequency.setValueAtTime(180, now);
osc.frequency.linearRampToValueAtTime(40, now + 0.4);
g.gain.setValueAtTime(0.12, now);
g.gain.exponentialRampToValueAtTime(0.001, now + 0.4);
osc.start(now); osc.stop(now + 0.4);
} else if (type === 'ufo') {
const osc = audioCtx.createOscillator();
const g = audioCtx.createGain();
osc.connect(g); g.connect(audioCtx.destination);
osc.type = 'sine';
osc.frequency.setValueAtTime(500, now);
osc.frequency.linearRampToValueAtTime(900, now + 0.2);
g.gain.setValueAtTime(0.05, now);
g.gain.exponentialRampToValueAtTime(0.001, now + 0.2);
osc.start(now); osc.stop(now + 0.2);
}
} catch (e) {}
}
const canvas = document.getElementById('siCanvas');
const ctx = canvas.getContext('2d');
let score = 0;
let highScore = parseInt(localStorage.getItem('ap_si_hi') || '0', 10);
let lives = 3;
let isGameOver = false;
// Cannon
const player = {
x: canvas.width / 2 - 15,
y: canvas.height - 35,
w: 30,
h: 18,
speed: 4.5,
movingLeft: false,
movingRight: false,
canShoot: true
};
let bullets = [];
let enemyBombs = [];
let invaders = [];
let invaderDir = 1;
let invaderStepDown = false;
let marchTimer = 0;
let marchInterval = 45; // frames between march ticks
// Bunkers / Shields
let shields = [];
// UFO Mystery Ship
let ufo = null;
let ufoTimer = 600;
function initShields() {
shields = [];
const numShields = 4;
const shieldW = 44;
const shieldH = 26;
const spacing = (canvas.width - numShields * shieldW) / (numShields + 1);
for (let i = 0; i < numShields; i++) {
const sx = spacing + i * (shieldW + spacing);
const sy = canvas.height - 85;
// Block pixels 4x4
for (let bx = 0; bx < shieldW; bx += 4) {
for (let by = 0; by < shieldH; by += 4) {
// Cutout bottom arch
if (bx >= 12 && bx <= 28 && by >= 14) continue;
// Cutout top corners
if ((bx < 4 && by < 4) || (bx >= 40 && by < 4)) continue;
shields.push({ x: sx + bx, y: sy + by, w: 4, h: 4, health: 3 });
}
}
}
}
function initInvaders() {
invaders = [];
const rows = 5;
const cols = 10;
const startX = 40;
const startY = 45;
const gapX = 38;
const gapY = 26;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
let type = 1; // Squid (top)
let points = 30;
let color = '#ec4899';
if (r === 1 || r === 2) {
type = 2; // Crab (middle)
points = 20;
color = '#38bdf8';
} else if (r >= 3) {
type = 3; // Octopus (bottom)
points = 10;
color = '#a855f7';
}
invaders.push({
x: startX + c * gapX,
y: startY + r * gapY,
w: 24,
h: 18,
type,
points,
color,
alive: true,
frame: 0
});
}
}
invaderDir = 1;
marchInterval = 45;
}
function initGame() {
document.getElementById('gameOverlay').style.display = 'none';
score = 0;
lives = 3;
isGameOver = false;
bullets = [];
enemyBombs = [];
ufo = null;
ufoTimer = 500;
player.x = canvas.width / 2 - 15;
updateUI();
initShields();
initInvaders();
}
function updateUI() {
document.getElementById('scoreEl').textContent = String(score).padStart(4, '0');
document.getElementById('hiScoreEl').textContent = String(highScore).padStart(4, '0');
let hearts = '';
for (let i = 0; i < lives; i++) hearts += '🚀';
document.getElementById('livesEl').textContent = hearts || '💥';
}
function shootLaser() {
if (!isGameOver && player.canShoot) {
// Limit to 2 concurrent player bullets
if (bullets.length < 2) {
bullets.push({
x: player.x + player.w / 2 - 2,
y: player.y - 6,
w: 3,
h: 10,
speed: 8
});
playSound('laser');
player.canShoot = false;
setTimeout(() => { player.canShoot = true; }, 180);
}
}
}
function update() {
if (!isGameOver) {
// Player movement
if (player.movingLeft) player.x -= player.speed;
if (player.movingRight) player.x += player.speed;
player.x = Math.max(10, Math.min(canvas.width - player.w - 10, player.x));
// Player bullets
for (let i = bullets.length - 1; i >= 0; i--) {
const b = bullets[i];
b.y -= b.speed;
if (b.y < 0) {
bullets.splice(i, 1);
continue;
}
// Bullet vs Shield
let hitShield = false;
for (let s = shields.length - 1; s >= 0; s--) {
const sh = shields[s];
if (b.x < sh.x + sh.w && b.x + b.w > sh.x && b.y < sh.y + sh.h && b.y + b.h > sh.y) {
shields.splice(s, 1);
hitShield = true;
break;
}
}
if (hitShield) {
bullets.splice(i, 1);
continue;
}
// Bullet vs UFO
if (ufo && b.x < ufo.x + ufo.w && b.x + b.w > ufo.x && b.y < ufo.y + ufo.h && b.y + b.h > ufo.y) {
score += ufo.points;
playSound('invader_hit');
ufo = null;
bullets.splice(i, 1);
updateUI();
continue;
}
// Bullet vs Invaders
for (let j = 0; j < invaders.length; j++) {
const inv = invaders[j];
if (inv.alive && b.x < inv.x + inv.w && b.x + b.w > inv.x && b.y < inv.y + inv.h && b.y + b.h > inv.y) {
inv.alive = false;
bullets.splice(i, 1);
score += inv.points;
playSound('invader_hit');
if (score > highScore) {
highScore = score;
localStorage.setItem('ap_si_hi', highScore);
}
updateUI();
// Speed up march as aliens die
const aliveCount = invaders.filter(a => a.alive).length;
marchInterval = Math.max(6, Math.floor(aliveCount * 0.9) + 4);
// Wave cleared check
if (aliveCount === 0) {
gameOver(true);
}
break;
}
}
}
// Enemy bombs movement
for (let i = enemyBombs.length - 1; i >= 0; i--) {
const b = enemyBombs[i];
b.y += b.speed;
// Bomb vs Shield
let hitShield = false;
for (let s = shields.length - 1; s >= 0; s--) {
const sh = shields[s];
if (b.x < sh.x + sh.w && b.x + b.w > sh.x && b.y < sh.y + sh.h && b.y + b.h > sh.y) {
shields.splice(s, 1);
hitShield = true;
break;
}
}
if (hitShield) {
enemyBombs.splice(i, 1);
continue;
}
// Bomb vs Player
if (b.x < player.x + player.w && b.x + b.w > player.x && b.y < player.y + player.h && b.y + b.h > player.y) {
enemyBombs.splice(i, 1);
lives--;
playSound('player_hit');
updateUI();
if (lives <= 0) {
gameOver(false);
}
continue;
}
if (b.y > canvas.height) {
enemyBombs.splice(i, 1);
}
}
// Invaders march movement
marchTimer++;
if (marchTimer >= marchInterval) {
marchTimer = 0;
playSound('march');
let hitEdge = false;
const aliveInvaders = invaders.filter(inv => inv.alive);
aliveInvaders.forEach(inv => {
inv.frame = inv.frame === 0 ? 1 : 0;
if (invaderDir === 1 && inv.x + inv.w >= canvas.width - 15) hitEdge = true;
if (invaderDir === -1 && inv.x <= 15) hitEdge = true;
});
if (hitEdge) {
invaderDir *= -1;
aliveInvaders.forEach(inv => {
inv.y += 14;
// Check if invaded earth / shields
if (inv.y + inv.h >= player.y) {
gameOver(false);
}
});
} else {
aliveInvaders.forEach(inv => {
inv.x += invaderDir * 10;
});
}
// Random bomb drop from lowest invader of a random column
if (Math.random() < 0.45 && aliveInvaders.length > 0 && enemyBombs.length < 3) {
const randomInvader = aliveInvaders[Math.floor(Math.random() * aliveInvaders.length)];
enemyBombs.push({
x: randomInvader.x + randomInvader.w / 2,
y: randomInvader.y + randomInvader.h,
w: 3,
h: 8,
speed: 4
});
}
}
// UFO Management
ufoTimer--;
if (ufoTimer <= 0) {
if (!ufo && Math.random() < 0.5) {
ufo = {
x: -40,
y: 20,
w: 36,
h: 16,
speed: 2.2,
points: [100, 150, 200, 300][Math.floor(Math.random() * 4)]
};
playSound('ufo');
}
ufoTimer = 650;
}
if (ufo) {
ufo.x += ufo.speed;
if (ufo.x > canvas.width + 40) ufo = null;
}
}
draw();
requestAnimationFrame(update);
}
function gameOver(isWin) {
isGameOver = true;
const overlay = document.getElementById('gameOverlay');
const title = document.getElementById('overlayTitle');
const scoreTxt = document.getElementById('overlayScore');
const btn = document.getElementById('overlayBtn');
const dict = I18N_SI[currentSiLang] || I18N_SI.fr;
overlay.style.display = 'flex';
scoreTxt.textContent = `Score final : ${score}`;
if (isWin) {
title.textContent = dict.level_cleared;
title.className = 'win';
btn.textContent = dict.next_level_btn;
} else {
title.textContent = dict.game_over;
title.className = '';
btn.textContent = dict.replay_btn;
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Starfield background
ctx.fillStyle = 'rgba(255,255,255,0.4)';
for (let i = 0; i < 20; i++) {
ctx.fillRect((i * 47) % canvas.width, (i * 31 + (performance.now() * 0.02)) % canvas.height, 1.5, 1.5);
}
// Draw Shields
ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue('--shield-color').trim() || '#22c55e';
shields.forEach(s => {
ctx.fillRect(s.x, s.y, s.w, s.h);
});
// Draw Player Cannon
const cannonColor = getComputedStyle(document.documentElement).getPropertyValue('--cannon-color').trim() || '#34d399';
ctx.fillStyle = cannonColor;
// Base
ctx.fillRect(player.x, player.y + 6, player.w, 12);
// Mid turret
ctx.fillRect(player.x + 6, player.y + 2, player.w - 12, 6);
// Gun nozzle
ctx.fillRect(player.x + player.w / 2 - 2, player.y - 3, 4, 6);
// Draw Player Bullets
ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue('--laser-color').trim() || '#38bdf8';
bullets.forEach(b => {
ctx.fillRect(b.x, b.y, b.w, b.h);
});
// Draw Enemy Bombs
ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue('--bomb-color').trim() || '#ff6b6b';
enemyBombs.forEach(b => {
ctx.fillRect(b.x, b.y, b.w, b.h);
// Little zigzag bomb tip
ctx.fillRect(b.x - 1, b.y + 2, 5, 2);
});
// Draw Invaders
invaders.forEach(inv => {
if (!inv.alive) return;
ctx.fillStyle = inv.color;
const x = inv.x, y = inv.y, w = inv.w, h = inv.h;
if (inv.type === 1) { // Squid
ctx.fillRect(x + 8, y, 8, 4);
ctx.fillRect(x + 4, y + 4, 16, 6);
ctx.fillRect(x + 6, y + 10, 12, 4);
if (inv.frame === 0) {
ctx.fillRect(x + 2, y + 14, 4, 4);
ctx.fillRect(x + 18, y + 14, 4, 4);
} else {
ctx.fillRect(x + 6, y + 14, 4, 4);
ctx.fillRect(x + 14, y + 14, 4, 4);
}
} else if (inv.type === 2) { // Crab
ctx.fillRect(x + 4, y, 16, 4);
ctx.fillRect(x + 2, y + 4, 20, 8);
if (inv.frame === 0) {
ctx.fillRect(x, y + 12, 4, 6);
ctx.fillRect(x + 20, y + 12, 4, 6);
} else {
ctx.fillRect(x + 4, y + 12, 4, 6);
ctx.fillRect(x + 16, y + 12, 4, 6);
}
} else { // Octopus
ctx.fillRect(x + 6, y, 12, 4);
ctx.fillRect(x + 2, y + 4, 20, 8);
ctx.fillRect(x + 4, y + 12, 16, 4);
if (inv.frame === 0) {
ctx.fillRect(x + 2, y + 14, 3, 4);
ctx.fillRect(x + 19, y + 14, 3, 4);
} else {
ctx.fillRect(x + 7, y + 14, 3, 4);
ctx.fillRect(x + 14, y + 14, 3, 4);
}
}
});
// Draw UFO
if (ufo) {
ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue('--ufo-color').trim() || '#f43f5e';
ctx.beginPath();
ctx.ellipse(ufo.x + ufo.w / 2, ufo.y + ufo.h / 2, ufo.w / 2, ufo.h / 2, 0, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = '#ffffff';
ctx.fillRect(ufo.x + ufo.w / 2 - 4, ufo.y + 2, 8, 4);
}
}
// Keyboard controls
window.addEventListener('keydown', e => {
if (['ArrowLeft', 'ArrowRight', ' '].includes(e.key)) e.preventDefault();
if (e.key === 'ArrowLeft' || e.key === 'q' || e.key === 'Q' || e.key === 'a' || e.key === 'A') player.movingLeft = true;
else if (e.key === 'ArrowRight' || e.key === 'd' || e.key === 'D') player.movingRight = true;
else if (e.key === ' ') shootLaser();
});
window.addEventListener('keyup', e => {
if (e.key === 'ArrowLeft' || e.key === 'q' || e.key === 'Q' || e.key === 'a' || e.key === 'A') player.movingLeft = false;
else if (e.key === 'ArrowRight' || e.key === 'd' || e.key === 'D') player.movingRight = false;
});
// Touch controls
const bLeft = document.getElementById('btnLeft');
const bRight = document.getElementById('btnRight');
const bFire = document.getElementById('btnFire');
bLeft.addEventListener('pointerdown', e => { e.preventDefault(); player.movingLeft = true; });
bLeft.addEventListener('pointerup', e => { e.preventDefault(); player.movingLeft = false; });
bLeft.addEventListener('pointerleave', e => { player.movingLeft = false; });
bRight.addEventListener('pointerdown', e => { e.preventDefault(); player.movingRight = true; });
bRight.addEventListener('pointerup', e => { e.preventDefault(); player.movingRight = false; });
bRight.addEventListener('pointerleave', e => { player.movingRight = false; });
bFire.addEventListener('pointerdown', e => { e.preventDefault(); shootLaser(); });
initGame();
update();
</script>
</body>
</html>