返回列表 发布新帖

网页版魂斗罗游戏,梦回童年!

2805 0
小K网牛逼 发表于 2026-7-3 00:00:25 | 查看全部 阅读模式 <

马上注册,结交更多好友,享用更多功能,让你轻松玩转小K网。

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

×
想起小时候玩的一款游戏,那时候还是游戏机玩的,魂斗罗,真的那时候玩的超级有意思,每天放学自己玩。
试着写了一版本地运行的纯HTML页面,玩了一下挺好。
操作方式
按键           功能
← → / A D        移动
Space / ↑ / W        跳跃
Z        射击
X        炸弹
↓ / S        趴下 / 空中向下瞄准
Enter        开始 / 重新开始

网页版魂斗罗游戏,梦回童年!

网页版魂斗罗游戏,梦回童年!

网页版魂斗罗游戏,梦回童年!

网页版魂斗罗游戏,梦回童年!

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>魂斗罗-单机版</title>
  7. <style>
  8. * { margin: 0; padding: 0; box-sizing: border-box; }
  9. html, body { width: 100%; height: 100%; overflow: hidden; background: #000; display: flex; align-items: center; justify-content: center; }
  10. canvas { display: block; image-rendering: pixelated; image-rendering: crisp-edges; background: #000; }
  11. </style>
  12. </head>
  13. <body>
  14. <canvas id="game"></canvas>
  15. <script>
  16. // ==================== CONTRA GAME ====================
  17. const canvas = document.getElementById('game');
  18. const ctx = canvas.getContext('2d');
  19. // --- Responsive Canvas ---
  20. const GAME_W = 800, GAME_H = 480;
  21. canvas.width = GAME_W; canvas.height = GAME_H;
  22. function resize() {
  23.   const s = Math.min(window.innerWidth / GAME_W, window.innerHeight / GAME_H);
  24.   canvas.style.width = (GAME_W * s) + 'px';
  25.   canvas.style.height = (GAME_H * s) + 'px';
  26. }
  27. window.addEventListener('resize', resize); resize();
  28. // --- Audio Engine ---
  29. const AudioCtx = window.AudioContext || window.webkitAudioContext;
  30. let audioCtx;
  31. function ensureAudio() { if (!audioCtx) audioCtx = new AudioCtx(); }
  32. function playSound(type) {
  33.   ensureAudio();
  34.   const o = audioCtx.createOscillator();
  35.   const g = audioCtx.createGain();
  36.   o.connect(g); g.connect(audioCtx.destination);
  37.   const t = audioCtx.currentTime;
  38.   switch(type) {
  39.     case 'shoot':
  40.       o.type='square'; o.frequency.setValueAtTime(800,t); o.frequency.exponentialRampToValueAtTime(200,t+0.08);
  41.       g.gain.setValueAtTime(0.15,t); g.gain.exponentialRampToValueAtTime(0.001,t+0.08);
  42.       o.start(t); o.stop(t+0.08); break;
  43.     case 'spread':
  44.       o.type='sawtooth'; o.frequency.setValueAtTime(600,t); o.frequency.exponentialRampToValueAtTime(100,t+0.12);
  45.       g.gain.setValueAtTime(0.12,t); g.gain.exponentialRampToValueAtTime(0.001,t+0.12);
  46.       o.start(t); o.stop(t+0.12); break;
  47.     case 'explosion':
  48.       o.type='sawtooth'; o.frequency.setValueAtTime(200,t); o.frequency.exponentialRampToValueAtTime(30,t+0.3);
  49.       g.gain.setValueAtTime(0.2,t); g.gain.exponentialRampToValueAtTime(0.001,t+0.3);
  50.       o.start(t); o.stop(t+0.3); break;
  51.     case 'powerup':
  52.       o.type='sine'; o.frequency.setValueAtTime(400,t); o.frequency.exponentialRampToValueAtTime(1200,t+0.2);
  53.       g.gain.setValueAtTime(0.15,t); g.gain.exponentialRampToValueAtTime(0.001,t+0.25);
  54.       o.start(t); o.stop(t+0.25); break;
  55.     case 'jump':
  56.       o.type='sine'; o.frequency.setValueAtTime(300,t); o.frequency.exponentialRampToValueAtTime(600,t+0.1);
  57.       g.gain.setValueAtTime(0.1,t); g.gain.exponentialRampToValueAtTime(0.001,t+0.1);
  58.       o.start(t); o.stop(t+0.1); break;
  59.     case 'hit':
  60.       o.type='square'; o.frequency.setValueAtTime(150,t); o.frequency.exponentialRampToValueAtTime(50,t+0.2);
  61.       g.gain.setValueAtTime(0.2,t); g.gain.exponentialRampToValueAtTime(0.001,t+0.2);
  62.       o.start(t); o.stop(t+0.2); break;
  63.     case 'boss':
  64.       o.type='sawtooth'; o.frequency.setValueAtTime(100,t); o.frequency.exponentialRampToValueAtTime(40,t+0.5);
  65.       g.gain.setValueAtTime(0.25,t); g.gain.exponentialRampToValueAtTime(0.001,t+0.5);
  66.       o.start(t); o.stop(t+0.5); break;
  67.   }
  68. }
  69. // --- Input ---
  70. const keys = {};
  71. window.addEventListener('keydown', e => { keys[e.code] = true; e.preventDefault(); });
  72. window.addEventListener('keyup', e => { keys[e.code] = false; e.preventDefault(); });
  73. // --- Game State ---
  74. let gameState = 'title'; // title, playing, gameover, win
  75. let score = 0, lives = 3, hiScore = parseInt(localStorage.getItem('contraHi') || '0');
  76. let cameraX = 0;
  77. const LEVEL_W = 6400;
  78. const GRAVITY = 0.5;
  79. let frameCount = 0;
  80. let screenShake = 0;
  81. let bossDefeated = false;
  82. // --- Platforms ---
  83. const platforms = [];
  84. function generateLevel() {
  85.   platforms.length = 0;
  86.   // Ground
  87.   for (let x = 0; x < LEVEL_W; x += 200) {
  88.     if (x > 400 && x < 600) continue; // gap
  89.     if (x > 1800 && x < 2000) continue; // gap
  90.     if (x > 3200 && x < 3400) continue; // gap
  91.     platforms.push({ x, y: GAME_H - 40, w: 200, h: 40, type: 'ground' });
  92.   }
  93.   // Floating platforms
  94.   const floats = [
  95.     {x:300,y:320,w:120},{x:500,y:280,w:100},{x:700,y:240,w:140},
  96.     {x:950,y:300,w:100},{x:1100,y:220,w:120},{x:1300,y:280,w:100},
  97.     {x:1500,y:200,w:140},{x:1700,y:320,w:100},{x:1900,y:260,w:120},
  98.     {x:2100,y:180,w:100},{x:2300,y:300,w:140},{x:2500,y:240,w:100},
  99.     {x:2700,y:320,w:120},{x:2900,y:200,w:100},{x:3100,y:280,w:140},
  100.     {x:3300,y:160,w:100},{x:3500,y:300,w:120},{x:3700,y:240,w:100},
  101.     {x:3900,y:320,w:140},{x:4100,y:200,w:100},{x:4300,y:280,w:120},
  102.     {x:4500,y:160,w:100},{x:4700,y:300,w:140},{x:4900,y:240,w:100},
  103.     {x:5100,y:320,w:120},{x:5300,y:200,w:100},{x:5500,y:280,w:140},
  104.     {x:5700,y:160,w:120},{x:5900,y:300,w:100}
  105.   ];
  106.   floats.forEach(f => platforms.push({ x:f.x, y:f.y, w:f.w, h:16, type:'platform' }));
  107.   // Walls/barriers
  108.   [800,1600,2400,3600,4800].forEach(x => {
  109.     platforms.push({ x, y:GAME_H-120, w:30, h:80, type:'wall' });
  110.   });
  111. }
  112. // --- Player ---
  113. const player = {
  114.   x: 100, y: 300, w: 28, h: 40, vx: 0, vy: 0,
  115.   dir: 1, onGround: false, jumping: false,
  116.   shooting: false, shootTimer: 0, shootCooldown: 8,
  117.   weapon: 'normal', // normal, spread, rapid, laser
  118.   weaponTimer: 0, invincible: 0,
  119.   prone: false, animFrame: 0, animTimer: 0,
  120.   alive: true, respawnTimer: 0
  121. };
  122. // --- Bullets ---
  123. let playerBullets = [];
  124. let enemyBullets = [];
  125. // --- Enemies ---
  126. let enemies = [];
  127. let boss = null;
  128. // --- Powerups ---
  129. let powerups = [];
  130. // --- Explosions ---
  131. let explosions = [];
  132. // --- Particles ---
  133. let particles = [];
  134. // --- Spawn enemies ---
  135. function spawnEnemies() {
  136.   enemies.length = 0;
  137.   // Soldiers
  138.   for (let i = 0; i < 40; i++) {
  139.     const ex = 600 + i * 140 + Math.random() * 60;
  140.     enemies.push({
  141.       type: 'soldier', x: ex, y: GAME_H - 80, w: 24, h: 36,
  142.       hp: 1, dir: -1, vx: 0, vy: 0, onGround: false,
  143.       shootTimer: Math.random() * 120, alive: true,
  144.       animFrame: 0, patrol: ex - 60, patrolMax: ex + 60,
  145.       state: 'patrol' // patrol, chase, shoot
  146.     });
  147.   }
  148.   // Turrets
  149.   [1000, 1800, 2600, 3400, 4200, 5000, 5600].forEach(tx => {
  150.     enemies.push({
  151.       type: 'turret', x: tx, y: GAME_H - 72, w: 32, h: 32,
  152.       hp: 3, dir: -1, shootTimer: 60, alive: true,
  153.       angle: 0
  154.     });
  155.   });
  156.   // Runners (fast enemies)
  157.   for (let i = 0; i < 15; i++) {
  158.     const ex = 1200 + i * 350 + Math.random() * 100;
  159.     enemies.push({
  160.       type: 'runner', x: ex, y: GAME_H - 76, w: 22, h: 34,
  161.       hp: 1, dir: -1, vx: -3, vy: 0, onGround: false,
  162.       alive: true, animFrame: 0
  163.     });
  164.   }
  165.   // Powerup drops
  166.   [700, 1400, 2200, 3000, 3800, 4600, 5400].forEach((px, i) => {
  167.     const types = ['spread','rapid','laser','life','shield'];
  168.     powerups.push({
  169.       x: px, y: 180 + Math.random() * 80, w: 24, h: 24,
  170.       type: types[i % types.length], alive: true, bobTimer: Math.random() * 100
  171.     });
  172.   });
  173. }
  174. // --- Boss ---
  175. function spawnBoss() {
  176.   boss = {
  177.     x: LEVEL_W - 300, y: GAME_H - 200, w: 120, h: 160,
  178.     hp: 50, maxHp: 50, phase: 0,
  179.     shootTimer: 0, moveTimer: 0, alive: true,
  180.     dir: -1, vy: 0
  181.   };
  182. }
  183. // --- Collision ---
  184. function rectCollide(a, b) {
  185.   return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
  186. }
  187. function resolveCollision(entity) {
  188.   entity.onGround = false;
  189.   for (const p of platforms) {
  190.     if (rectCollide(entity, p)) {
  191.       const overlapX = Math.min(entity.x + entity.w - p.x, p.x + p.w - entity.x);
  192.       const overlapY = Math.min(entity.y + entity.h - p.y, p.y + p.h - entity.y);
  193.       if (overlapY < overlapX) {
  194.         if (entity.y + entity.h/2 < p.y + p.h/2) {
  195.           entity.y = p.y - entity.h;
  196.           entity.vy = 0;
  197.           entity.onGround = true;
  198.         } else {
  199.           entity.y = p.y + p.h;
  200.           entity.vy = 0;
  201.         }
  202.       } else {
  203.         if (entity.x + entity.w/2 < p.x + p.w/2) {
  204.           entity.x = p.x - entity.w;
  205.         } else {
  206.           entity.x = p.x + p.w;
  207.         }
  208.         entity.vx = 0;
  209.       }
  210.     }
  211.   }
  212. }
  213. // --- Draw Helpers ---
  214. function drawPixelChar(x, y, dir, frame, prone, shooting) {
  215.   const sx = x - cameraX;
  216.   if (sx < -50 || sx > GAME_W + 50) return;
  217.   ctx.save();
  218.   ctx.translate(sx + 14, y);
  219.   ctx.scale(dir, 1);
  220.   if (prone) {
  221.     // Prone position
  222.     ctx.fillStyle = '#1e88e5';
  223.     ctx.fillRect(-14, 24, 28, 12); // body horizontal
  224.     ctx.fillStyle = '#ffcc80';
  225.     ctx.fillRect(10, 22, 10, 10); // head
  226.     ctx.fillStyle = '#1565c0';
  227.     ctx.fillRect(-14, 28, 28, 8); // legs
  228.     // Gun
  229.     ctx.fillStyle = '#757575';
  230.     ctx.fillRect(18, 26, 16, 4);
  231.   } else {
  232.     // Head
  233.     ctx.fillStyle = '#ffcc80';
  234.     ctx.fillRect(-6, 0, 12, 12);
  235.     // Hair/bandana
  236.     ctx.fillStyle = '#e53935';
  237.     ctx.fillRect(-7, 0, 14, 4);
  238.     ctx.fillRect(-9, 2, 4, 3); // bandana tail
  239.     // Eyes
  240.     ctx.fillStyle = '#000';
  241.     ctx.fillRect(2, 5, 3, 3);
  242.     // Body
  243.     ctx.fillStyle = '#1e88e5';
  244.     ctx.fillRect(-8, 12, 16, 14);
  245.     // Belt
  246.     ctx.fillStyle = '#795548';
  247.     ctx.fillRect(-8, 24, 16, 3);
  248.     // Legs
  249.     const legOff = Math.sin(frame * 0.3) * 4;
  250.     ctx.fillStyle = '#1565c0';
  251.     ctx.fillRect(-7, 27, 6, 13 + (frame % 2 === 0 ? legOff : -legOff));
  252.     ctx.fillRect(1, 27, 6, 13 + (frame % 2 === 0 ? -legOff : legOff));
  253.     // Boots
  254.     ctx.fillStyle = '#4e342e';
  255.     ctx.fillRect(-8, 37, 7, 3);
  256.     ctx.fillRect(1, 37, 7, 3);
  257.     // Gun arm
  258.     ctx.fillStyle = '#757575';
  259.     if (shooting) {
  260.       ctx.fillRect(8, 14, 18, 4);
  261.       // Muzzle flash
  262.       ctx.fillStyle = '#ffeb3b';
  263.       ctx.fillRect(26, 12, 6, 8);
  264.     } else {
  265.       ctx.fillRect(8, 16, 14, 3);
  266.     }
  267.     // Arm
  268.     ctx.fillStyle = '#ffcc80';
  269.     ctx.fillRect(6, 14, 4, 6);
  270.   }
  271.   ctx.restore();
  272. }
  273. function drawSoldier(e) {
  274.   const sx = e.x - cameraX;
  275.   if (sx < -50 || sx > GAME_W + 50) return;
  276.   ctx.save();
  277.   ctx.translate(sx + 12, e.y);
  278.   ctx.scale(e.dir, 1);
  279.   // Head
  280.   ctx.fillStyle = '#4caf50';
  281.   ctx.fillRect(-5, 0, 10, 10);
  282.   // Helmet
  283.   ctx.fillStyle = '#2e7d32';
  284.   ctx.fillRect(-6, -2, 12, 6);
  285.   // Eyes
  286.   ctx.fillStyle = '#f00';
  287.   ctx.fillRect(1, 4, 3, 2);
  288.   // Body
  289.   ctx.fillStyle = '#388e3c';
  290.   ctx.fillRect(-7, 10, 14, 14);
  291.   // Legs
  292.   const lo = Math.sin(e.animFrame * 0.2) * 3;
  293.   ctx.fillStyle = '#1b5e20';
  294.   ctx.fillRect(-6, 24, 5, 12 + lo);
  295.   ctx.fillRect(1, 24, 5, 12 - lo);
  296.   // Gun
  297.   ctx.fillStyle = '#616161';
  298.   ctx.fillRect(7, 14, 12, 3);
  299.   ctx.restore();
  300. }
  301. function drawTurret(e) {
  302.   const sx = e.x - cameraX;
  303.   if (sx < -50 || sx > GAME_W + 50) return;
  304.   // Base
  305.   ctx.fillStyle = '#616161';
  306.   ctx.fillRect(sx, e.y + 16, 32, 16);
  307.   // Dome
  308.   ctx.fillStyle = '#757575';
  309.   ctx.beginPath();
  310.   ctx.arc(sx + 16, e.y + 16, 14, Math.PI, 0);
  311.   ctx.fill();
  312.   // Barrel
  313.   ctx.save();
  314.   ctx.translate(sx + 16, e.y + 14);
  315.   ctx.rotate(e.angle);
  316.   ctx.fillStyle = '#424242';
  317.   ctx.fillRect(0, -3, 24, 6);
  318.   ctx.restore();
  319.   // Light
  320.   ctx.fillStyle = frameCount % 30 < 15 ? '#f44336' : '#ff8a80';
  321.   ctx.fillRect(sx + 13, e.y + 6, 6, 4);
  322. }
  323. function drawRunner(e) {
  324.   const sx = e.x - cameraX;
  325.   if (sx < -50 || sx > GAME_W + 50) return;
  326.   ctx.save();
  327.   ctx.translate(sx + 11, e.y);
  328.   ctx.scale(e.dir, 1);
  329.   // Head
  330.   ctx.fillStyle = '#ff6f00';
  331.   ctx.fillRect(-5, 0, 10, 10);
  332.   // Eyes
  333.   ctx.fillStyle = '#fff';
  334.   ctx.fillRect(1, 3, 4, 4);
  335.   ctx.fillStyle = '#f00';
  336.   ctx.fillRect(2, 4, 2, 2);
  337.   // Body
  338.   ctx.fillStyle = '#e65100';
  339.   ctx.fillRect(-6, 10, 12, 12);
  340.   // Legs (running fast)
  341.   const lo = Math.sin(e.animFrame * 0.5) * 6;
  342.   ctx.fillStyle = '#bf360c';
  343.   ctx.fillRect(-5, 22, 4, 12 + lo);
  344.   ctx.fillRect(1, 22, 4, 12 - lo);
  345.   // Claws
  346.   ctx.fillStyle = '#ff3d00';
  347.   ctx.fillRect(6, 12, 8, 3);
  348.   ctx.restore();
  349. }
  350. function drawBoss(b) {
  351.   if (!b || !b.alive) return;
  352.   const sx = b.x - cameraX;
  353.   ctx.save();
  354.   // Main body
  355.   ctx.fillStyle = '#455a64';
  356.   ctx.fillRect(sx, b.y, b.w, b.h);
  357.   // Armor plates
  358.   ctx.fillStyle = '#37474f';
  359.   ctx.fillRect(sx + 10, b.y + 10, b.w - 20, 30);
  360.   ctx.fillRect(sx + 10, b.y + 60, b.w - 20, 30);
  361.   ctx.fillRect(sx + 10, b.y + 110, b.w - 20, 30);
  362.   // Core (weak point)
  363.   const coreGlow = Math.sin(frameCount * 0.1) * 0.3 + 0.7;
  364.   ctx.fillStyle = `rgba(244, 67, 54, ${coreGlow})`;
  365.   ctx.beginPath();
  366.   ctx.arc(sx + b.w/2, b.y + b.h/2, 20, 0, Math.PI * 2);
  367.   ctx.fill();
  368.   ctx.fillStyle = '#ff8a80';
  369.   ctx.beginPath();
  370.   ctx.arc(sx + b.w/2, b.y + b.h/2, 10, 0, Math.PI * 2);
  371.   ctx.fill();
  372.   // Cannons
  373.   ctx.fillStyle = '#263238';
  374.   ctx.fillRect(sx - 20, b.y + 20, 30, 12);
  375.   ctx.fillRect(sx - 20, b.y + b.h - 40, 30, 12);
  376.   ctx.fillRect(sx + b.w - 10, b.y + 40, 30, 12);
  377.   ctx.fillRect(sx + b.w - 10, b.y + b.h - 60, 30, 12);
  378.   // Eyes
  379.   ctx.fillStyle = '#f44336';
  380.   ctx.fillRect(sx + 25, b.y + 15, 12, 8);
  381.   ctx.fillRect(sx + b.w - 37, b.y + 15, 12, 8);
  382.   // HP bar
  383.   ctx.fillStyle = '#333';
  384.   ctx.fillRect(sx, b.y - 20, b.w, 10);
  385.   ctx.fillStyle = b.hp > b.maxHp * 0.3 ? '#f44336' : '#ff1744';
  386.   ctx.fillRect(sx + 1, b.y - 19, (b.w - 2) * (b.hp / b.maxHp), 8);
  387.   // Name
  388.   ctx.fillStyle = '#fff';
  389.   ctx.font = '10px monospace';
  390.   ctx.fillText('BOSS - ALIEN CORE', sx + 10, b.y - 24);
  391.   ctx.restore();
  392. }
  393. // --- Background ---
  394. function drawBackground() {
  395.   // Sky gradient
  396.   const grad = ctx.createLinearGradient(0, 0, 0, GAME_H);
  397.   grad.addColorStop(0, '#0d1b2a');
  398.   grad.addColorStop(0.4, '#1b2838');
  399.   grad.addColorStop(0.7, '#2d4a3e');
  400.   grad.addColorStop(1, '#1a3a2a');
  401.   ctx.fillStyle = grad;
  402.   ctx.fillRect(0, 0, GAME_W, GAME_H);
  403.   // Stars
  404.   ctx.fillStyle = '#fff';
  405.   for (let i = 0; i < 60; i++) {
  406.     const sx = ((i * 137 + 50) % GAME_W + GAME_W - cameraX * 0.05 % GAME_W) % GAME_W;
  407.     const sy = (i * 89 + 20) % (GAME_H * 0.5);
  408.     const size = (i % 3 === 0) ? 2 : 1;
  409.     ctx.globalAlpha = 0.3 + (Math.sin(frameCount * 0.02 + i) * 0.3);
  410.     ctx.fillRect(sx, sy, size, size);
  411.   }
  412.   ctx.globalAlpha = 1;
  413.   // Mountains (parallax)
  414.   ctx.fillStyle = '#1a3328';
  415.   for (let i = 0; i < 12; i++) {
  416.     const mx = i * 200 - (cameraX * 0.15) % 2400;
  417.     const mh = 80 + (i * 47) % 60;
  418.     ctx.beginPath();
  419.     ctx.moveTo(mx, GAME_H - 40);
  420.     ctx.lineTo(mx + 100, GAME_H - 40 - mh);
  421.     ctx.lineTo(mx + 200, GAME_H - 40);
  422.     ctx.fill();
  423.   }
  424.   // Trees (parallax)
  425.   ctx.fillStyle = '#0d2818';
  426.   for (let i = 0; i < 20; i++) {
  427.     const tx = i * 150 - (cameraX * 0.3) % 3000;
  428.     const th = 40 + (i * 31) % 30;
  429.     ctx.fillRect(tx + 15, GAME_H - 40 - th, 10, th);
  430.     ctx.beginPath();
  431.     ctx.moveTo(tx, GAME_H - 40 - th + 10);
  432.     ctx.lineTo(tx + 20, GAME_H - 40 - th - 25);
  433.     ctx.lineTo(tx + 40, GAME_H - 40 - th + 10);
  434.     ctx.fill();
  435.   }
  436. }
  437. function drawPlatforms() {
  438.   for (const p of platforms) {
  439.     const sx = p.x - cameraX;
  440.     if (sx > GAME_W + 10 || sx + p.w < -10) continue;
  441.     if (p.type === 'ground') {
  442.       ctx.fillStyle = '#3e2723';
  443.       ctx.fillRect(sx, p.y, p.w, p.h);
  444.       ctx.fillStyle = '#4caf50';
  445.       ctx.fillRect(sx, p.y, p.w, 6);
  446.       ctx.fillStyle = '#2e7d32';
  447.       ctx.fillRect(sx, p.y + 6, p.w, 4);
  448.       // Dirt texture
  449.       ctx.fillStyle = '#5d4037';
  450.       for (let dx = 0; dx < p.w; dx += 20) {
  451.         ctx.fillRect(sx + dx + 5, p.y + 15, 8, 4);
  452.         ctx.fillRect(sx + dx + 2, p.y + 25, 6, 3);
  453.       }
  454.     } else if (p.type === 'platform') {
  455.       ctx.fillStyle = '#78909c';
  456.       ctx.fillRect(sx, p.y, p.w, p.h);
  457.       ctx.fillStyle = '#90a4ae';
  458.       ctx.fillRect(sx, p.y, p.w, 4);
  459.       ctx.fillStyle = '#546e7a';
  460.       ctx.fillRect(sx + 2, p.y + 6, p.w - 4, 4);
  461.       // Rivets
  462.       ctx.fillStyle = '#b0bec5';
  463.       ctx.fillRect(sx + 4, p.y + 2, 3, 3);
  464.       ctx.fillRect(sx + p.w - 7, p.y + 2, 3, 3);
  465.     } else if (p.type === 'wall') {
  466.       ctx.fillStyle = '#5d4037';
  467.       ctx.fillRect(sx, p.y, p.w, p.h);
  468.       ctx.fillStyle = '#6d4c41';
  469.       for (let dy = 0; dy < p.h; dy += 12) {
  470.         ctx.fillRect(sx, p.y + dy, p.w, 1);
  471.         ctx.fillRect(sx + p.w/2, p.y + dy, 1, 12);
  472.       }
  473.     }
  474.   }
  475. }
  476. function drawPowerups() {
  477.   for (const p of powerups) {
  478.     if (!p.alive) continue;
  479.     const sx = p.x - cameraX;
  480.     if (sx < -30 || sx > GAME_W + 30) continue;
  481.     const bob = Math.sin((frameCount + p.bobTimer) * 0.05) * 4;
  482.     const py = p.y + bob;
  483.     // Wing container
  484.     ctx.fillStyle = '#e53935';
  485.     ctx.fillRect(sx - 2, py - 2, 28, 28);
  486.     ctx.fillStyle = '#c62828';
  487.     ctx.fillRect(sx, py, 24, 24);
  488.     // Letter
  489.     ctx.fillStyle = '#fff';
  490.     ctx.font = 'bold 14px monospace';
  491.     const letters = { spread: 'S', rapid: 'R', laser: 'L', life: '1UP', shield: '&#128737;' };
  492.     ctx.fillText(letters[p.type] || '?', sx + 4, py + 17);
  493.     // Glow
  494.     ctx.globalAlpha = 0.3 + Math.sin(frameCount * 0.1) * 0.2;
  495.     ctx.fillStyle = '#ffeb3b';
  496.     ctx.fillRect(sx - 4, py - 4, 32, 32);
  497.     ctx.globalAlpha = 1;
  498.   }
  499. }
  500. function drawBullets() {
  501.   // Player bullets
  502.   for (const b of playerBullets) {
  503.     const sx = b.x - cameraX;
  504.     if (sx < -10 || sx > GAME_W + 10) continue;
  505.     if (b.type === 'laser') {
  506.       ctx.fillStyle = '#00e5ff';
  507.       ctx.fillRect(sx, b.y - 1, b.w, 3);
  508.       ctx.fillStyle = '#b2ebf2';
  509.       ctx.fillRect(sx, b.y, b.w, 1);
  510.     } else if (b.type === 'spread') {
  511.       ctx.fillStyle = '#ff9800';
  512.       ctx.beginPath();
  513.       ctx.arc(sx + b.w/2, b.y + b.h/2, 3, 0, Math.PI * 2);
  514.       ctx.fill();
  515.     } else {
  516.       ctx.fillStyle = '#ffeb3b';
  517.       ctx.fillRect(sx, b.y, b.w, b.h);
  518.       ctx.fillStyle = '#fff';
  519.       ctx.fillRect(sx + 1, b.y + 1, b.w - 2, b.h - 2);
  520.     }
  521.   }
  522.   // Enemy bullets
  523.   for (const b of enemyBullets) {
  524.     const sx = b.x - cameraX;
  525.     if (sx < -10 || sx > GAME_W + 10) continue;
  526.     ctx.fillStyle = '#f44336';
  527.     ctx.beginPath();
  528.     ctx.arc(sx + 3, b.y + 3, 4, 0, Math.PI * 2);
  529.     ctx.fill();
  530.     ctx.fillStyle = '#ff8a80';
  531.     ctx.beginPath();
  532.     ctx.arc(sx + 3, b.y + 3, 2, 0, Math.PI * 2);
  533.     ctx.fill();
  534.   }
  535. }
  536. function drawExplosions() {
  537.   for (let i = explosions.length - 1; i >= 0; i--) {
  538.     const e = explosions[i];
  539.     const sx = e.x - cameraX;
  540.     const progress = e.timer / e.maxTimer;
  541.     const r = e.radius * (1 - progress * 0.5);
  542.     ctx.globalAlpha = progress;
  543.     ctx.fillStyle = '#ff6f00';
  544.     ctx.beginPath(); ctx.arc(sx, e.y, r, 0, Math.PI * 2); ctx.fill();
  545.     ctx.fillStyle = '#ffeb3b';
  546.     ctx.beginPath(); ctx.arc(sx, e.y, r * 0.6, 0, Math.PI * 2); ctx.fill();
  547.     ctx.fillStyle = '#fff';
  548.     ctx.beginPath(); ctx.arc(sx, e.y, r * 0.2, 0, Math.PI * 2); ctx.fill();
  549.     ctx.globalAlpha = 1;
  550.     e.timer--;
  551.     if (e.timer <= 0) explosions.splice(i, 1);
  552.   }
  553. }
  554. function drawParticles() {
  555.   for (let i = particles.length - 1; i >= 0; i--) {
  556.     const p = particles[i];
  557.     const sx = p.x - cameraX;
  558.     ctx.globalAlpha = p.life / p.maxLife;
  559.     ctx.fillStyle = p.color;
  560.     ctx.fillRect(sx, p.y, p.size, p.size);
  561.     p.x += p.vx; p.y += p.vy; p.vy += 0.1; p.life--;
  562.     if (p.life <= 0) particles.splice(i, 1);
  563.   }
  564.   ctx.globalAlpha = 1;
  565. }
  566. function drawHUD() {
  567.   // Score
  568.   ctx.fillStyle = 'rgba(0,0,0,0.5)';
  569.   ctx.fillRect(0, 0, GAME_W, 36);
  570.   ctx.fillStyle = '#fff';
  571.   ctx.font = 'bold 16px monospace';
  572.   ctx.fillText(`SCORE: ${score.toString().padStart(8, '0')}`, 10, 24);
  573.   ctx.fillText(`HI: ${hiScore.toString().padStart(8, '0')}`, 220, 24);
  574.   // Lives
  575.   ctx.fillStyle = '#e53935';
  576.   for (let i = 0; i < lives; i++) {
  577.     ctx.fillRect(460 + i * 22, 10, 14, 16);
  578.     ctx.fillStyle = '#ffcc80';
  579.     ctx.fillRect(463 + i * 22, 6, 8, 8);
  580.     ctx.fillStyle = '#e53935';
  581.   }
  582.   // Weapon
  583.   ctx.fillStyle = '#ffeb3b';
  584.   ctx.font = '12px monospace';
  585.   const wNames = { normal: 'NORMAL', spread: 'SPREAD', rapid: 'RAPID', laser: 'LASER' };
  586.   ctx.fillText(`WEAPON: ${wNames[player.weapon]}`, 580, 24);
  587.   // Weapon timer
  588.   if (player.weaponTimer > 0 && player.weapon !== 'normal') {
  589.     ctx.fillStyle = '#333';
  590.     ctx.fillRect(580, 28, 80, 4);
  591.     ctx.fillStyle = '#ffeb3b';
  592.     ctx.fillRect(580, 28, 80 * (player.weaponTimer / 600), 4);
  593.   }
  594.   // Progress bar
  595.   ctx.fillStyle = '#333';
  596.   ctx.fillRect(GAME_W - 160, 14, 150, 8);
  597.   ctx.fillStyle = '#4caf50';
  598.   ctx.fillRect(GAME_W - 160, 14, 150 * Math.min(1, cameraX / (LEVEL_W - GAME_W)), 8);
  599.   ctx.fillStyle = '#fff';
  600.   ctx.font = '8px monospace';
  601.   ctx.fillText('PROGRESS', GAME_W - 145, 12);
  602. }
  603. // --- Title Screen ---
  604. function drawTitle() {
  605.   // Background
  606.   ctx.fillStyle = '#0a0a0a';
  607.   ctx.fillRect(0, 0, GAME_W, GAME_H);
  608.   // Animated background lines
  609.   ctx.strokeStyle = 'rgba(229, 57, 53, 0.1)';
  610.   ctx.lineWidth = 1;
  611.   for (let i = 0; i < 20; i++) {
  612.     const y = (i * 30 + frameCount * 0.5) % GAME_H;
  613.     ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(GAME_W, y); ctx.stroke();
  614.   }
  615.   // Title
  616.   ctx.save();
  617.   const titleY = 100 + Math.sin(frameCount * 0.03) * 8;
  618.   ctx.fillStyle = '#e53935';
  619.   ctx.font = 'bold 72px monospace';
  620.   ctx.textAlign = 'center';
  621.   ctx.fillText('CONTRA', GAME_W / 2, titleY);
  622.   // Title shadow
  623.   ctx.fillStyle = '#b71c1c';
  624.   ctx.fillText('CONTRA', GAME_W / 2 + 3, titleY + 3);
  625.   // Subtitle
  626.   ctx.fillStyle = '#ffeb3b';
  627.   ctx.font = 'bold 20px monospace';
  628.   ctx.fillText('魂 斗 罗', GAME_W / 2, titleY + 40);
  629.   // Blinking text
  630.   if (frameCount % 60 < 40) {
  631.     ctx.fillStyle = '#fff';
  632.     ctx.font = '18px monospace';
  633.     ctx.fillText('PRESS ENTER TO START', GAME_W / 2, 280);
  634.   }
  635.   // Controls
  636.   ctx.fillStyle = '#90a4ae';
  637.   ctx.font = '13px monospace';
  638.   ctx.fillText('← → : MOVE    ↑ : AIM UP    ↓ : PRONE', GAME_W / 2, 340);
  639.   ctx.fillText('SPACE : JUMP    Z : SHOOT    X : BOMB', GAME_W / 2, 365);
  640.   // Hi-Score
  641.   ctx.fillStyle = '#ffeb3b';
  642.   ctx.font = '14px monospace';
  643.   ctx.fillText(`HI-SCORE: ${hiScore.toString().padStart(8, '0')}`, GAME_W / 2, 420);
  644.   ctx.restore();
  645. }
  646. function drawGameOver() {
  647.   ctx.fillStyle = 'rgba(0,0,0,0.7)';
  648.   ctx.fillRect(0, 0, GAME_W, GAME_H);
  649.   ctx.save();
  650.   ctx.textAlign = 'center';
  651.   ctx.fillStyle = '#e53935';
  652.   ctx.font = 'bold 56px monospace';
  653.   ctx.fillText('GAME OVER', GAME_W / 2, GAME_H / 2 - 30);
  654.   ctx.fillStyle = '#fff';
  655.   ctx.font = '20px monospace';
  656.   ctx.fillText(`FINAL SCORE: ${score.toString().padStart(8, '0')}`, GAME_W / 2, GAME_H / 2 + 20);
  657.   if (frameCount % 60 < 40) {
  658.     ctx.fillStyle = '#ffeb3b';
  659.     ctx.font = '16px monospace';
  660.     ctx.fillText('PRESS ENTER TO CONTINUE', GAME_W / 2, GAME_H / 2 + 70);
  661.   }
  662.   ctx.restore();
  663. }
  664. function drawWin() {
  665.   ctx.fillStyle = 'rgba(0,0,0,0.7)';
  666.   ctx.fillRect(0, 0, GAME_W, GAME_H);
  667.   ctx.save();
  668.   ctx.textAlign = 'center';
  669.   ctx.fillStyle = '#4caf50';
  670.   ctx.font = 'bold 48px monospace';
  671.   ctx.fillText('MISSION COMPLETE!', GAME_W / 2, GAME_H / 2 - 40);
  672.   ctx.fillStyle = '#ffeb3b';
  673.   ctx.font = 'bold 24px monospace';
  674.   ctx.fillText(`SCORE: ${score.toString().padStart(8, '0')}`, GAME_W / 2, GAME_H / 2 + 10);
  675.   ctx.fillStyle = '#fff';
  676.   ctx.font = '16px monospace';
  677.   ctx.fillText('THE ALIEN CORE HAS BEEN DESTROYED!', GAME_W / 2, GAME_H / 2 + 50);
  678.   if (frameCount % 60 < 40) {
  679.     ctx.fillStyle = '#90a4ae';
  680.     ctx.font = '14px monospace';
  681.     ctx.fillText('PRESS ENTER TO PLAY AGAIN', GAME_W / 2, GAME_H / 2 + 90);
  682.   }
  683.   ctx.restore();
  684. }
  685. // --- Update Functions ---
  686. function updatePlayer() {
  687.   if (!player.alive) {
  688.     player.respawnTimer--;
  689.     if (player.respawnTimer <= 0) {
  690.       if (lives > 0) {
  691.         player.alive = true;
  692.         player.x = cameraX + 100;
  693.         player.y = 200;
  694.         player.vx = 0; player.vy = 0;
  695.         player.invincible = 120;
  696.         player.weapon = 'normal';
  697.       } else {
  698.         gameState = 'gameover';
  699.         if (score > hiScore) { hiScore = score; localStorage.setItem('contraHi', hiScore); }
  700.       }
  701.     }
  702.     return;
  703.   }
  704.   // Movement
  705.   const speed = 3;
  706.   if (keys['ArrowLeft'] || keys['KeyA']) { player.vx = -speed; player.dir = -1; }
  707.   else if (keys['ArrowRight'] || keys['KeyD']) { player.vx = speed; player.dir = 1; }
  708.   else { player.vx = 0; }
  709.   // Prone
  710.   player.prone = (keys['ArrowDown'] || keys['KeyS']) && player.onGround;
  711.   if (player.prone) { player.h = 20; player.vx *= 0.5; }
  712.   else { player.h = 40; }
  713.   // Jump
  714.   if ((keys['Space'] || keys['ArrowUp'] || keys['KeyW']) && player.onGround) {
  715.     player.vy = -10;
  716.     player.jumping = true;
  717.     player.onGround = false;
  718.     playSound('jump');
  719.   }
  720.   // Gravity
  721.   player.vy += GRAVITY;
  722.   player.x += player.vx;
  723.   player.y += player.vy;
  724.   // Resolve collisions
  725.   resolveCollision(player);
  726.   // Bounds
  727.   if (player.x < cameraX) player.x = cameraX;
  728.   if (player.y > GAME_H + 50) {
  729.     playerDie();
  730.     return;
  731.   }
  732.   // Shooting
  733.   if (player.shootTimer > 0) player.shootTimer--;
  734.   const cooldown = player.weapon === 'rapid' ? 4 : player.shootCooldown;
  735.   if (keys['KeyZ'] && player.shootTimer <= 0) {
  736.     shoot();
  737.     player.shootTimer = cooldown;
  738.   }
  739.   // Bomb (X key)
  740.   if (keys['KeyX'] && !player._bombUsed) {
  741.     player._bombUsed = true;
  742.     // Screen clear bomb
  743.     for (const e of enemies) {
  744.       if (e.alive && Math.abs(e.x - player.x) < 400) {
  745.         e.alive = false;
  746.         score += 100;
  747.         explosions.push({ x: e.x, y: e.y, radius: 30, timer: 20, maxTimer: 20 });
  748.       }
  749.     }
  750.     screenShake = 15;
  751.     playSound('explosion');
  752.   }
  753.   if (!keys['KeyX']) player._bombUsed = false;
  754.   // Invincibility
  755.   if (player.invincible > 0) player.invincible--;
  756.   // Weapon timer
  757.   if (player.weaponTimer > 0) {
  758.     player.weaponTimer--;
  759.     if (player.weaponTimer <= 0) player.weapon = 'normal';
  760.   }
  761.   // Animation
  762.   if (Math.abs(player.vx) > 0) {
  763.     player.animTimer++;
  764.     if (player.animTimer > 6) { player.animFrame++; player.animTimer = 0; }
  765.   }
  766.   // Camera
  767.   const targetCam = player.x - GAME_W * 0.35;
  768.   cameraX += (targetCam - cameraX) * 0.08;
  769.   cameraX = Math.max(0, Math.min(LEVEL_W - GAME_W, cameraX));
  770.   // Boss trigger
  771.   if (cameraX > LEVEL_W - GAME_W - 100 && !boss && !bossDefeated) {
  772.     spawnBoss();
  773.     playSound('boss');
  774.   }
  775. }
  776. function shoot() {
  777.   const bx = player.x + (player.dir > 0 ? player.w : -8);
  778.   const by = player.y + (player.prone ? 26 : 14);
  779.   let bvx = player.dir * 8;
  780.   let bvy = 0;
  781.   // Aim up
  782.   if (keys['ArrowUp'] && !player.onGround) {
  783.     bvy = -8;
  784.     bvx = player.dir * 4;
  785.   } else if (keys['ArrowUp'] && player.onGround && !player.prone) {
  786.     bvy = -8;
  787.     bvx = 0;
  788.   }
  789.   // Aim down (in air)
  790.   if (keys['ArrowDown'] && !player.onGround) {
  791.     bvy = 8;
  792.     bvx = player.dir * 4;
  793.   }
  794.   if (player.weapon === 'spread') {
  795.     playSound('spread');
  796.     for (let a = -0.3; a <= 0.3; a += 0.15) {
  797.       playerBullets.push({
  798.         x: bx, y: by, w: 6, h: 6,
  799.         vx: bvx * Math.cos(a) - bvy * Math.sin(a),
  800.         vy: bvx * Math.sin(a) + bvy * Math.cos(a),
  801.         type: 'spread', life: 60
  802.       });
  803.     }
  804.   } else if (player.weapon === 'laser') {
  805.     playSound('spread');
  806.     playerBullets.push({
  807.       x: bx, y: by, w: 30, h: 3,
  808.       vx: bvx * 1.5, vy: bvy * 1.5,
  809.       type: 'laser', life: 40
  810.     });
  811.   } else {
  812.     playSound('shoot');
  813.     playerBullets.push({
  814.       x: bx, y: by, w: 8, h: 3,
  815.       vx: bvx, vy: bvy,
  816.       type: 'normal', life: 80
  817.     });
  818.   }
  819.   player.shooting = true;
  820.   setTimeout(() => player.shooting = false, 80);
  821. }
  822. function playerDie() {
  823.   if (player.invincible > 0) return;
  824.   player.alive = false;
  825.   lives--;
  826.   player.respawnTimer = 90;
  827.   screenShake = 20;
  828.   playSound('hit');
  829.   explosions.push({ x: player.x, y: player.y + 20, radius: 25, timer: 25, maxTimer: 25 });
  830.   for (let i = 0; i < 15; i++) {
  831.     particles.push({
  832.       x: player.x + 14, y: player.y + 20,
  833.       vx: (Math.random() - 0.5) * 6, vy: (Math.random() - 0.5) * 6 - 3,
  834.       size: 2 + Math.random() * 3, color: ['#e53935','#ffeb3b','#ff9800'][Math.floor(Math.random()*3)],
  835.       life: 30 + Math.random() * 20, maxLife: 50
  836.     });
  837.   }
  838. }
  839. function updateBullets() {
  840.   // Player bullets
  841.   for (let i = playerBullets.length - 1; i >= 0; i--) {
  842.     const b = playerBullets[i];
  843.     b.x += b.vx; b.y += b.vy; b.life--;
  844.     if (b.life <= 0 || b.x < cameraX - 50 || b.x > cameraX + GAME_W + 50 || b.y < -20 || b.y > GAME_H + 20) {
  845.       playerBullets.splice(i, 1); continue;
  846.     }
  847.     // Hit enemies
  848.     for (const e of enemies) {
  849.       if (!e.alive) continue;
  850.       if (rectCollide(b, e)) {
  851.         e.hp--;
  852.         if (e.hp <= 0) {
  853.           e.alive = false;
  854.           score += e.type === 'turret' ? 300 : e.type === 'runner' ? 200 : 100;
  855.           explosions.push({ x: e.x + e.w/2, y: e.y + e.h/2, radius: 20, timer: 18, maxTimer: 18 });
  856.           playSound('explosion');
  857.           for (let j = 0; j < 8; j++) {
  858.             particles.push({
  859.               x: e.x + e.w/2, y: e.y + e.h/2,
  860.               vx: (Math.random()-0.5)*5, vy: (Math.random()-0.5)*5-2,
  861.               size: 2+Math.random()*2, color: ['#ff6f00','#ffeb3b','#fff'][Math.floor(Math.random()*3)],
  862.               life: 20+Math.random()*15, maxLife: 35
  863.             });
  864.           }
  865.         }
  866.         playerBullets.splice(i, 1); break;
  867.       }
  868.     }
  869.     // Hit boss
  870.     if (boss && boss.alive && rectCollide(b, boss)) {
  871.       boss.hp--;
  872.       if (boss.hp <= 0) {
  873.         boss.alive = false;
  874.         bossDefeated = true;
  875.         score += 5000;
  876.         screenShake = 30;
  877.         playSound('explosion');
  878.         for (let k = 0; k < 30; k++) {
  879.           explosions.push({
  880.             x: boss.x + Math.random() * boss.w,
  881.             y: boss.y + Math.random() * boss.h,
  882.             radius: 15 + Math.random() * 20,
  883.             timer: 20 + Math.random() * 20, maxTimer: 40
  884.           });
  885.         }
  886.         setTimeout(() => { gameState = 'win'; if (score > hiScore) { hiScore = score; localStorage.setItem('contraHi', hiScore); } }, 2000);
  887.       }
  888.       playerBullets.splice(i, 1);
  889.       playSound('hit');
  890.     }
  891.     // Hit platforms (walls only)
  892.     for (const p of platforms) {
  893.       if (p.type === 'wall' && rectCollide(b, p)) {
  894.         playerBullets.splice(i, 1); break;
  895.       }
  896.     }
  897.   }
  898.   // Enemy bullets
  899.   for (let i = enemyBullets.length - 1; i >= 0; i--) {
  900.     const b = enemyBullets[i];
  901.     b.x += b.vx; b.y += b.vy; b.life--;
  902.     if (b.life <= 0 || b.x < cameraX - 100 || b.x > cameraX + GAME_W + 100) {
  903.       enemyBullets.splice(i, 1); continue;
  904.     }
  905.     // Hit player
  906.     if (player.alive && rectCollide({ x: b.x, y: b.y, w: 6, h: 6 }, player)) {
  907.       playerDie();
  908.       enemyBullets.splice(i, 1);
  909.     }
  910.   }
  911. }
  912. function updateEnemies() {
  913.   for (const e of enemies) {
  914.     if (!e.alive) continue;
  915.     const dx = player.x - e.x;
  916.     const dist = Math.abs(dx);
  917.     if (e.type === 'soldier') {
  918.       // AI
  919.       if (dist < 350 && player.alive) {
  920.         e.dir = dx > 0 ? 1 : -1;
  921.         e.state = dist < 250 ? 'shoot' : 'chase';
  922.       } else {
  923.         e.state = 'patrol';
  924.       }
  925.       if (e.state === 'patrol') {
  926.         e.vx = e.dir * 0.8;
  927.         if (e.x <= e.patrol) e.dir = 1;
  928.         if (e.x >= e.patrolMax) e.dir = -1;
  929.       } else if (e.state === 'chase') {
  930.         e.vx = e.dir * 1.5;
  931.       } else {
  932.         e.vx = 0;
  933.         e.shootTimer--;
  934.         if (e.shootTimer <= 0 && dist < 400) {
  935.           e.shootTimer = 60 + Math.random() * 40;
  936.           const angle = Math.atan2(player.y - e.y, player.x - e.x);
  937.           enemyBullets.push({
  938.             x: e.x + 12, y: e.y + 14,
  939.             vx: Math.cos(angle) * 3, vy: Math.sin(angle) * 3,
  940.             life: 120
  941.           });
  942.         }
  943.       }
  944.       e.vy += GRAVITY;
  945.       e.x += e.vx;
  946.       e.y += e.vy;
  947.       resolveCollision(e);
  948.       e.animFrame++;
  949.       // Touch damage
  950.       if (player.alive && rectCollide(player, e)) playerDie();
  951.     } else if (e.type === 'turret') {
  952.       if (player.alive) {
  953.         e.angle = Math.atan2(player.y - e.y, player.x - e.x);
  954.         e.dir = dx > 0 ? 1 : -1;
  955.       }
  956.       e.shootTimer--;
  957.       if (e.shootTimer <= 0 && dist < 500) {
  958.         e.shootTimer = 80;
  959.         enemyBullets.push({
  960.           x: e.x + 16 + Math.cos(e.angle) * 24,
  961.           y: e.y + 14 + Math.sin(e.angle) * 24,
  962.           vx: Math.cos(e.angle) * 4, vy: Math.sin(e.angle) * 4,
  963.           life: 100
  964.         });
  965.       }
  966.     } else if (e.type === 'runner') {
  967.       if (dist < 500 && player.alive) {
  968.         e.dir = dx > 0 ? 1 : -1;
  969.         e.vx = e.dir * 3.5;
  970.       }
  971.       e.vy += GRAVITY;
  972.       e.x += e.vx;
  973.       e.y += e.vy;
  974.       resolveCollision(e);
  975.       e.animFrame++;
  976.       if (player.alive && rectCollide(player, e)) playerDie();
  977.     }
  978.   }
  979. }
  980. function updateBoss() {
  981.   if (!boss || !boss.alive) return;
  982.   boss.shootTimer--;
  983.   boss.moveTimer++;
  984.   // Movement
  985.   boss.y = GAME_H - 200 + Math.sin(boss.moveTimer * 0.02) * 40;
  986.   // Attack patterns
  987.   if (boss.shootTimer <= 0) {
  988.     boss.phase = (boss.phase + 1) % 3;
  989.     if (boss.phase === 0) {
  990.       // Spread shot
  991.       for (let a = -0.6; a <= 0.6; a += 0.2) {
  992.         enemyBullets.push({
  993.           x: boss.x + 20, y: boss.y + boss.h / 2,
  994.           vx: Math.cos(a + Math.PI) * 4, vy: Math.sin(a + Math.PI) * 4,
  995.           life: 150
  996.         });
  997.       }
  998.       boss.shootTimer = 40;
  999.     } else if (boss.phase === 1) {
  1000.       // Aimed shot
  1001.       const angle = Math.atan2(player.y - boss.y - boss.h/2, player.x - boss.x);
  1002.       for (let i = 0; i < 3; i++) {
  1003.         setTimeout(() => {
  1004.           if (boss && boss.alive) {
  1005.             enemyBullets.push({
  1006.               x: boss.x + 20, y: boss.y + boss.h / 2,
  1007.               vx: Math.cos(angle) * 5, vy: Math.sin(angle) * 5,
  1008.               life: 120
  1009.             });
  1010.           }
  1011.         }, i * 100);
  1012.       }
  1013.       boss.shootTimer = 60;
  1014.     } else {
  1015.       // Rain of bullets
  1016.       for (let i = 0; i < 5; i++) {
  1017.         enemyBullets.push({
  1018.           x: boss.x + Math.random() * boss.w,
  1019.           y: boss.y + boss.h,
  1020.           vx: (Math.random() - 0.5) * 3,
  1021.           vy: 2 + Math.random() * 3,
  1022.           life: 120
  1023.         });
  1024.       }
  1025.       boss.shootTimer = 50;
  1026.     }
  1027.     playSound('shoot');
  1028.   }
  1029.   // Touch damage
  1030.   if (player.alive && rectCollide(player, boss)) playerDie();
  1031. }
  1032. function updatePowerups() {
  1033.   for (const p of powerups) {
  1034.     if (!p.alive) continue;
  1035.     if (player.alive && rectCollide(player, p)) {
  1036.       p.alive = false;
  1037.       playSound('powerup');
  1038.       score += 500;
  1039.       switch (p.type) {
  1040.         case 'spread': player.weapon = 'spread'; player.weaponTimer = 600; break;
  1041.         case 'rapid': player.weapon = 'rapid'; player.weaponTimer = 600; break;
  1042.         case 'laser': player.weapon = 'laser'; player.weaponTimer = 600; break;
  1043.         case 'life': lives = Math.min(lives + 1, 9); break;
  1044.         case 'shield': player.invincible = 300; break;
  1045.       }
  1046.       for (let i = 0; i < 10; i++) {
  1047.         particles.push({
  1048.           x: p.x + 12, y: p.y + 12,
  1049.           vx: (Math.random()-0.5)*4, vy: (Math.random()-0.5)*4,
  1050.           size: 2+Math.random()*2, color: '#ffeb3b',
  1051.           life: 20+Math.random()*10, maxLife: 30
  1052.         });
  1053.       }
  1054.     }
  1055.   }
  1056. }
  1057. // --- Game Init ---
  1058. function initGame() {
  1059.   score = 0; lives = 3; cameraX = 0;
  1060.   bossDefeated = false; boss = null;
  1061.   playerBullets = []; enemyBullets = [];
  1062.   explosions = []; particles = [];
  1063.   player.x = 100; player.y = 300;
  1064.   player.vx = 0; player.vy = 0;
  1065.   player.alive = true; player.invincible = 60;
  1066.   player.weapon = 'normal'; player.weaponTimer = 0;
  1067.   player.dir = 1; player.prone = false;
  1068.   generateLevel();
  1069.   spawnEnemies();
  1070. }
  1071. // --- Main Loop ---
  1072. function gameLoop() {
  1073.   frameCount++;
  1074.   if (gameState === 'title') {
  1075.     drawTitle();
  1076.     if (keys['Enter'] || keys['Space']) {
  1077.       gameState = 'playing';
  1078.       initGame();
  1079.       ensureAudio();
  1080.     }
  1081.   } else if (gameState === 'playing') {
  1082.     // Screen shake
  1083.     ctx.save();
  1084.     if (screenShake > 0) {
  1085.       ctx.translate(
  1086.         (Math.random() - 0.5) * screenShake,
  1087.         (Math.random() - 0.5) * screenShake
  1088.       );
  1089.       screenShake--;
  1090.     }
  1091.     drawBackground();
  1092.     drawPlatforms();
  1093.     drawPowerups();
  1094.     updatePlayer();
  1095.     updateEnemies();
  1096.     updateBoss();
  1097.     updateBullets();
  1098.     updatePowerups();
  1099.     // Draw enemies
  1100.     for (const e of enemies) {
  1101.       if (!e.alive) continue;
  1102.       const sx = e.x - cameraX;
  1103.       if (sx < -50 || sx > GAME_W + 50) continue;
  1104.       if (e.type === 'soldier') drawSoldier(e);
  1105.       else if (e.type === 'turret') drawTurret(e);
  1106.       else if (e.type === 'runner') drawRunner(e);
  1107.     }
  1108.     drawBoss(boss);
  1109.     drawBullets();
  1110.     drawExplosions();
  1111.     drawParticles();
  1112.     // Draw player
  1113.     if (player.alive) {
  1114.       if (player.invincible > 0 && frameCount % 4 < 2) {
  1115.         // Flash when invincible
  1116.       } else {
  1117.         drawPixelChar(player.x, player.y, player.dir, player.animFrame, player.prone, player.shooting);
  1118.       }
  1119.     }
  1120.     drawHUD();
  1121.     ctx.restore();
  1122.   } else if (gameState === 'gameover') {
  1123.     drawBackground();
  1124.     drawPlatforms();
  1125.     drawGameOver();
  1126.     if (keys['Enter']) { gameState = 'title'; keys['Enter'] = false; }
  1127.   } else if (gameState === 'win') {
  1128.     drawBackground();
  1129.     drawPlatforms();
  1130.     drawWin();
  1131.     if (keys['Enter']) { gameState = 'title'; keys['Enter'] = false; }
  1132.   }
  1133.   requestAnimationFrame(gameLoop);
  1134. }
  1135. // --- Start ---
  1136. gameLoop();
  1137. </script>
  1138. </body>
  1139. </html>
复制代码


回复

您需要登录后才可以回帖 登录 | 立即注册 微信登录

本版积分规则

您需要 登录 后才可以回复,轻松玩转社区,没有帐号?立即注册
快速回复
关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表