<html lang="zh"> <head> <meta charset="UTF-8"> <title>校园闯关</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { background: #111; display: flex; justify-content: center; align-items: center; height: 100vh; overflow: hidden; font-family: 'Microsoft YaHei', sans-serif; } canvas { border: 2px solid #444; cursor: crosshair; } #startScreen, #winScreen, #deathScreen { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; background: rgba(0,0,0,0.92); color: #fff; z-index: 10; } #startScreen h1, #winScreen h1, #deathScreen h1 { font-size: 46px; margin-bottom: 15px; text-shadow: 0 0 20px rgba(255,100,100,0.5); } #startScreen p, #winScreen p, #deathScreen p { font-size: 16px; margin-bottom: 8px; color: #ccc; } .section-title { font-size: 14px; color: #888; margin-top: 18px; margin-bottom: 8px; letter-spacing: 2px; } .btn-row { display: flex; gap: 8px; margin-bottom: 5px; flex-wrap: wrap; justify-content: center; } button { padding: 10px 18px; font-size: 14px; cursor: pointer; background: #333; color: #fff; border: 2px solid #555; border-radius: 8px; transition: all 0.2s; outline: none; } button:hover { transform: translateY(-2px); filter: brightness(1.2); } .mode-btn.single { background: #2980b9; border-color: #3498db; } .mode-btn.double { background: #27ae60; border-color: #2ecc71; } .mode-btn.quad { background: #8e44ad; border-color: #9b59b6; } .mode-btn.octo { background: #c0392b; border-color: #e74c3c; } .mode-btn.army { background: linear-gradient(135deg, #f39c12, #e74c3c); border-color: #f1c40f; } .diff-btn.easy { background: #2c3e50; border-color: #34495e; } .diff-btn.normal { background: #d35400; border-color: #e67e22; } .diff-btn.hard { background: #8e44ad; border-color: #9b59b6; } .diff-btn.nightmare { background: #111; border-color: #c0392b; color: #e74c3c; font-weight: bold; text-shadow: 0 0 5px #f00; } button.active { transform: translateY(-3px); box-shadow: 0 0 15px rgba(255,255,255,0.4), 0 5px 15px rgba(0,0,0,0.5); border-color: #fff !important; filter: brightness(1.3); } .start-btn { margin-top: 25px; padding: 16px 60px; font-size: 22px; font-weight: bold; background: linear-gradient(135deg, #e74c3c, #c0392b); border: none; border-radius: 12px; box-shadow: 0 4px 15px rgba(231, 76, 60, 0.4); } .start-btn:hover { box-shadow: 0 6px 25px rgba(231, 76, 60, 0.6); } .hidden { display: none !important; } #controls { color: #777; font-size: 12px; margin-top: 15px; text-align: center; line-height: 1.7; max-width: 650px; } .warn-text { color: #e74c3c; font-size: 12px; margin-top: 8px; font-style: italic; } .army-warn { color: #f1c40f; font-size: 13px; margin-top: 6px; font-weight: bold; } </style> </head> <body> <canvas id="gameCanvas" width="960" height="640"></canvas> <div id="startScreen"> <h1>🏫 校园闯关</h1> <p>在这所学校中杀出一条血路!</p> <div class="section-title">— 选择模式 —</div> <div class="btn-row"> <button id="btn-single" class="mode-btn single active" onclick="selectMode('single')">🎮 单人</button> <button id="btn-double" class="mode-btn double" onclick="selectMode('double')">👥 双人</button> <button id="btn-quad" class="mode-btn quad" onclick="selectMode('quad')">👥👥 四人</button> <button id="btn-octo" class="mode-btn octo" onclick="selectMode('octo')">👥x4 八人</button> <button id="btn-army" class="mode-btn army" onclick="selectMode('army')">⚔️ 20人大战</button> </div> <p class="army-warn">* 20人模式:19名AI队友!每击杀敌人全队属性提升!</p> <div class="section-title">— 选择难度 —</div> <div class="btn-row"> <button id="btn-easy" class="diff-btn easy" onclick="selectDiff('easy')">😊 简单</button> <button id="btn-normal" class="diff-btn normal active" onclick="selectDiff('normal')">😐 中等</button> <button id="btn-hard" class="diff-btn hard" onclick="selectDiff('hard')">😈 困难</button> <button id="btn-nightmare" class="diff-btn nightmare" onclick="selectDiff('nightmare')">💀 噩梦</button> </div> <p class="warn-text">* 八人简单 ≈ 四人中等 | 噩梦难度极其凶残!</p> <button class="start-btn" onclick="startGame()">⚔️ 开始游戏</button> <div id="controls"> <b>操作说明</b><br> WASD 移动 | 鼠标瞄准 | 左键 主武器 | 右键 副武器(戒尺) | 右键长按蓄力投掷 | 1/2/3 切换武器 | R 换弹 </div> </div> <div id="winScreen" class="hidden"> <h1>🎉 通关成功!</h1> <p>你成功闯过了所有关卡!</p> <button class="start-btn" onclick="showStart()">返回主菜单</button> </div> <div id="deathScreen" class="hidden"> <h1>💀 你倒下了...</h1> <p>再试一次吧!</p> <button class="start-btn" onclick="showStart()">返回主菜单</button> </div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const W = canvas.width, H = canvas.height; let game = {}, keys = {}; let mouse = { x: W/2, y: H/2, left: false, right: false }; let particles = [], bullets = [], enemyBullets = [], companionBullets = [], pickups = []; let gameRunning = false, animFrame; let selectedMode = 'single', selectedDiff = 'normal';
function selectMode(m) { selectedMode = m; document.querySelectorAll(‘.mode-btn’).forEach(b => b.classList.remove(‘active’)); document.getElementById(‘btn-’+m).classList.add(‘active’); } function selectDiff(d) { selectedDiff = d; document.querySelectorAll(‘.diff-btn’).forEach(b => b.classList.remove(‘active’)); document.getElementById(‘btn-’+d).classList.add(‘active’); }
const WEAPONS = { fist: { name: ‘拳头’, type: ‘melee’, damage: 15, range: 50, speed: 400, ammo: Infinity, maxAmmo: Infinity, magSize: Infinity, reloadTime: 0, color: ‘#fff’, knockback: 8, icon: ’👊’ }, pistol: { name: ‘手枪’, type: ‘gun’, damage: 20, range: 500, speed: 180, ammo: 48, maxAmmo: 48, magSize: 12, reloadTime: 1000, color: ‘#ff0’, knockback: 5, icon: ’🔫’, bulletSpeed: 12, bulletSize: 3 }, rifle: { name: ‘步枪’, type: ‘gun’, damage: 30, range: 600, speed: 100, ammo: 90, maxAmmo: 90, magSize: 30, reloadTime: 1500, color: ‘#f80’, knockback: 7, icon: ’🔫’, bulletSpeed: 16, bulletSize: 4 }, ruler: { name: ‘戒尺’, type: ‘melee’, damage: 25, range: 60, speed: 300, ammo: Infinity, maxAmmo: Infinity, magSize: Infinity, reloadTime: 0, color: ‘#8B4513’, knockback: 12, icon: ’📏’, throwable: true } };
const COMP_COLORS = [‘#2ecc71’,‘#3498db’,‘#9b59b6’,‘#e67e22’,‘#1abc9c’,‘#ff7979’,‘#f1c40f’,‘#e91e63’,‘#00bcd4’,‘#8bc34a’,‘#ff5722’,‘#607d8b’,‘#795548’,‘#4caf50’,‘#03a9f4’,‘#ff9800’,‘#673ab7’,‘#cddc39’,‘#009688’];
function getCompanionOffset(id, total) { if (total <= 3) { return [[-50,30],[50,30],[0,60]][id] || [0, 40+id*20]; } const ring = Math.floor(id / 8) + 1; const posInRing = id % 8; const countInRing = Math.min(8, total - Math.floor(id/8)8); const angle = (Math.PI2/countInRing) * posInRing - Math.PI/2; const radius = 40 + ring * 35; return [Math.cos(angle)*radius, Math.sin(angle)*radius]; }
function createCompanion(id) { const color = COMP_COLORS[id % COMP_COLORS.length]; return { id, name: ‘队友’+String.fromCharCode(65+id), color, x: W/2, y: H/2, w: 22, h: 22, hp: 70, maxHp: 70, speed: 3.0, angle: 0, shootTimer: Math.floor(Math.random()*25), shootCooldown: 18+id, damage: 10, invincible: 0, alive: true, weaponLevel: 1, dmgBonus: 0, spdBonus: 0 }; }
function initGame(mode, diff) { const modeStats = { single: { diff: 1.0, spawn: 1.0, max: 1.0 }, double: { diff: 1.3, spawn: 1.5, max: 1.3 }, quad: { diff: 1.7, spawn: 2.0, max: 1.6 }, octo: { diff: 2.4, spawn: 3.5, max: 2.5 }, army: { diff: 4.0, spawn: 6.0, max: 3.5 } }; const diffStats = { easy: { diff: 0.7, spawn: 0.7, max: 0.7, php: 1.2, heal: 1.3 }, normal: { diff: 1.0, spawn: 1.0, max: 1.0, php: 1.0, heal: 1.0 }, hard: { diff: 1.4, spawn: 1.3, max: 1.2, php: 0.8, heal: 0.7 }, nightmare: { diff: 1.9, spawn: 1.6, max: 1.5, php: 0.6, heal: 0.4 } }; const ms = modeStats[mode], ds = diffStats[diff]; const finalDiffMult = ms.diff * ds.diff; const finalSpawnMult = ms.spawn * ds.spawn; const finalMaxMult = ms.max * ds.max; const baseHp = Math.floor(100 * ds.php); const numCompanions = mode === ‘single’ ? 0 : (mode === ‘double’ ? 1 : (mode === ‘quad’ ? 3 : (mode === ‘octo’ ? 7 : 19)));
game = {
mode, diff, diffMult: finalDiffMult, spawnMult: finalSpawnMult, maxEnemyMult: finalMaxMult, healMod: ds.heal,
level: 1,
player: {
x: W/2, y: H/2, w: 28, h: 28, hp: baseHp, maxHp: baseHp, speed: 3.5, angle: 0,
weapons: [{ ...WEAPONS.fist, currentAmmo: Infinity, reloading: false, reloadTimer: 0, lastShot: 0 }, null, null],
currentSlot: 0, secondary: null,
rulerThrown: false, rulerX: 0, rulerY: 0, rulerAngle: 0, rulerReturning: false, rulerChargeStart: 0, rulerCharging: false,
invincible: 0, fistSwing: 0, rulerSwing: 0,
dmgBonus: 0, spdBonus: 0, hpRegen: 0
},
companions: [], enemies: [],
spawnTimer: 0, spawnCount: 0, maxEnemies: 0, totalToSpawn: 0,
levelComplete: false, transition: false, transitionTimer: 0, transitionText: '',
shakeX: 0, shakeY: 0, shakeTime: 0, levelMessage: '', levelMessageTimer: 0,
bossActive: false, killCount: 0, totalKills: 0,
isArmy: mode === 'army',
armyBuffCount: 0
};
for (let i = 0; i < numCompanions; i++) game.companions.push(createCompanion(i));
particles = []; bullets = []; enemyBullets = []; companionBullets = []; pickups = [];
setupLevel(1);
}
function setupLevel(lvl) { game.level = lvl; game.levelComplete = false; game.transition = false; game.spawnTimer = 0; game.spawnCount = 0; game.enemies = []; enemyBullets = []; bullets = []; companionBullets = []; pickups = []; const p = game.player, sm = game.spawnMult, mem = game.maxEnemyMult; const healAmt = Math.floor(30 * game.healMod);
if (lvl === 1) {
p.x = W/2; p.y = H/2;
p.weapons[0] = { ...WEAPONS.fist, currentAmmo: Infinity, reloading: false, reloadTimer: 0, lastShot: 0 };
p.weapons[1] = null; p.weapons[2] = null; p.secondary = null; p.currentSlot = 0;
game.totalToSpawn = Math.floor(15 * sm); game.maxEnemies = Math.max(3, Math.floor(6 * mem));
const mn = {single:'单人',double:'双人',quad:'四人',octo:'八人',army:'20人'}[game.mode];
const dn = {easy:'简单',normal:'中等',hard:'困难',nightmare:'噩梦'}[game.diff];
game.levelMessage = `第一关:教室 [${mn}/${dn}]`; game.levelMessageTimer = 180;
} else if (lvl === 2) {
p.x = W/2; p.y = H-80; p.hp = Math.min(p.hp+healAmt, p.maxHp);
p.weapons[0] = { ...WEAPONS.pistol, currentAmmo: WEAPONS.pistol.magSize, reloading: false, reloadTimer: 0, lastShot: 0 };
p.currentSlot = 0;
game.totalToSpawn = Math.floor(12*sm); game.maxEnemies = Math.max(3, Math.floor(5*mem));
game.levelMessage = '第二关:走廊 — 获得手枪!'; game.levelMessageTimer = 180;
} else if (lvl === 3) {
p.x = W/2; p.y = H-80; p.hp = Math.min(p.hp+healAmt, p.maxHp);
p.weapons[0] = { ...WEAPONS.rifle, currentAmmo: WEAPONS.rifle.magSize, reloading: false, reloadTimer: 0, lastShot: 0 };
p.secondary = { ...WEAPONS.ruler }; p.currentSlot = 0;
game.totalToSpawn = 0; game.maxEnemies = 1;
game.levelMessage = '第三关:教导主任'; game.levelMessageTimer = 180;
setTimeout(() => { if (game.level===3 && gameRunning) spawnBoss(); }, 2000);
}
const totalComp = game.companions.length;
game.companions.forEach((c, i) => {
if (c.alive) {
const off = getCompanionOffset(i, totalComp);
c.x = p.x + off[0]; c.y = p.y + off[1];
c.weaponLevel = lvl;
c.hp = Math.min(c.hp + Math.floor(20*game.healMod), c.maxHp);
}
});
}
function onKill(enemy) { game.killCount++; game.totalKills++; const p = game.player;
if (game.isArmy) {
// Every kill: buff all alive teammates and player
game.armyBuffCount++;
const dmgUp = 1.5;
const spdUp = 0.03;
const hpUp = 3;
p.dmgBonus += dmgUp;
p.spdBonus += spdUp;
p.maxHp += hpUp;
p.hp = Math.min(p.hp + 2, p.maxHp);
game.companions.forEach(c => {
if (c.alive) {
c.dmgBonus += dmgUp;
c.spdBonus += spdUp * 0.5;
c.maxHp += hpUp;
c.hp = Math.min(c.hp + 1, c.maxHp);
}
});
// Visual buff effect
spawnBuffText(p.x, p.y - 30, `⬆ 全队强化 x${game.armyBuffCount}`, '#f1c40f');
// Spawn pickup occasionally
if (game.armyBuffCount % 5 === 0) {
pickups.push({ x: enemy.x, y: enemy.y, type: 'heal', life: 300, size: 8, color: '#2ecc71' });
}
} else {
// Non-army: smaller personal buff
if (game.totalKills % 3 === 0) {
p.dmgBonus += 0.5;
p.hp = Math.min(p.hp + 1, p.maxHp);
spawnBuffText(p.x, p.y - 30, `⬆ +伤害`, '#ff0');
}
}
}
function spawnBuffText(x, y, text, color) { particles.push({ x, y, vx: 0, vy: -2, life: 60, maxLife: 60, text, color, type: ‘text’, size: 18 }); }
function spawnEnemy() { const lvl = game.level, dm = game.diffMult; let e = {}; if (lvl === 1) { const side = Math.random() < 0.5 ? ‘front’ : ‘back’; if (side === ‘front’) e = { x: 100+Math.random()100, y: -20 }; else e = { x: W-200+Math.random()100, y: H+20 }; e.w = 24; e.h = 24; e.hp = 40dm; e.maxHp = 40dm; e.speed = 1.2+Math.random()0.5; e.damage = 8dm; e.type = ‘student’; e.color = ‘#4a9’; e.attackCD = 0; e.hit = 0; } else if (lvl === 2) { const side = Math.floor(Math.random()*4); if (side===0) e = { x: Math.random()*W, y: -20 }; else if (side===1) e = { x: Math.random()*W, y: H+20 }; else if (side===2) e = { x: -20, y: Math.random()H }; else e = { x: W+20, y: Math.random()H }; e.w = 28; e.h = 28; e.hp = 60dm; e.maxHp = 60dm; e.speed = 1.5+Math.random()0.4; e.damage = 12dm; e.type = ‘teacher’; e.color = ‘#e67e22’; e.attackCD = 0; e.hit = 0; } game.enemies.push(e); game.spawnCount++; }
function spawnBoss() { game.bossActive = true; const dm = game.diffMult; game.enemies.push({ x: W/2, y: 100, w: 50, h: 50, hp: 500dm, maxHp: 500dm, speed: 1.0, damage: 15*dm, type: ‘boss’, color: ‘#c0392b’, attackCD: 0, patternTimer: 0, hit: 0 }); }
function spawnBlood(x,y,angle,count) { for(let i=0;i<count;i++){const a=angle+(Math.random()-0.5)*1.2,sp=2+Math.random()*4;particles.push({x,y,vx:Math.cos(a)*sp,vy:Math.sin(a)*sp,life:25+Math.random()*15,maxLife:40,size:2+Math.random()*3,color:hsl(0,${70+Math.random()*30}%,${30+Math.random()*25}%),type:‘blood’});} }
function spawnHitEffect(x,y) { for(let i=0;i<3;i++){const a=Math.random()Math.PI2,sp=1+Math.random()*3;particles.push({x,y,vx:Math.cos(a)*sp,vy:Math.sin(a)*sp,life:10+Math.random()*8,maxLife:18,size:2+Math.random()*2,color:‘#ff0’,type:‘spark’});} }
function spawnText(x,y,text,color) { particles.push({x,y,vx:0,vy:-1.5,life:35,maxLife:35,text,color,type:‘text’,size:13}); }
function screenShake(i) { game.shakeTime=5; game.shakeX=(Math.random()-0.5)*i; game.shakeY=(Math.random()-0.5)*i; }
document.addEventListener(‘keydown’, e => { keys[e.key.toLowerCase()]=true; if(e.key>=‘1’&&e.key<=‘3’){const s=parseInt(e.key)-1;if(game.player.weapons[s])game.player.currentSlot=s;} if(e.key.toLowerCase()===‘r’)reloadWeapon(); }); document.addEventListener(‘keyup’, e => keys[e.key.toLowerCase()]=false); canvas.addEventListener(‘mousemove’, e => { const r=canvas.getBoundingClientRect(); mouse.x=e.clientX-r.left; mouse.y=e.clientY-r.top; }); canvas.addEventListener(‘mousedown’, e => { e.preventDefault(); if(e.button===0)mouse.left=true; if(e.button===2){mouse.right=true;if(game.player.secondary&&!game.player.rulerThrown){game.player.rulerCharging=true;game.player.rulerChargeStart=Date.now();}} }); canvas.addEventListener(‘mouseup’, e => { if(e.button===0)mouse.left=false; if(e.button===2){mouse.right=false;if(game.player.rulerCharging&&game.player.secondary){if(Date.now()-game.player.rulerChargeStart>300)throwRuler();else swingRuler();game.player.rulerCharging=false;}} }); canvas.addEventListener(‘contextmenu’, e => e.preventDefault());
function reloadWeapon(){const w=game.player.weapons[game.player.currentSlot];if(w&&w.type===‘gun’&&w.currentAmmo<w.magSize&&w.ammo>0&&!w.reloading){w.reloading=true;w.reloadTimer=w.reloadTime;}}
function throwRuler(){const p=game.player;if(p.rulerThrown)return;p.rulerThrown=true;p.rulerX=p.x;p.rulerY=p.y;p.rulerAngle=p.angle;p.rulerReturning=false;}
function swingRuler(){const p=game.player;if(!p.secondary||p.rulerSwing>0)return;p.rulerSwing=15;const range=p.secondary.range+10;game.enemies.forEach(e=>{const dx=e.x-p.x,dy=e.y-p.y,dist=Math.sqrt(dxdx+dydy);let ad=Math.atan2(dy,dx)-p.angle;while(ad>Math.PI)ad-=Math.PI2;while(ad<-Math.PI)ad+=Math.PI2;if(dist<range&&Math.abs(ad)<Math.PI/3){const dmg=p.secondary.damage+p.dmgBonus;e.hp-=dmg;e.hit=10;const pa=Math.atan2(e.y-p.y,e.x-p.x);e.x+=Math.cos(pa)p.secondary.knockback1.5;e.y+=Math.sin(pa)p.secondary.knockback1.5;spawnBlood(e.x,e.y,pa,6);spawnHitEffect(e.x,e.y);spawnText(e.x,e.y-20,-${Math.floor(dmg)},‘#ff4444’);screenShake(4);}});}
function getCurrentWeapon(){return game.player.weapons[game.player.currentSlot];}
function shoot(){
const p=game.player,w=getCurrentWeapon();if(!w)return;
const now=Date.now();if(w.reloading||now-w.lastShot<w.speed)return;
if(w.type===‘melee’&&!w.throwable){
if(p.fistSwing>0)return;p.fistSwing=12;w.lastShot=now;
game.enemies.forEach(e=>{const dx=e.x-p.x,dy=e.y-p.y,dist=Math.sqrt(dxdx+dydy);let ad=Math.atan2(dy,dx)-p.angle;while(ad>Math.PI)ad-=Math.PI2;while(ad<-Math.PI)ad+=Math.PI2;if(dist<w.range+15&&Math.abs(ad)<Math.PI/3){const dmg=w.damage+p.dmgBonus;e.hp-=dmg;e.hit=10;const pa=Math.atan2(e.y-p.y,e.x-p.x);e.x+=Math.cos(pa)*w.knockback;e.y+=Math.sin(pa)*w.knockback;spawnBlood(e.x,e.y,pa,5);spawnHitEffect(e.x,e.y);spawnText(e.x,e.y-20,-${Math.floor(dmg)},‘#ff4444’);screenShake(3);}});
} else if(w.type===‘gun’){
if(w.currentAmmo<=0){reloadWeapon();return;}
w.lastShot=now;w.currentAmmo—;
const bx=p.x+Math.cos(p.angle)*20,by=p.y+Math.sin(p.angle)*20;
bullets.push({x:bx,y:by,vx:Math.cos(p.angle)*w.bulletSpeed,vy:Math.sin(p.angle)*w.bulletSpeed,damage:w.damage+p.dmgBonus,knockback:w.knockback,size:w.bulletSize,color:w.color,life:w.range/w.bulletSpeed});
screenShake(1);
if(w.currentAmmo<=0)reloadWeapon();
}
}
function updateCompanions(){ const p=game.player, totalComp=game.companions.length; game.companions.forEach(c=>{ if(!c.alive)return; if(c.invincible>0)c.invincible—; let nearest=null,nearestDist=Infinity; game.enemies.forEach(e=>{const d=Math.sqrt((e.x-c.x)**2+(e.y-c.y)2);if(d<nearestDist){nearestDist=d;nearest=e;}}); c.target=nearest; const off=getCompanionOffset(c.id,totalComp); let targetX=p.x+off[0],targetY=p.y+off[1]; if(nearest&&nearestDist<400){ const midX=(p.x+nearest.x)/2,midY=(p.y+nearest.y)/2; const perpAngle=Math.atan2(nearest.y-p.y,nearest.x-p.x)+Math.PI/2(c.id%2===0?1:-1); targetX=midX+Math.cos(perpAngle)(25+c.id6); targetY=midY+Math.sin(perpAngle)(25+c.id*6); if(nearest.type===‘boss’&&nearestDist<150){targetX=c.x-Math.cos(Math.atan2(nearest.y-c.y,nearest.x-c.x))60;targetY=c.y-Math.sin(Math.atan2(nearest.y-c.y,nearest.x-c.x))60;} } // Repulsion game.companions.forEach(other=>{if(other!==c&&other.alive){let dx=c.x-other.x,dy=c.y-other.y,dist=Math.sqrt(dxdx+dydy);if(dist<40&&dist>0){targetX+=(dx/dist)20;targetY+=(dy/dist)20;}}}); let dpx=c.x-p.x,dpy=c.y-p.y,dpd=Math.sqrt(dpxdpx+dpydpy); if(dpd<30&&dpd>0){targetX+=(dpx/dpd)*12;targetY+=(dpy/dpd)*12;}
const dx=targetX-c.x,dy=targetY-c.y,md=Math.sqrt(dx*dx+dy*dy);
const cSpeed=c.speed+c.spdBonus;
if(md>5){c.x+=(dx/md)*cSpeed;c.y+=(dy/md)*cSpeed;}
c.x=Math.max(15,Math.min(W-15,c.x));c.y=Math.max(15,Math.min(H-15,c.y));
c.angle=nearest?Math.atan2(nearest.y-c.y,nearest.x-c.x):Math.atan2(p.y-c.y,p.x-c.x);
c.shootTimer++;
if(nearest&&nearestDist<400&&c.shootTimer>=c.shootCooldown){
c.shootTimer=0;const sa=c.angle+(Math.random()-0.5)*0.15;
let bDmg=c.damage+c.dmgBonus,bColor='#5f5';
if(c.weaponLevel>=2){bDmg=16+c.dmgBonus;bColor='#5ff';}
if(c.weaponLevel>=3){bDmg=22+c.dmgBonus;bColor='#fa0';}
companionBullets.push({x:c.x+Math.cos(sa)*16,y:c.y+Math.sin(sa)*16,vx:Math.cos(sa)*11,vy:Math.sin(sa)*11,damage:bDmg,knockback:3,size:2.5,color:bColor,life:40});
}
});
}
function update(dt){ const p=game.player; if(game.transition){game.transitionTimer—;if(game.transitionTimer<=0){game.transition=false;if(game.level>3){gameRunning=false;document.getElementById(‘winScreen’).classList.remove(‘hidden’);return;}}return;} if(game.levelMessageTimer>0)game.levelMessageTimer—;
let mx=0,my=0;
if(keys['w']||keys['arrowup'])my-=1;if(keys['s']||keys['arrowdown'])my+=1;
if(keys['a']||keys['arrowleft'])mx-=1;if(keys['d']||keys['arrowright'])mx+=1;
if(mx!==0||my!==0){const len=Math.sqrt(mx*mx+my*my);mx/=len;my/=len;}
const pSpeed=p.speed+p.spdBonus;
p.x+=mx*pSpeed;p.y+=my*pSpeed;
p.x=Math.max(25,Math.min(W-25,p.x));p.y=Math.max(25,Math.min(H-25,p.y));
p.angle=Math.atan2(mouse.y-p.y,mouse.x-p.x);
if(p.invincible>0)p.invincible--;if(p.fistSwing>0)p.fistSwing--;if(p.rulerSwing>0)p.rulerSwing--;
// Army HP regen
if(game.isArmy && game.armyBuffCount > 0) {
if(Math.random() < 0.01 * game.armyBuffCount) p.hp = Math.min(p.hp+0.5, p.maxHp);
}
p.weapons.forEach(w=>{if(w&&w.reloading){w.reloadTimer-=dt*1000;if(w.reloadTimer<=0){w.reloading=false;if(w.ammo!==Infinity){w.currentAmmo=w.magSize;w.ammo=w.maxAmmo;}}}});
if(mouse.left)shoot();
if(p.rulerThrown){
if(!p.rulerReturning){p.rulerX+=Math.cos(p.rulerAngle)*10;p.rulerY+=Math.sin(p.rulerAngle)*10;if(Math.sqrt((p.rulerX-p.x)**2+(p.rulerY-p.y)**2)>300||p.rulerX<0||p.rulerX>W||p.rulerY<0||p.rulerY>H)p.rulerReturning=true;game.enemies.forEach(e=>{if(Math.sqrt((e.x-p.rulerX)**2+(e.y-p.rulerY)**2)<e.w+10){const dmg=(p.secondary.damage+p.dmgBonus)*1.5;e.hp-=dmg;e.hit=10;const pa=Math.atan2(e.y-p.rulerY,e.x-p.rulerX);e.x+=Math.cos(pa)*15;e.y+=Math.sin(pa)*15;spawnBlood(e.x,e.y,pa,8);spawnText(e.x,e.y-20,`-${Math.floor(dmg)}`,'#ff0');screenShake(4);}});}
else{const dx=p.x-p.rulerX,dy=p.y-p.rulerY,dist=Math.sqrt(dx*dx+dy*dy);if(dist<20){p.rulerThrown=false;p.rulerReturning=false;}else{const ra=Math.atan2(dy,dx);p.rulerX+=Math.cos(ra)*12;p.rulerY+=Math.sin(ra)*12;p.rulerAngle=ra;game.enemies.forEach(e=>{if(Math.sqrt((e.x-p.rulerX)**2+(e.y-p.rulerY)**2)<e.w+10){e.hp-=p.secondary.damage+p.dmgBonus;e.hit=8;spawnBlood(e.x,e.y,ra,5);spawnText(e.x,e.y-20,`-${Math.floor(p.secondary.damage+p.dmgBonus)}`,'#ff0');}});}}
p.rulerAngle+=0.3;
}
if(!game.levelComplete&&game.spawnCount<game.totalToSpawn&&game.enemies.length<game.maxEnemies){game.spawnTimer++;const sr=game.level===1?90:120;if(game.spawnTimer>=Math.max(8,Math.floor(sr/game.spawnMult))){game.spawnTimer=0;spawnEnemy();}}
let targets=[{x:p.x,y:p.y,isPlayer:true,ref:p}];
game.companions.forEach(c=>{if(c.alive)targets.push({x:c.x,y:c.y,isPlayer:false,ref:c});});
game.enemies.forEach(e=>{
if(e.hit>0)e.hit--;
let ct=targets[0],cd=Math.sqrt((e.x-targets[0].x)**2+(e.y-targets[0].y)**2);
for(let i=1;i<targets.length;i++){const d=Math.sqrt((e.x-targets[i].x)**2+(e.y-targets[i].y)**2);const bias=targets[i].isPlayer?0.75:1.0;if(d*bias<cd){cd=d*bias;ct=targets[i];}}
const dx=ct.x-e.x,dy=ct.y-e.y,dist=Math.sqrt(dx*dx+dy*dy),angle=Math.atan2(dy,dx);
if(e.type==='boss'){
e.patternTimer++;
if(dist>150){e.x+=Math.cos(angle)*e.speed;e.y+=Math.sin(angle)*e.speed;}
else if(dist<100){e.x-=Math.cos(angle)*e.speed*0.5;e.y-=Math.sin(angle)*e.speed*0.5;}
e.x=Math.max(40,Math.min(W-40,e.x));e.y=Math.max(40,Math.min(H-40,e.y));
const baseModeDiv = {single:1.0,double:1.3,quad:1.7,octo:2.4,army:4.0}[game.mode];
const baseBm=game.diffMult/baseModeDiv;
const finalBm=baseBm*({single:1.0,double:1.0,quad:1.2,octo:1.4,army:1.6}[game.mode]);
if(e.patternTimer%Math.max(5,Math.floor(30/finalBm))===0){const num=Math.floor((e.hp<e.maxHp*0.5?8:5)*finalBm);for(let i=0;i<num;i++){const a=angle+(i-(num-1)/2)*0.22;enemyBullets.push({x:e.x,y:e.y,vx:Math.cos(a)*3.5,vy:Math.sin(a)*3.5,damage:e.damage,size:5,color:'#f55',life:180,type:'ticket'});}}
if(e.patternTimer%80===40){const num=e.hp<e.maxHp*0.3?16:12;for(let i=0;i<num;i++){const a=(Math.PI*2/num)*i+e.patternTimer*0.02;enemyBullets.push({x:e.x,y:e.y,vx:Math.cos(a)*2.5,vy:Math.sin(a)*2.5,damage:e.damage,size:4,color:'#fa0',life:150,type:'ticket'});}}
if(e.hp<e.maxHp*0.3&&e.patternTimer%Math.max(3,Math.floor(8/finalBm))===0){const a=e.patternTimer*0.15;enemyBullets.push({x:e.x,y:e.y,vx:Math.cos(a)*3,vy:Math.sin(a)*3,damage:e.damage*0.7,size:4,color:'#f0f',life:120,type:'ticket'});}
} else {
e.x+=Math.cos(angle)*e.speed;e.y+=Math.sin(angle)*e.speed;
if(e.attackCD>0)e.attackCD--;
if(dist<35&&e.attackCD<=0){const tgt=ct.ref;if(tgt.invincible<=0){tgt.hp-=e.damage;tgt.invincible=30;screenShake(ct.isPlayer?5:2);spawnBlood(tgt.x,tgt.y,angle+Math.PI,3);}e.attackCD=60;}
}
});
bullets.forEach(b=>{b.x+=b.vx;b.y+=b.vy;b.life--;game.enemies.forEach(e=>{if(Math.sqrt((e.x-b.x)**2+(e.y-b.y)**2)<e.w/2+b.size){e.hp-=b.damage;e.hit=10;b.life=0;const pa=Math.atan2(b.vy,b.vx);e.x+=Math.cos(pa)*b.knockback;e.y+=Math.sin(pa)*b.knockback;spawnBlood(e.x,e.y,pa,6);spawnHitEffect(b.x,b.y);spawnText(e.x,e.y-20,`-${Math.floor(b.damage)}`,'#ff4444');screenShake(2);}});});
bullets=bullets.filter(b=>b.life>0&&b.x>-10&&b.x<W+10&&b.y>-10&&b.y<H+10);
companionBullets.forEach(b=>{b.x+=b.vx;b.y+=b.vy;b.life--;game.enemies.forEach(e=>{if(Math.sqrt((e.x-b.x)**2+(e.y-b.y)**2)<e.w/2+b.size){e.hp-=b.damage;e.hit=8;b.life=0;const pa=Math.atan2(b.vy,b.vx);e.x+=Math.cos(pa)*b.knockback;e.y+=Math.sin(pa)*b.knockback;spawnBlood(e.x,e.y,pa,3);spawnText(e.x,e.y-20,`-${Math.floor(b.damage)}`,'#5f5');}});});
companionBullets=companionBullets.filter(b=>b.life>0&&b.x>-10&&b.x<W+10&&b.y>-10&&b.y<H+10);
enemyBullets.forEach(b=>{b.x+=b.vx;b.y+=b.vy;b.life--;targets.forEach(t=>{const tgt=t.ref;if(tgt.invincible<=0&&Math.sqrt((tgt.x-b.x)**2+(tgt.y-b.y)**2)<(t.isPlayer?16:14)){tgt.hp-=b.damage;tgt.invincible=20;b.life=0;if(t.isPlayer)screenShake(3);spawnBlood(tgt.x,tgt.y,Math.atan2(tgt.y-b.y,tgt.x-b.x),3);}});});
enemyBullets=enemyBullets.filter(b=>b.life>0&&b.x>-20&&b.x<W+20&&b.y>-20&&b.y<H+20);
// Pickups
pickups.forEach(pk => {
pk.life--;
// Player pickup
if(Math.sqrt((pk.x-p.x)**2+(pk.y-p.y)**2) < 30) {
if(pk.type === 'heal') {
p.hp = Math.min(p.hp + 15, p.maxHp);
game.companions.forEach(c => { if(c.alive) c.hp = Math.min(c.hp+8, c.maxHp); });
spawnBuffText(pk.x, pk.y-10, '💚 全队治疗', '#2ecc71');
}
pk.life = 0;
}
});
pickups = pickups.filter(pk => pk.life > 0);
game.enemies=game.enemies.filter(e=>{if(e.hp<=0){for(let i=0;i<10;i++){const a=Math.random()*Math.PI*2,sp=1+Math.random()*4;particles.push({x:e.x,y:e.y,vx:Math.cos(a)*sp,vy:Math.sin(a)*sp,life:25+Math.random()*15,maxLife:40,size:3+Math.random()*3,color:`hsl(0,${70+Math.random()*30}%,${25+Math.random()*30}%)`,type:'blood'});}onKill(e);if(e.type==='boss'){game.bossActive=false;game.levelComplete=true;}return false;}return true;});
game.companions.forEach(c=>{if(c.hp<=0&&c.alive){c.alive=false;for(let i=0;i<15;i++){const a=Math.random()*Math.PI*2,sp=1+Math.random()*5;particles.push({x:c.x,y:c.y,vx:Math.cos(a)*sp,vy:Math.sin(a)*sp,life:35,maxLife:35,size:3+Math.random()*3,color:c.color,type:'blood'});}screenShake(6);}});
if(!game.levelComplete&&(game.level===1||game.level===2)){if(game.spawnCount>=game.totalToSpawn&&game.enemies.length===0)game.levelComplete=true;}
if(game.levelComplete&&!game.transition){
game.transition=true;game.transitionTimer=120;
if(game.level===1){game.transitionText='从前门离开... 获得手枪!';setTimeout(()=>{if(gameRunning)setupLevel(2);},2000);}
else if(game.level===2){game.transitionText='获得副武器:戒尺!进入第三关...';setTimeout(()=>{if(gameRunning)setupLevel(3);},2000);}
else if(game.level===3){game.transitionText='恭喜通关!';setTimeout(()=>{game.level=4;gameRunning=false;document.getElementById('winScreen').classList.remove('hidden');},2000);}
}
if(p.hp<=0){gameRunning=false;document.getElementById('deathScreen').classList.remove('hidden');}
updateCompanions();
particles.forEach(pt=>{pt.x+=pt.vx||0;pt.y+=pt.vy||0;if(pt.type==='blood'){pt.vx*=0.95;pt.vy*=0.95;pt.vy+=0.05;}pt.life--;});
particles=particles.filter(pt=>pt.life>0);
if(game.shakeTime>0){game.shakeTime--;game.shakeX=(Math.random()-0.5)*game.shakeTime;game.shakeY=(Math.random()-0.5)*game.shakeTime;}else{game.shakeX=0;game.shakeY=0;}
}
function drawRoom(){
const lvl=game.level;
if(lvl===1){
ctx.fillStyle=‘#2c2c2c’;ctx.fillRect(0,0,W,H);ctx.fillStyle=‘#4a4035’;ctx.fillRect(20,20,W-40,H-40);
ctx.strokeStyle=‘#3a3025’;ctx.lineWidth=1;for(let x=20;x<W-20;x+=60){ctx.beginPath();ctx.moveTo(x,20);ctx.lineTo(x,H-20);ctx.stroke();}for(let y=20;y<H-20;y+=60){ctx.beginPath();ctx.moveTo(20,y);ctx.lineTo(W-20,y);ctx.stroke();}
ctx.fillStyle=‘#666’;ctx.fillRect(0,0,W,20);ctx.fillRect(0,H-20,W,20);ctx.fillRect(0,0,20,H);ctx.fillRect(W-20,0,20,H);
ctx.fillStyle=‘#8B4513’;ctx.fillRect(80,0,60,22);ctx.fillStyle=‘#5a2d0c’;ctx.fillRect(85,2,50,18);ctx.fillStyle=‘#fff’;ctx.font=‘10px sans-serif’;ctx.fillText(‘前门’,88,14);
ctx.fillStyle=‘#8B4513’;ctx.fillRect(W-200,H-22,60,22);ctx.fillStyle=‘#5a2d0c’;ctx.fillRect(W-195,H-20,50,18);ctx.fillStyle=‘#fff’;ctx.fillText(‘后门’,W-192,H-7);
[[150,150],[350,150],[550,150],[750,150],[150,300],[350,300],[550,300],[750,300],[150,450],[350,450],[550,450],[750,450]].forEach(([dx,dy])=>{ctx.fillStyle=‘#8B7355’;ctx.fillRect(dx-25,dy-15,50,30);ctx.strokeStyle=‘#6B5335’;ctx.lineWidth=2;ctx.strokeRect(dx-25,dy-15,50,30);});
ctx.fillStyle=‘#2d5a2d’;ctx.fillRect(W/2-100,25,200,50);ctx.strokeStyle=‘#8B7355’;ctx.lineWidth=3;ctx.strokeRect(W/2-100,25,200,50);
ctx.fillStyle=‘#ddd’;ctx.font=‘14px sans-serif’;ctx.textAlign=‘center’;ctx.fillText(‘好好学习 天天向上’,W/2,55);ctx.textAlign=‘left’;
} else if(lvl===2){
ctx.fillStyle=‘#1a1a2e’;ctx.fillRect(0,0,W,H);ctx.fillStyle=‘#3d3d5c’;ctx.fillRect(0,80,W,H-160);
for(let x=0;x<W;x+=80){ctx.fillStyle=x%160===0?’#353550’:‘#3d3d5c’;ctx.fillRect(x,80,80,H-160);}
ctx.fillStyle=‘#4a4a6a’;ctx.fillRect(0,0,W,80);ctx.fillRect(0,H-80,W,80);
for(let x=100;x<W;x+=200){ctx.fillStyle=‘#6B4226’;ctx.fillRect(x-20,55,40,28);ctx.fillStyle=‘#FFD700’;ctx.beginPath();ctx.arc(x+12,70,3,0,Math.PI2);ctx.fill();}
for(let x=150;x<W;x+=200){ctx.fillStyle=‘#6B4226’;ctx.fillRect(x-20,H-83,40,28);}
ctx.fillStyle=‘#2a4a7a’;for(let x=50;x<W;x+=120){ctx.fillRect(x,10,60,35);ctx.strokeStyle=‘#888’;ctx.lineWidth=2;ctx.strokeRect(x,10,60,35);ctx.beginPath();ctx.moveTo(x+30,10);ctx.lineTo(x+30,45);ctx.stroke();}
} else if(lvl===3){
ctx.fillStyle=‘#1a0a0a’;ctx.fillRect(0,0,W,H);
const g=ctx.createRadialGradient(W/2,H/2,50,W/2,H/2,400);g.addColorStop(0,‘#3a2020’);g.addColorStop(1,‘#1a0a0a’);ctx.fillStyle=g;ctx.fillRect(0,0,W,H);
ctx.strokeStyle=‘#8B0000’;ctx.lineWidth=4;ctx.strokeRect(15,15,W-30,H-30);
ctx.fillStyle=‘#4a2020’;ctx.fillRect(60,60,30,30);ctx.fillRect(W-90,60,30,30);ctx.fillRect(60,H-90,30,30);ctx.fillRect(W-90,H-90,30,30);
if(game.bossActive){ctx.strokeStyle=rgba(255,0,0,${0.3+Math.sin(Date.now()*0.005)*0.2});ctx.lineWidth=2;ctx.beginPath();ctx.arc(W/2,H/2,200,0,Math.PI2);ctx.stroke();}
ctx.fillStyle=‘#5a3020’;ctx.fillRect(W/2-80,30,160,40);ctx.fillStyle=‘#8B0000’;ctx.font=‘bold 14px sans-serif’;ctx.textAlign=‘center’;ctx.fillText(‘教导主任办公室’,W/2,55);ctx.textAlign=‘left’;
}
}
function drawPlayer(){ const p=game.player;ctx.save();ctx.translate(p.x,p.y); ctx.globalAlpha=p.invincible>0?(p.invincible%4<2?0.4:1):1; ctx.fillStyle=‘rgba(0,0,0,0.3)‘;ctx.beginPath();ctx.ellipse(0,16,14,6,0,0,Math.PI2);ctx.fill(); ctx.rotate(p.angle);ctx.fillStyle=‘#3498db’;ctx.beginPath();ctx.arc(0,0,14,0,Math.PI2);ctx.fill();ctx.strokeStyle=‘#2980b9’;ctx.lineWidth=2;ctx.stroke(); ctx.fillStyle=‘#fff’;ctx.beginPath();ctx.arc(10,0,4,0,Math.PI*2);ctx.fill(); const w=getCurrentWeapon();if(w){if(w.type===‘gun’){ctx.fillStyle=‘#555’;ctx.fillRect(14,-3,18,6);ctx.fillStyle=‘#333’;ctx.fillRect(28,-2,6,4);}else if(w.name===‘拳头’&&p.fistSwing>0){ctx.fillStyle=‘#fdb’;ctx.beginPath();ctx.arc(20+(12-p.fistSwing)2,0,7,0,Math.PI2);ctx.fill();}} ctx.rotate(-p.angle);
// Army buff glow
if(game.isArmy && game.armyBuffCount > 0) {
ctx.globalAlpha = 0.15 + Math.sin(Date.now()*0.008)*0.1;
ctx.fillStyle = '#f1c40f';
ctx.beginPath(); ctx.arc(0,0,20+game.armyBuffCount*0.3,0,Math.PI*2); ctx.fill();
ctx.globalAlpha = p.invincible>0?(p.invincible%4<2?0.4:1):1;
}
if(p.rulerSwing>0){ctx.save();ctx.rotate(p.angle+(p.rulerSwing/15-0.5)*Math.PI*0.8);ctx.fillStyle='#8B4513';ctx.fillRect(10,-3,45,6);ctx.strokeStyle='#5a2d0c';ctx.lineWidth=1;ctx.strokeRect(10,-3,45,6);ctx.fillStyle='#333';for(let i=0;i<5;i++)ctx.fillRect(15+i*8,-3,1,6);ctx.restore();}
if(p.secondary&&!p.rulerThrown&&p.rulerSwing<=0){ctx.save();ctx.rotate(p.angle+Math.PI+0.3);ctx.fillStyle='#8B4513';ctx.fillRect(5,-2,25,4);ctx.restore();}
if(p.rulerCharging){const pct=Math.min((Date.now()-p.rulerChargeStart)/1000,1);ctx.strokeStyle=pct>0.3?'#ff0':'#888';ctx.lineWidth=3;ctx.beginPath();ctx.arc(0,0,22,0,Math.PI*2*pct);ctx.stroke();}
ctx.globalAlpha=1;ctx.restore();
}
function drawCompanions(){ game.companions.forEach(c=>{ if(!c.alive)return;ctx.save();ctx.translate(c.x,c.y); ctx.globalAlpha=c.invincible>0?(c.invincible%4<2?0.4:1):1; ctx.fillStyle=‘rgba(0,0,0,0.2)‘;ctx.beginPath();ctx.ellipse(0,10,9,3,0,0,Math.PI2);ctx.fill(); ctx.rotate(c.angle);ctx.fillStyle=c.color;ctx.beginPath();ctx.arc(0,0,9,0,Math.PI2);ctx.fill();ctx.strokeStyle=‘rgba(0,0,0,0.3)‘;ctx.lineWidth=1;ctx.stroke(); ctx.fillStyle=‘#fff’;ctx.beginPath();ctx.arc(6,0,2.5,0,Math.PI2);ctx.fill(); if(c.weaponLevel>=3){ctx.fillStyle=‘#444’;ctx.fillRect(8,-2,12,4);}else if(c.weaponLevel>=2){ctx.fillStyle=‘#555’;ctx.fillRect(8,-1.5,10,3);}else{ctx.fillStyle=‘#fdb’;ctx.beginPath();ctx.arc(12,0,3,0,Math.PI2);ctx.fill();} ctx.rotate(-c.angle);ctx.fillStyle=c.color;ctx.font=‘bold 7px sans-serif’;ctx.textAlign=‘center’;ctx.fillText(c.name,0,-14);ctx.textAlign=‘left’;
// Buff glow for army
if(game.isArmy && game.armyBuffCount > 3) {
ctx.globalAlpha = 0.1;
ctx.fillStyle = '#f1c40f';
ctx.beginPath(); ctx.arc(0,0,13,0,Math.PI*2); ctx.fill();
}
ctx.globalAlpha=1;ctx.restore();
});
}
function drawThrownRuler(){ const p=game.player;if(!p.rulerThrown)return; ctx.save();ctx.translate(p.rulerX,p.rulerY);ctx.rotate(p.rulerAngle);ctx.fillStyle=‘#8B4513’;ctx.fillRect(-25,-4,50,8);ctx.strokeStyle=‘#5a2d0c’;ctx.lineWidth=1;ctx.strokeRect(-25,-4,50,8);ctx.fillStyle=‘#333’;for(let i=0;i<6;i++)ctx.fillRect(-20+i8,-4,1,8);ctx.restore(); ctx.save();ctx.globalAlpha=0.3;ctx.fillStyle=p.rulerReturning?’#0f0’:‘#ff0’;ctx.beginPath();ctx.arc(p.rulerX,p.rulerY,15,0,Math.PI2);ctx.fill();ctx.restore(); }
function drawEnemy(e){ ctx.save();ctx.translate(e.x,e.y);const f=e.hit>0; if(e.type===‘boss’){ ctx.fillStyle=‘rgba(0,0,0,0.4)‘;ctx.beginPath();ctx.ellipse(0,28,25,10,0,0,Math.PI2);ctx.fill(); ctx.fillStyle=f?’#fff’:‘#c0392b’;ctx.beginPath();ctx.arc(0,0,25,0,Math.PI2);ctx.fill();ctx.strokeStyle=‘#922’;ctx.lineWidth=3;ctx.stroke(); ctx.fillStyle=‘#fdb’;ctx.beginPath();ctx.arc(0,-5,15,0,Math.PI2);ctx.fill(); ctx.fillStyle=‘#000’;ctx.fillRect(-8,-10,5,4);ctx.fillRect(3,-10,5,4); ctx.strokeStyle=‘#000’;ctx.lineWidth=2;ctx.beginPath();ctx.moveTo(-10,-14);ctx.lineTo(-3,-12);ctx.stroke();ctx.beginPath();ctx.moveTo(10,-14);ctx.lineTo(3,-12);ctx.stroke();ctx.beginPath();ctx.arc(0,-1,5,0,Math.PI);ctx.stroke(); } else { ctx.fillStyle=‘rgba(0,0,0,0.2)‘;ctx.beginPath();ctx.ellipse(0,e.h/2+2,e.w/2,4,0,0,Math.PI2);ctx.fill(); ctx.fillStyle=f?’#fff’:e.color;ctx.beginPath();ctx.arc(0,0,e.w/2,0,Math.PI2);ctx.fill();ctx.strokeStyle=‘rgba(0,0,0,0.3)‘;ctx.lineWidth=1.5;ctx.stroke(); if(e.type===‘student’){ctx.fillStyle=‘#fdb’;ctx.beginPath();ctx.arc(0,-2,e.w/2-4,0,Math.PI2);ctx.fill();ctx.fillStyle=‘#000’;ctx.beginPath();ctx.arc(-4,-4,1.5,0,Math.PI2);ctx.fill();ctx.beginPath();ctx.arc(4,-4,1.5,0,Math.PI2);ctx.fill();ctx.fillStyle=‘#fff’;ctx.fillRect(-5,4,10,5);} else if(e.type===‘teacher’){ctx.fillStyle=‘#fdb’;ctx.beginPath();ctx.arc(0,-3,e.w/2-3,0,Math.PI2);ctx.fill();ctx.strokeStyle=‘#333’;ctx.lineWidth=1;ctx.beginPath();ctx.arc(-5,-4,3,0,Math.PI2);ctx.stroke();ctx.beginPath();ctx.arc(5,-4,3,0,Math.PI2);ctx.stroke();ctx.save();ctx.rotate(Math.atan2(game.player.y-e.y,game.player.x-e.x));ctx.fillStyle=‘#8B4513’;ctx.fillRect(12,-1.5,18,3);ctx.restore();} if(e.hp<e.maxHp){ctx.fillStyle=‘#333’;ctx.fillRect(-12,-e.h/2-8,24,3);ctx.fillStyle=‘#e74c3c’;ctx.fillRect(-12,-e.h/2-8,24(e.hp/e.maxHp),3);} } ctx.restore(); }
function drawBullets(){
bullets.forEach(b=>{ctx.fillStyle=b.color;ctx.beginPath();ctx.arc(b.x,b.y,b.size,0,Math.PI2);ctx.fill();ctx.globalAlpha=0.4;ctx.beginPath();ctx.arc(b.x-b.vx0.5,b.y-b.vy0.5,b.size0.6,0,Math.PI2);ctx.fill();ctx.globalAlpha=1;});
companionBullets.forEach(b=>{ctx.fillStyle=b.color;ctx.beginPath();ctx.arc(b.x,b.y,b.size,0,Math.PI2);ctx.fill();});
enemyBullets.forEach(b=>{ctx.save();ctx.translate(b.x,b.y);ctx.rotate(Math.atan2(b.vy,b.vx));ctx.fillStyle=b.color;ctx.shadowColor=b.color;ctx.shadowBlur=4;ctx.fillRect(-b.size,-b.size/2,b.size*2,b.size);ctx.fillStyle=‘#fff’;ctx.font=${Math.max(5,b.size-2)}px sans-serif;ctx.textAlign=‘center’;ctx.fillText(‘罚’,0,b.size/2-1);ctx.textAlign=‘left’;ctx.shadowBlur=0;ctx.restore();});
}
function drawPickups(){ pickups.forEach(pk=>{ const pulse = 1 + Math.sin(Date.now()0.01)0.3; ctx.globalAlpha = pk.life < 60 ? pk.life/60 : 1; ctx.fillStyle = pk.color; ctx.beginPath(); ctx.arc(pk.x, pk.y, pk.sizepulse, 0, Math.PI2); ctx.fill(); ctx.fillStyle = ‘#fff’; ctx.font = ‘bold 10px sans-serif’; ctx.textAlign = ‘center’; ctx.fillText(’+’, pk.x, pk.y+4); ctx.textAlign = ‘left’; ctx.globalAlpha = 1; }); }
function drawParticles(){
particles.forEach(pt=>{ctx.globalAlpha=pt.life/pt.maxLife;if(pt.type===‘text’){ctx.fillStyle=pt.color;ctx.font=bold ${pt.size}px sans-serif;ctx.textAlign=‘center’;ctx.fillText(pt.text,pt.x,pt.y);ctx.textAlign=‘left’;}else{ctx.fillStyle=pt.color;ctx.beginPath();ctx.arc(pt.x,pt.y,pt.size*(pt.life/pt.maxLife),0,Math.PI*2);ctx.fill();}});ctx.globalAlpha=1;
}
function drawUI(){
const p=game.player;
// Player HP
ctx.fillStyle=‘rgba(0,0,0,0.7)‘;ctx.fillRect(15,H-42,180,22);
ctx.fillStyle=p.hp>30?’#2ecc71’:‘#e74c3c’;ctx.fillRect(16,H-41,178*(p.hp/p.maxHp),20);
ctx.strokeStyle=‘#fff’;ctx.lineWidth=1;ctx.strokeRect(15,H-42,180,22);
ctx.fillStyle=‘#fff’;ctx.font=‘bold 11px sans-serif’;ctx.textAlign=‘center’;
ctx.fillText(HP:${Math.max(0,Math.ceil(p.hp))}/${p.maxHp},105,H-27);
// Army buff counter
if(game.isArmy) {
ctx.fillStyle='rgba(0,0,0,0.7)';ctx.fillRect(15,H-66,180,20);
ctx.fillStyle='#f1c40f';ctx.fillRect(16,H-65,Math.min(178,178*(game.armyBuffCount/50)),18);
ctx.strokeStyle='#f39c12';ctx.lineWidth=1;ctx.strokeRect(15,H-66,180,20);
ctx.fillStyle='#fff';ctx.font='bold 10px sans-serif';ctx.textAlign='center';
ctx.fillText(`⬆强化x${game.armyBuffCount} | 伤害+${p.dmgBonus.toFixed(1)} | 速度+${p.spdBonus.toFixed(2)}`,105,H-52);
}
// Companion HP (compact)
if(game.companions.length > 0) {
const alive = game.companions.filter(c=>c.alive);
const barH = game.isArmy ? 10 : 18;
const barW = game.isArmy ? 52 : 75;
const cols = game.isArmy ? 10 : 5;
let ci = 0;
alive.forEach((c,i) => {
const col = i % cols;
const row = Math.floor(i / cols);
const bx = 205 + col * (barW + 3);
const by = H - 42 - row * (barH + 2);
ctx.fillStyle='rgba(0,0,0,0.6)';ctx.fillRect(bx,by,barW,barH);
ctx.fillStyle=c.hp>c.maxHp*0.3?c.color:'#e74c3c';ctx.fillRect(bx+1,by+1,(barW-2)*(c.hp/c.maxHp),barH-2);
ctx.strokeStyle=c.color;ctx.lineWidth=0.5;ctx.strokeRect(bx,by,barW,barH);
if(!game.isArmy) {
ctx.fillStyle='#fff';ctx.font=`bold ${barH>14?9:7}px sans-serif`;ctx.textAlign='center';
ctx.fillText(`${c.name}:${Math.ceil(c.hp)}`,bx+barW/2,by+barH-3);
} else {
ctx.fillStyle='#fff';ctx.font='bold 6px sans-serif';ctx.textAlign='center';
ctx.fillText(c.name,bx+barW/2,by+barH-2);
}
});
}
// Weapon slots
const slotW=70,slotH=55,startX=W/2-(slotW*3+12)/2;
for(let i=0;i<3;i++){const x=startX+i*(slotW+6),y=H-95;const w=p.weapons[i];ctx.fillStyle=i===p.currentSlot?'rgba(255,255,255,0.2)':'rgba(0,0,0,0.6)';ctx.fillRect(x,y,slotW,slotH);ctx.strokeStyle=i===p.currentSlot?'#fff':'#555';ctx.lineWidth=i===p.currentSlot?2:1;ctx.strokeRect(x,y,slotW,slotH);ctx.fillStyle='#888';ctx.font='9px sans-serif';ctx.textAlign='left';ctx.fillText(`${i+1}`,x+3,y+10);if(w){ctx.font='18px sans-serif';ctx.textAlign='center';ctx.fillText(w.icon,x+slotW/2,y+26);ctx.font='8px sans-serif';ctx.fillStyle='#fff';ctx.fillText(w.name,x+slotW/2,y+38);if(w.type==='gun'){ctx.font='9px sans-serif';ctx.fillStyle=w.currentAmmo<=3?'#e74c3c':'#ff0';ctx.fillText(w.reloading?'换弹...':`${w.currentAmmo}/${w.ammo}`,x+slotW/2,y+50);}else{ctx.font='9px sans-serif';ctx.fillStyle='#aaa';ctx.fillText('∞',x+slotW/2,y+50);}}else{ctx.fillStyle='#444';ctx.font='10px sans-serif';ctx.textAlign='center';ctx.fillText('空',x+slotW/2,y+32);}}
if(p.secondary){const sx=W-85,sy=H-95;ctx.fillStyle='rgba(139,69,19,0.3)';ctx.fillRect(sx,sy,70,slotH);ctx.strokeStyle=p.rulerCharging?'#ff0':'#8B4513';ctx.lineWidth=2;ctx.strokeRect(sx,sy,70,slotH);ctx.font='8px sans-serif';ctx.fillStyle='#aaa';ctx.textAlign='left';ctx.fillText('右键',sx+3,sy+10);ctx.font='18px sans-serif';ctx.textAlign='center';ctx.fillText('📏',sx+35,sy+26);ctx.font='8px sans-serif';ctx.fillStyle='#fff';ctx.fillText('戒尺',sx+35,sy+38);ctx.fillStyle=p.rulerThrown?'#0f0':'#aaa';ctx.fillText(p.rulerThrown?(p.rulerReturning?'返回':'投掷'):'蓄力投',sx+35,sy+50);}
ctx.textAlign='left';ctx.fillStyle='#fff';ctx.font='bold 13px sans-serif';ctx.fillText({1:'第一关:教室',2:'第二关:走廊',3:'第三关:教导主任'}[game.level]||'',15,18);
const ms={single:'单人',double:'双人',quad:'四人',octo:'八人',army:'20人'}[game.mode];
const ds={easy:'简单',normal:'中等',hard:'困难',nightmare:'噩梦'}[game.diff];
ctx.font='10px sans-serif';ctx.fillStyle=game.diff==='nightmare'?'#e74c3c':(game.diff==='hard'?'#9b59b6':(game.diff==='easy'?'#3498db':'#f39c12'));
ctx.fillText(`${ms} / ${ds} | 击杀:${game.killCount} | 存活队友:${game.companions.filter(c=>c.alive).length}/${game.companions.length}`,15,33);
if(game.levelMessageTimer>0){ctx.globalAlpha=game.levelMessageTimer>30?1:game.levelMessageTimer/30;ctx.fillStyle='#fff';ctx.font='bold 26px sans-serif';ctx.textAlign='center';ctx.fillText(game.levelMessage,W/2,H/2-50);ctx.textAlign='left';ctx.globalAlpha=1;}
if(game.transition){ctx.fillStyle='rgba(0,0,0,0.6)';ctx.fillRect(0,0,W,H);ctx.fillStyle='#fff';ctx.font='bold 24px sans-serif';ctx.textAlign='center';ctx.fillText(game.transitionText,W/2,H/2);ctx.textAlign='left';}
if(game.level===3&&game.bossActive){const boss=game.enemies.find(e=>e.type==='boss');if(boss){const bw=380;ctx.fillStyle='rgba(0,0,0,0.7)';ctx.fillRect(W/2-bw/2-5,6,bw+10,22);ctx.fillStyle=boss.hp>boss.maxHp*0.3?'#c0392b':'#ff0';ctx.fillRect(W/2-bw/2,8,bw*(boss.hp/boss.maxHp),18);ctx.strokeStyle='#fff';ctx.lineWidth=1;ctx.strokeRect(W/2-bw/2-5,6,bw+10,22);ctx.fillStyle='#fff';ctx.font='bold 11px sans-serif';ctx.textAlign='center';ctx.fillText(`教导主任 ${Math.ceil(boss.hp)}/${Math.ceil(boss.maxHp)}`,W/2,22);ctx.textAlign='left';}}
ctx.strokeStyle='rgba(255,255,255,0.5)';ctx.lineWidth=1;const cx=mouse.x,cy=mouse.y;ctx.beginPath();ctx.arc(cx,cy,8,0,Math.PI*2);ctx.stroke();ctx.beginPath();ctx.moveTo(cx-12,cy);ctx.lineTo(cx-5,cy);ctx.moveTo(cx+5,cy);ctx.lineTo(cx+12,cy);ctx.moveTo(cx,cy-12);ctx.lineTo(cx,cy-5);ctx.moveTo(cx,cy+5);ctx.lineTo(cx,cy+12);ctx.stroke();
}
function draw(){ctx.save();ctx.translate(game.shakeX,game.shakeY);drawRoom();game.enemies.forEach(e=>drawEnemy(e));drawPlayer();drawCompanions();drawThrownRuler();drawBullets();drawPickups();drawParticles();ctx.restore();drawUI();} function gameLoop(){if(!gameRunning)return;update(1/60);draw();animFrame=requestAnimationFrame(gameLoop);}
function startGame(){ document.getElementById(‘startScreen’).classList.add(‘hidden’); document.getElementById(‘winScreen’).classList.add(‘hidden’); document.getElementById(‘deathScreen’).classList.add(‘hidden’); if(animFrame)cancelAnimationFrame(animFrame); initGame(selectedMode,selectedDiff);gameRunning=true;gameLoop(); } function showStart(){document.getElementById(‘winScreen’).classList.add(‘hidden’);document.getElementById(‘deathScreen’).classList.add(‘hidden’);document.getElementById(‘startScreen’).classList.remove(‘hidden’);gameRunning=false;}
ctx.fillStyle=‘#111’;ctx.fillRect(0,0,W,H);
</body> </html>