族群之争 · 电子斗蛐蛐
by EpicCoder88550 lines33.0 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>族群之争 · 电子斗蛐蛐</title>
<style>
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
body {
margin: 0; min-height: 100vh; background: #15171a;
display: flex; flex-direction: column; align-items: center; justify-content: center;
font-family: system-ui, -apple-system, sans-serif; color: #fff; overflow: hidden;
}
.game-wrapper { position: relative; width: 100vw; max-width: 368px; aspect-ratio: 1 / 1; box-shadow: 0 12px 48px rgba(0,0,0,.65); background: #2b2119; }
canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(21, 23, 26, 0.95); display: flex; flex-direction: column; align-items: center; justify-content: center; z-index: 10; transition: opacity 0.3s; }
.overlay.hidden { opacity: 0; pointer-events: none; }
h1 { font-size: 24px; letter-spacing: 2px; margin-bottom: 5px; color: #e8d39a; }
.subtitle { font-size: 13px; color: #9fb3c8; margin-bottom: 20px; }
.char-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 15px; margin-bottom: 30px; }
.char-card { width: 100px; height: 120px; background: #232830; border: 2px solid #39414d; border-radius: 10px; display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: pointer; transition: all 0.2s; user-select: none; }
.char-card:active { transform: scale(0.95); }
.char-card.selected { border-color: #e08b3a; background: #3a2e21; box-shadow: 0 0 15px rgba(224, 139, 58, 0.4); }
.char-icon { width: 50px; height: 50px; border-radius: 50%; margin-bottom: 10px; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 18px; color: #111; }
.char-name { font-size: 14px; font-weight: bold; }
button { background: #4ade80; color: #000; border: none; border-radius: 8px; padding: 12px 40px; font-size: 16px; font-weight: bold; cursor: pointer; transition: all 0.2s; opacity: 0.5; pointer-events: none; }
button.enabled { opacity: 1; pointer-events: auto; box-shadow: 0 4px 15px rgba(74, 222, 128, 0.4); }
#game-hud { position: absolute; top: 10px; left: 0; width: 100%; text-align: center; pointer-events: none; font-size: 18px; font-weight: bold; color: #fff; text-shadow: 0 2px 4px #000; opacity: 0; transition: 0.3s; }
#game-hud.show { opacity: 1; }
#result-layer { background: rgba(21, 23, 26, 0.85); backdrop-filter: blur(3px); }
#winner-text { font-size: 28px; color: #fbbf24; margin-bottom: 40px; text-shadow: 0 0 10px rgba(251, 191, 36, 0.5); }
</style>
</head>
<body>
<div class="game-wrapper">
<canvas id="game"></canvas>
<div id="game-hud">战斗中...</div>
<div id="ui-layer" class="overlay">
<h1>族群之争</h1>
<div class="subtitle">请选择两名武士出战</div>
<div class="char-grid">
<div class="char-card" data-char="鹰霜"><div class="char-icon" style="background:#e08b3a;">🦅</div><div class="char-name">鹰霜</div></div>
<div class="char-card" data-char="黄牙"><div class="char-icon" style="background:#e8d39a;">🌼</div><div class="char-name">黄牙</div></div>
<div class="char-card" data-char="火星"><div class="char-icon" style="background:#f97316;">🔥</div><div class="char-name">火星</div></div>
<div class="char-card" data-char="长鞭"><div class="char-icon" style="background:#1a1a1a; color:#ff0000;">🩸</div><div class="char-name">长鞭</div></div>
<div class="char-card" data-char="灰翅"><div class="char-icon" style="background:#9ca3af;">🕊️</div><div class="char-name">灰翅</div></div>
<div class="char-card" data-char="根泉"><div class="char-icon" style="background:#4b5563;">🌿</div><div class="char-name">根泉</div></div>
</div>
<button id="btn-start">开始战斗</button>
</div>
<div id="result-layer" class="overlay hidden">
<div id="winner-text">胜者:火星</div>
<button id="btn-restart" class="enabled">重新选择</button>
</div>
</div>
<script>
(() => {
const CELL = 32, COLS = 10, ROWS = 10, WALL = 24;
const ARENA_W = CELL * COLS, ARENA_H = CELL * ROWS;
const CANVAS_W = ARENA_W + WALL * 2, CANVAS_H = ARENA_H + WALL * 2;
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const dpr = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = CANVAS_W * dpr; canvas.height = CANVAS_H * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
let entities = [], traps = [], projectiles = [], groundEffects = [], floatingTexts = [], effects = [];
let paused = true, gameStarted = false, selectedChars = [];
let teamA = null, teamB = null;
class Cat {
constructor(name, team, x, y, color, hp, isRanged, isMinion = false) {
this.name = name; this.team = team;
this.x = x; this.y = y; this.color = color;
this.maxHp = hp; this.hp = hp; this.r = isMinion ? 8 : 14;
this.isRanged = isRanged; this.isMinion = isMinion; this.alive = true;
this.baseSpeed = 75;
if (isRanged) this.baseSpeed = 65;
if (name === '火星') this.baseSpeed = 85;
if (name === '长鞭') this.baseSpeed = 90;
if (name === '灰翅') this.baseSpeed = 60;
if (name === '根泉') this.baseSpeed = 85;
this.speed = this.baseSpeed;
this.atkCd = 0; this.skillCd = 0;
this.atkRange = isRanged ? 200 : 32; // 黄牙默认200
if (name === '长鞭') this.atkRange = 16;
if (name === '灰翅') this.atkRange = 240; // ★ 灰翅射程提升至 240
this.atkDamage = 30;
if (name === '火星') this.atkDamage = 28;
if (name === '长鞭') this.atkDamage = 150;
if (name === '灰翅') this.atkDamage = 20;
if (name === '根泉') this.atkDamage = 35;
if (isMinion) this.atkDamage = 45;
this.scoutRange = 140;
if (name === '火星') this.scoutRange = 180;
if (name === '长鞭') this.scoutRange = 150;
if (name === '灰翅') this.scoutRange = 280; // ★ 灰翅视野提升至 280
if (name === '根泉') this.scoutRange = 160;
if (isMinion) this.scoutRange = 120;
this.wanderAngle = Math.random() * Math.PI * 2;
this.wanderDistRemaining = 0;
this.slowPercent = 0; this.slowTimer = 0;
this.poisonTimer = 0; this.poisonTickTimer = 0;
this.trapDebuffs = [];
this.knockbackVx = 0; this.knockbackVy = 0;
this.hitFlashTimer = 0;
this.lockOnTimer = 0; this.lockOnTarget = null;
// 暴击系统
this.critRate = 0.1; this.critMult = 1.5;
if (name === '长鞭') { this.critRate = 0.05; this.critMult = 2.0; }
if (name === '根泉') { this.critRate = 0.2; this.critMult = 1.8; }
if (name === '灰翅') { this.critRate = 0.15; this.critMult = 2.0; }
if (name === '火星') { this.critRate = 0.1; this.critMult = 1.5; }
// 专属状态
if (name === '火星') { this.hasRevived = false; this.invincibleTimer = 0; this.dashTimer = 0; this.damageDealt = 0; this.leaderBuff = 0; }
if (name === '长鞭') { this.invincibleTimer = 0; this.damageTakenTracker = 0; }
if (name === '灰翅') { this.asthmaTimer = 10; this.isAsthma = false; }
if (name === '根泉') { this.damageTakenTracker = 0; this.possessedTimer = 0; this.nextHitGuaranteedCrit = false; this.markedTarget = null; this.markTimer = 0; }
}
takeDamage(amount, isPoison = false, attacker = null) {
if (!this.alive || this.invincibleTimer > 0) return;
if (this.name === '长鞭' && !isPoison && Math.random() < 0.3) {
let angle = attacker ? Math.atan2(this.y - attacker.y, this.x - attacker.x) : Math.random() * Math.PI * 2;
this.knockbackVx = Math.cos(angle) * 800; this.knockbackVy = Math.sin(angle) * 800;
this.invincibleTimer = 0.3;
floatingTexts.push({ x: this.x, y: this.y - 30, text: "闪避!", timer: 0.8, maxTimer: 0.8, color: '#a855f7' });
effects.push({ type: 'trail', x: this.x, y: this.y, timer: 0.4, maxTimer: 0.4, color: 'rgba(26, 26, 26, 0.8)' });
return;
}
this.hp -= amount;
if (!isPoison) {
this.hitFlashTimer = 0.15;
if (!this.isRanged && attacker && attacker.isRanged && !this.isMinion) {
this.lockOnTimer = 3.0; this.lockOnTarget = attacker;
floatingTexts.push({ x: this.x, y: this.y - 30, text: "锁定!", timer: 0.8, maxTimer: 0.8, color: '#ff4d4d' });
}
if (this.isRanged && attacker && Math.random() < 0.3) {
let angle = Math.atan2(this.y - attacker.y, this.x - attacker.x);
this.knockbackVx = Math.cos(angle) * 450; this.knockbackVy = Math.sin(angle) * 450;
floatingTexts.push({ x: this.x, y: this.y - 30, text: "拉开距离!", timer: 0.8, maxTimer: 0.8, color: '#9fb3c8' });
} else {
let angle = Math.random() * Math.PI * 2;
this.knockbackVx = Math.cos(angle) * 300; this.knockbackVy = Math.sin(angle) * 300;
}
effects.push({ type: 'ring', x: this.x, y: this.y, timer: 0.2, maxTimer: 0.2, color: '#fff' });
}
floatingTexts.push({ x: this.x + (Math.random() * 20 - 10), y: this.y - 20 + (Math.random() * 10 - 5), text: Math.round(amount), timer: 0.8, maxTimer: 0.8, color: isPoison ? '#d946ef' : (amount > 200 ? '#ff0000' : '#ff4d4d'), isBig: amount > 200 });
if (this.name === '长鞭') { this.damageTakenTracker += amount; while (this.damageTakenTracker >= 100) { this.damageTakenTracker -= 100; this.summonMinions(); } }
if (this.name === '根泉') {
this.damageTakenTracker += amount;
if (this.damageTakenTracker >= 150 && this.possessedTimer <= 0) {
this.damageTakenTracker -= 150;
this.possessedTimer = 2.0; this.nextHitGuaranteedCrit = true;
floatingTexts.push({ x: this.x, y: this.y - 40, text: "火星附身!", timer: 1.2, maxTimer: 1.2, color: '#f97316', isBig: true });
effects.push({ type: 'revive', x: this.x, y: this.y, timer: 0.8, maxTimer: 0.8 });
}
}
if (this.hp <= 0) {
if (this.name === '火星' && !this.hasRevived) {
this.hasRevived = true; this.hp = 1; this.invincibleTimer = 2.0;
this.slowTimer = 0; this.slowPercent = 0; this.poisonTimer = 0; this.trapDebuffs = [];
effects.push({ type: 'revive', x: this.x, y: this.y, timer: 1.0, maxTimer: 1.0 });
floatingTexts.push({ x: this.x, y: this.y - 40, text: "九命之韧!", timer: 1.5, maxTimer: 1.5, color: '#fbbf24', isBig: true });
} else { this.hp = 0; this.alive = false; checkGameOver(); }
}
}
summonMinions() {
let currentMinions = entities.filter(e => e.isMinion && e.team === this.team && e.alive).length;
if (currentMinions >= 8) return;
for (let i = 0; i < 2; i++) {
let angle = Math.random() * Math.PI * 2;
let dist = 30 + Math.random() * 20;
let mx = this.x + Math.cos(angle) * dist, my = this.y + Math.sin(angle) * dist;
mx = Math.max(10, Math.min(ARENA_W - 10, mx)); my = Math.max(10, Math.min(ARENA_H - 10, my));
entities.push(new Cat('血族', this.team, mx, my, '#1a1a1a', 100, false, true));
effects.push({ type: 'ring', x: mx, y: my, timer: 0.4, maxTimer: 0.4, color: '#ff0000' });
}
floatingTexts.push({ x: this.x, y: this.y - 40, text: "血族降临!", timer: 1.0, maxTimer: 1.0, color: '#ff0000' });
}
update(dt) {
if (!this.alive) return;
if (this.hitFlashTimer > 0) this.hitFlashTimer -= dt;
if (this.invincibleTimer > 0) this.invincibleTimer -= dt;
if (this.slowTimer > 0) this.slowTimer -= dt; else this.slowPercent = 0;
if (this.lockOnTimer > 0) { this.lockOnTimer -= dt; if (this.lockOnTimer <= 0 || !this.lockOnTarget || !this.lockOnTarget.alive) { this.lockOnTimer = 0; this.lockOnTarget = null; } }
if (this.name === '灰翅') {
if (!this.isAsthma) {
this.asthmaTimer -= dt;
if (this.asthmaTimer <= 0) { this.isAsthma = true; this.asthmaTimer = 2.0; floatingTexts.push({ x: this.x, y: this.y - 30, text: "哮喘发作...", timer: 1.0, maxTimer: 1.0, color: '#9ca3af' }); }
} else {
this.asthmaTimer -= dt;
if (this.asthmaTimer <= 0) { this.isAsthma = false; this.asthmaTimer = 10.0; }
}
}
if (this.name === '根泉' && this.possessedTimer > 0) {
this.possessedTimer -= dt;
if (this.possessedTimer <= 0) this.nextHitGuaranteedCrit = false;
}
if (Math.abs(this.knockbackVx) > 1 || Math.abs(this.knockbackVy) > 1) {
this.x += this.knockbackVx * dt; this.y += this.knockbackVy * dt;
this.knockbackVx *= Math.pow(0.001, dt); this.knockbackVy *= Math.pow(0.001, dt);
} else { this.knockbackVx = 0; this.knockbackVy = 0; }
let nearestEnemy = null, minDist = Infinity;
for (let e of entities) { if (e.team !== this.team && e.alive) { let d = Math.hypot(e.x - this.x, e.y - this.y); if (d < minDist) { minDist = d; nearestEnemy = e; } } }
let inSight = nearestEnemy && minDist < this.scoutRange;
let currentSpeed = this.speed * (1 - this.slowPercent);
if (this.invincibleTimer > 0 && this.name === '火星') currentSpeed *= 1.5;
if (this.dashTimer > 0 && this.name === '火星') currentSpeed *= 3.0;
if (this.possessedTimer > 0 && this.name === '根泉') currentSpeed *= 2.0;
if (this.isAsthma && this.name === '灰翅') currentSpeed *= 0.5;
let moveTarget = this.lockOnTimer > 0 && this.lockOnTarget ? this.lockOnTarget : (!this.isRanged && inSight && nearestEnemy ? nearestEnemy : null);
if (moveTarget) {
let dx = moveTarget.x - this.x, dy = moveTarget.y - this.y, dist = Math.hypot(dx, dy);
if (this.name === '火星' && this.dashTimer <= 0 && this.skillCd <= 0 && dist > this.atkRange) {
this.dashTimer = 0.4; this.skillCd = 8.0;
floatingTexts.push({ x: this.x, y: this.y - 30, text: "猛虎扑击!", timer: 0.8, maxTimer: 0.8, color: '#f97316' });
}
if (this.dashTimer > 0) {
this.dashTimer -= dt;
if (Math.random() < 0.6) effects.push({ type: 'trail', x: this.x, y: this.y, timer: 0.3, maxTimer: 0.3, color: 'rgba(249, 115, 22, 0.6)' });
this.x += (dx / dist) * currentSpeed * dt; this.y += (dy / dist) * currentSpeed * dt;
} else if (dist > this.atkRange * 0.8) {
this.x += (dx / dist) * currentSpeed * dt; this.y += (dy / dist) * currentSpeed * dt;
}
} else {
if (this.wanderDistRemaining <= 0) { this.wanderAngle = Math.random() * Math.PI * 2; this.wanderDistRemaining = 10 + Math.random() * 40; }
let step = currentSpeed * 0.6 * dt; if (step > this.wanderDistRemaining) step = this.wanderDistRemaining;
let nextX = this.x + Math.cos(this.wanderAngle) * step, nextY = this.y + Math.sin(this.wanderAngle) * step;
if (nextX < this.r || nextX > ARENA_W - this.r || nextY < this.r || nextY > ARENA_H - this.r) this.wanderDistRemaining = 0;
else { this.x = nextX; this.y = nextY; this.wanderDistRemaining -= step; }
}
if (this.trapDebuffs.length > 0 && this.dashTimer <= 0 && this.invincibleTimer <= 0) {
for (let iter = 0; iter < 5; iter++) { for (let debuff of this.trapDebuffs) { let tDist = Math.hypot(this.x - debuff.center.x, this.y - debuff.center.y); if (tDist > 128) { let angle = Math.atan2(this.y - debuff.center.y, this.x - debuff.center.x); this.x = debuff.center.x + Math.cos(angle) * 128; this.y = debuff.center.y + Math.sin(angle) * 128; } } }
}
this.x = Math.max(this.r, Math.min(ARENA_W - this.r, this.x));
this.y = Math.max(this.r, Math.min(ARENA_H - this.r, this.y));
this.atkCd -= dt; this.skillCd -= dt;
if (nearestEnemy && minDist <= this.atkRange && this.atkCd <= 0 && !(this.isAsthma && this.name === '灰翅')) {
let slashAngle = Math.atan2(nearestEnemy.y - this.y, nearestEnemy.x - this.x);
let slashColor = '#e08b3a';
if (this.name === '火星') slashColor = '#f97316';
if (this.name === '长鞭') slashColor = '#ff0000';
if (this.name === '根泉') slashColor = '#4b5563';
if (this.isMinion) slashColor = '#333';
if (this.name === '灰翅') {
this.atkCd = 1.5;
let isCrit = Math.random() < (this.critRate + (nearestEnemy.markedTarget === this ? 0.15 : 0));
nearestEnemy.takeDamage(this.atkDamage * (isCrit ? this.critMult : 1), false, this);
nearestEnemy.markedTarget = this; nearestEnemy.markTimer = 3.0;
effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: isCrit ? '#fbbf24' : '#9ca3af' });
if (isCrit) floatingTexts.push({ x: nearestEnemy.x, y: nearestEnemy.y - 30, text: "战略暴击!", timer: 0.8, maxTimer: 0.8, color: '#fbbf24' });
}
else if (this.name === '根泉') {
this.atkCd = 1.0;
let isCrit = this.nextHitGuaranteedCrit || Math.random() < this.critRate;
let finalDmg = isCrit ? this.atkDamage * (this.nextHitGuaranteedCrit ? 3.0 : this.critMult) : this.atkDamage;
if (this.nextHitGuaranteedCrit) this.nextHitGuaranteedCrit = false;
nearestEnemy.takeDamage(finalDmg, false, this);
effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.25, maxTimer: 0.25, color: isCrit ? '#fbbf24' : '#4b5563' });
if (isCrit) {
floatingTexts.push({ x: nearestEnemy.x, y: nearestEnemy.y - 30, text: "亡魂暴击!", timer: 0.8, maxTimer: 0.8, color: '#fbbf24' });
effects.push({ type: 'trail', x: nearestEnemy.x, y: nearestEnemy.y, timer: 0.6, maxTimer: 0.6, color: 'rgba(75, 85, 99, 0.6)' });
nearestEnemy.takeDamage(30, true);
}
}
else if (this.name === '长鞭') {
this.atkCd = 1.2;
let hpRatio = this.hp / this.maxHp;
let currentCrit = this.critRate + (1 - hpRatio) * 0.45;
let isCrit = Math.random() < currentCrit;
let finalDmg = isCrit ? 340 : 150;
nearestEnemy.takeDamage(finalDmg, false, this);
effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.3, maxTimer: 0.3, color: isCrit ? '#ff0000' : slashColor });
if (isCrit) effects.push({ type: 'ring', x: nearestEnemy.x, y: nearestEnemy.y, timer: 0.3, maxTimer: 0.3, color: '#ff0000' });
}
else if (this.name === '火星') {
this.atkCd = 1.0;
let isCrit = Math.random() < this.critRate;
let baseDmg = 28 + 28 * 0.05 * this.leaderBuff;
let finalDmg = Math.round(baseDmg * (isCrit ? this.critMult : 1));
nearestEnemy.takeDamage(finalDmg, false, this);
effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: isCrit ? '#fbbf24' : slashColor });
this.damageDealt += finalDmg;
let newBuff = Math.floor(this.damageDealt / 100);
if (newBuff > this.leaderBuff) { this.leaderBuff = newBuff; floatingTexts.push({ x: this.x, y: this.y - 20, text: `领袖+${this.leaderBuff}`, timer: 1.0, maxTimer: 1.0, color: '#fbbf24' }); }
}
else if (this.name === '鹰霜') {
this.atkCd = 1.0;
nearestEnemy.takeDamage(this.atkDamage, false, this);
effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: slashColor });
}
else if (this.isRanged) {
this.atkCd = 1.5;
let dx = nearestEnemy.x - this.x, dy = nearestEnemy.y - this.y, dist = Math.hypot(dx, dy);
// ★ 灰翅的弹道变慢(因为是老头),黄牙正常
let bulletSpeed = this.name === '灰翅' ? 200 : 250;
projectiles.push({ x: this.x, y: this.y, vx: (dx / dist) * bulletSpeed, vy: (dy / dist) * bulletSpeed, damage: this.atkDamage, type: 'berry', owner: this, life: 3.0 });
}
else if (this.isMinion) {
this.atkCd = 1.0;
nearestEnemy.takeDamage(45, false, this);
effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: '#333' });
}
}
if (this.name === '鹰霜' && this.skillCd <= 0) {
this.skillCd = 3.0;
if (Math.random() < 0.8 && traps.length < 6) traps.push({ x: this.x, y: this.y, r: 25, armed: true, owner: this });
}
}
draw() {
if (!this.alive) return;
let sx = WALL + this.x, sy = WALL + this.y;
if (this.trapDebuffs.length > 0 && this.invincibleTimer <= 0 && this.dashTimer <= 0) { sx += (Math.random() - 0.5) * 4; sy += (Math.random() - 0.5) * 4; }
if (this.lockOnTimer > 0 && this.lockOnTarget) {
ctx.strokeStyle = `rgba(255, 50, 50, 0.5)`; ctx.lineWidth = 2; ctx.setLineDash([5, 5]);
ctx.beginPath(); ctx.moveTo(sx, sy); ctx.lineTo(WALL + this.lockOnTarget.x, WALL + this.lockOnTarget.y); ctx.stroke(); ctx.setLineDash([]);
}
if (!this.isMinion) {
ctx.fillStyle = 'white'; ctx.font = 'bold 12px sans-serif'; ctx.textAlign = 'center';
ctx.fillText(`${this.name} (${Math.ceil(this.hp)})`, sx, sy - this.r - 15);
ctx.fillStyle = 'rgba(0,0,0,0.6)'; ctx.fillRect(sx - 20, sy - this.r - 10, 40, 5);
ctx.fillStyle = this.hp > 300 ? '#4ade80' : '#ef4444'; ctx.fillRect(sx - 20, sy - this.r - 10, 40 * (this.hp / this.maxHp), 5);
} else {
ctx.fillStyle = 'rgba(0,0,0,0.6)'; ctx.fillRect(sx - 10, sy - this.r - 6, 20, 3);
ctx.fillStyle = '#ef4444'; ctx.fillRect(sx - 10, sy - this.r - 6, 20 * (this.hp / this.maxHp), 3);
}
ctx.beginPath(); ctx.arc(sx, sy, this.r, 0, Math.PI * 2);
ctx.fillStyle = this.hitFlashTimer > 0 ? '#ffffff' : this.color; ctx.fill();
ctx.strokeStyle = this.isMinion ? '#ff0000' : '#000'; ctx.lineWidth = this.isMinion ? 1 : 2; ctx.stroke();
if (this.invincibleTimer > 0) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 4, 0, Math.PI * 2); ctx.strokeStyle = `rgba(255, 255, 255, ${0.5 + 0.5 * Math.sin(Date.now() / 100)})`; ctx.lineWidth = 3; ctx.stroke(); }
else if (this.dashTimer > 0) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 6, 0, Math.PI * 2); ctx.strokeStyle = 'rgba(249, 115, 22, 0.8)'; ctx.lineWidth = 4; ctx.stroke(); }
if (this.possessedTimer > 0) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 5, 0, Math.PI * 2); ctx.strokeStyle = `rgba(249, 115, 22, ${0.6 + 0.4 * Math.sin(Date.now() / 80)})`; ctx.lineWidth = 3; ctx.stroke(); }
if (this.isAsthma) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 3, 0, Math.PI * 2); ctx.strokeStyle = 'rgba(156, 163, 175, 0.7)'; ctx.lineWidth = 2; ctx.setLineDash([3, 3]); ctx.stroke(); ctx.setLineDash([]); }
if (this.slowTimer > 0) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 2, 0, Math.PI * 2); ctx.strokeStyle = 'rgba(168, 85, 247, 0.8)'; ctx.lineWidth = 2; ctx.stroke(); }
for (let debuff of this.trapDebuffs) {
ctx.beginPath(); ctx.arc(WALL + debuff.center.x, WALL + debuff.center.y, 128, 0, Math.PI * 2);
let alpha = 0.15 + 0.15 * Math.abs(Math.sin(debuff.timeLeft * 5));
ctx.strokeStyle = `rgba(255, 50, 50, ${alpha})`; ctx.lineWidth = 1.5; ctx.setLineDash([5, 5]); ctx.stroke(); ctx.setLineDash([]);
}
}
}
function startBattle() {
if (selectedChars.length < 2) return;
gameStarted = true;
document.getElementById('ui-layer').classList.add('hidden');
document.getElementById('result-layer').classList.add('hidden');
document.getElementById('game-hud').classList.add('show');
entities = [];
let c1 = selectedChars[0], c2 = selectedChars[1];
function createChar(name, team, x, y) {
if (name === '鹰霜') return new Cat('鹰霜', team, x, y, '#e08b3a', 1030, false);
if (name === '黄牙') return new Cat('黄牙', team, x, y, '#e8d39a', 1000, true);
if (name === '火星') return new Cat('火星', team, x, y, '#f97316', 1050, false);
if (name === '长鞭') return new Cat('长鞭', team, x, y, '#1a1a1a', 550, false);
if (name === '灰翅') return new Cat('灰翅', team, x, y, '#9ca3af', 1000, true);
if (name === '根泉') return new Cat('根泉', team, x, y, '#4b5563', 1000, false);
}
teamA = createChar(c1, 'A', 60, 60); teamB = createChar(c2, 'B', 260, 260);
entities.push(teamA); entities.push(teamB);
traps = []; projectiles = []; groundEffects = []; floatingTexts = []; effects = [];
paused = false;
document.getElementById('game-hud').innerText = '战斗中...';
}
function update(dt) {
if (paused || !gameStarted) return;
for (let entity of entities) { if (entity.alive) entity.update(dt); }
for (let e of entities) { if (e.markTimer > 0) { e.markTimer -= dt; if (e.markTimer <= 0) e.markedTarget = null; } }
for (let i = traps.length - 1; i >= 0; i--) {
let t = traps[i]; if (!t.armed) continue;
for (let entity of entities) {
if (!entity.alive || entity.team === t.owner.team || entity.invincibleTimer > 0) continue;
if (Math.hypot(entity.x - t.x, entity.y - t.y) < entity.r + t.r) {
t.armed = false; traps.splice(i, 1);
entity.trapDebuffs.push({ center: { x: t.x, y: t.y }, timeLeft: 5.0, tickTimer: 1.0, tickCount: 0 });
entity.slowPercent = 0.3; entity.slowTimer = 5.0;
effects.push({ type: 'ring', x: t.x, y: t.y, timer: 0.3, maxTimer: 0.3, color: '#ef4444' });
}
}
}
for (let i = projectiles.length - 1; i >= 0; i--) {
let p = projectiles[i]; p.life -= dt;
p.x += p.vx * dt; p.y += p.vy * dt;
if (p.x < 0 || p.x > ARENA_W || p.y < 0 || p.y > ARENA_H || p.life <= 0) { projectiles.splice(i, 1); continue; }
let hit = false;
for (let entity of entities) {
if (entity.alive && entity.team !== p.owner.team && Math.hypot(entity.x - p.x, entity.y - p.y) < entity.r + 6) {
entity.takeDamage(p.damage, false, p.owner);
entity.poisonTimer = 5.0; entity.poisonTickTimer = 1.0;
groundEffects.push({ x: entity.x, y: entity.y, r: 30, timer: 20, owner: p.owner, seed: Math.random() });
effects.push({ type: 'ring', x: entity.x, y: entity.y, timer: 0.3, maxTimer: 0.3, color: '#d946ef' });
hit = true; break;
}
}
if (hit) projectiles.splice(i, 1);
}
for (let i = groundEffects.length - 1; i >= 0; i--) {
let g = groundEffects[i]; g.timer -= dt;
if (g.timer <= 0) { groundEffects.splice(i, 1); continue; }
if (Math.random() < 0.05) effects.push({ type: 'bubble', x: g.x + (Math.random() - 0.5) * g.r * 1.5, y: g.y + (Math.random() - 0.5) * g.r * 1.5, timer: 0.6, maxTimer: 0.6 });
for (let entity of entities) {
if (!entity.alive || entity.team === g.owner.team || entity.invincibleTimer > 0) continue;
if (Math.hypot(entity.x - g.x, entity.y - g.y) < entity.r + g.r) {
if (entity.slowPercent < 0.2) { entity.slowPercent = 0.2; entity.slowTimer = 0.5; }
if (!entity.juiceTick) entity.juiceTick = 0;
entity.juiceTick -= dt;
if (entity.juiceTick <= 0) { entity.juiceTick = 1.0; entity.takeDamage(1, true); }
}
}
}
for (let i = entities.length - 1; i >= 0; i--) { if (!entities[i].alive && entities[i].isMinion) entities.splice(i, 1); }
for (let i = effects.length - 1; i >= 0; i--) { effects[i].timer -= dt; if (effects[i].timer <= 0) effects.splice(i, 1); }
for (let i = floatingTexts.length - 1; i >= 0; i--) { let ft = floatingTexts[i]; ft.y -= 30 * dt; ft.timer -= dt; if (ft.timer <= 0) floatingTexts.splice(i, 1); }
}
function draw() {
ctx.fillStyle = '#2b2119'; ctx.fillRect(0, 0, CANVAS_W, CANVAS_H);
ctx.fillStyle = '#55734a'; ctx.fillRect(WALL, WALL, ARENA_W, ARENA_H);
ctx.strokeStyle = 'rgba(255,255,255,0.075)'; ctx.beginPath();
for (let i = 1; i < COLS; i++) { ctx.moveTo(WALL + i * CELL, WALL); ctx.lineTo(WALL + i * CELL, WALL + ARENA_H); }
for (let j = 1; j < ROWS; j++) { ctx.moveTo(WALL, WALL + j * CELL); ctx.lineTo(WALL + ARENA_W, WALL + j * CELL); }
ctx.stroke();
for (let g of groundEffects) {
ctx.fillStyle = 'rgba(126, 34, 206, 0.5)'; ctx.beginPath();
for (let a = 0; a < Math.PI * 2; a += Math.PI / 4) { let r = g.r * (0.8 + Math.sin(a * 3 + g.seed) * 0.2); let px = WALL + g.x + Math.cos(a) * r, py = WALL + g.y + Math.sin(a) * r; if (a === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py); }
ctx.closePath(); ctx.fill();
}
for (let t of traps) {
ctx.beginPath(); ctx.arc(WALL + t.x, WALL + t.y, t.r, 0, Math.PI * 2); ctx.fillStyle = 'rgba(239, 68, 68, 0.3)'; ctx.fill();
let pulse = 1 + Math.sin(Date.now() / 150) * 0.1; ctx.beginPath();
for (let i = 0; i < 6; i++) { let ang = i * Math.PI / 3; let px = WALL + t.x + Math.cos(ang) * t.r * 0.6 * pulse, py = WALL + t.y + Math.sin(ang) * t.r * 0.6 * pulse; if (i === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py); }
ctx.closePath(); ctx.strokeStyle = '#ef4444'; ctx.lineWidth = 2; ctx.stroke();
}
for (let p of projectiles) {
ctx.beginPath(); ctx.arc(WALL + p.x, WALL + p.y, 6, 0, Math.PI * 2); ctx.fillStyle = '#d946ef'; ctx.fill(); ctx.strokeStyle = '#4a044e'; ctx.lineWidth = 1; ctx.stroke();
let speed = Math.hypot(p.vx, p.vy); let trailX = WALL + p.x - (p.vx / speed) * 8, trailY = WALL + p.y - (p.vy / speed) * 8;
ctx.beginPath(); ctx.arc(trailX, trailY, 3, 0, Math.PI * 2); ctx.fillStyle = 'rgba(217, 70, 239, 0.4)'; ctx.fill();
}
if (gameStarted) for (let entity of entities) entity.draw();
for (let e of effects) {
let progress = 1 - (e.timer / e.maxTimer);
if (e.type === 'slash') {
ctx.save(); ctx.translate(WALL + e.x, WALL + e.y); ctx.rotate(e.angle); ctx.strokeStyle = e.color; ctx.globalAlpha = 1 - progress; ctx.lineWidth = 3; ctx.lineCap = 'round';
for (let i = -1; i <= 1; i++) { ctx.beginPath(); ctx.moveTo(0, i * 8 - 5); ctx.quadraticCurveTo(15, i * 8 + 5, 30, i * 8 - 5); ctx.stroke(); }
ctx.restore();
} else if (e.type === 'trail') { ctx.beginPath(); ctx.arc(WALL + e.x, WALL + e.y, 14, 0, Math.PI * 2); ctx.fillStyle = e.color; ctx.globalAlpha = 1 - progress; ctx.fill(); }
else if (e.type === 'revive') { ctx.beginPath(); ctx.arc(WALL + e.x, WALL + e.y, 14 + progress * 60, 0, Math.PI * 2); ctx.strokeStyle = `rgba(255, 215, 0, ${1 - progress})`; ctx.lineWidth = 4; ctx.stroke(); }
else if (e.type === 'ring') { ctx.beginPath(); ctx.arc(WALL + e.x, WALL + e.y, 10 + progress * 20, 0, Math.PI * 2); ctx.strokeStyle = e.color; ctx.globalAlpha = 1 - progress; ctx.lineWidth = 2; ctx.stroke(); }
else if (e.type === 'bubble') { ctx.beginPath(); ctx.arc(WALL + e.x, WALL + e.y, 2 + progress * 3, 0, Math.PI * 2); ctx.fillStyle = `rgba(217, 70, 239, ${1 - progress})`; ctx.fill(); }
}
ctx.globalAlpha = 1;
for (let ft of floatingTexts) {
let progress = 1 - (ft.timer / ft.maxTimer); let scale = progress < 0.2 ? (progress / 0.2) : (1 - (progress - 0.2) / 0.8 * 0.5); let fontSize = ft.isBig ? 22 : 16;
ctx.save(); ctx.translate(WALL + ft.x, WALL + ft.y); ctx.scale(scale, scale);
ctx.font = `bold ${fontSize}px sans-serif`; ctx.textAlign = 'center'; ctx.globalAlpha = Math.min(1, ft.timer * 2);
ctx.lineWidth = 3; ctx.strokeStyle = '#000'; ctx.strokeText(ft.text, 0, 0);
ctx.fillStyle = ft.color; ctx.fillText(ft.text, 0, 0); ctx.restore();
}
}
function checkGameOver() {
if (!teamA.alive || !teamB.alive) {
paused = true;
let winner = teamA.alive ? teamA.name : (teamB.alive ? teamB.name : '平局');
document.getElementById('game-hud').classList.remove('show');
document.getElementById('winner-text').innerText = `胜者:${winner}`;
document.getElementById('result-layer').classList.remove('hidden');
}
}
const cards = document.querySelectorAll('.char-card');
const btnStart = document.getElementById('btn-start');
const btnRestart = document.getElementById('btn-restart');
cards.forEach(card => {
card.addEventListener('click', () => {
const charName = card.dataset.char;
if (card.classList.contains('selected')) { card.classList.remove('selected'); selectedChars = selectedChars.filter(c => c !== charName); }
else {
if (selectedChars.length >= 2) { let first = selectedChars.shift(); document.querySelector(`.char-card[data-char="${first}"]`).classList.remove('selected'); }
card.classList.add('selected'); selectedChars.push(charName);
}
btnStart.classList.toggle('enabled', selectedChars.length === 2);
});
});
btnStart.addEventListener('click', startBattle);
btnRestart.addEventListener('click', () => {
document.getElementById('result-layer').classList.add('hidden');
document.getElementById('ui-layer').classList.remove('hidden');
selectedChars = []; cards.forEach(c => c.classList.remove('selected')); btnStart.classList.remove('enabled');
gameStarted = false; entities = []; traps = []; projectiles = []; groundEffects = []; floatingTexts = []; effects = [];
draw();
});
let last = performance.now();
function loop(now) {
let dt = Math.min((now - last) / 1000, 0.1); last = now;
update(dt); draw(); requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
})();
</script>
</body>
</html>Game Source: 族群之争 · 电子斗蛐蛐
Creator: EpicCoder88
Libraries: none
Complexity: complex (550 lines, 33.0 KB)
The full source code is displayed above on this page.
Remix Instructions
To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: game-epiccoder88-mwhx8qvp" to link back to the original. Then publish at arcadelab.ai/publish.