族群之争 · 电子斗蛐蛐
by EpicCoder88686 lines25.9 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: 528px;
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; }
/* ============ UI 遮罩层通用样式 ============ */
.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-card.disabled { opacity: 0.4; cursor: not-allowed; pointer-events: none; }
.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); }
button.enabled:active { transform: scale(0.95); }
#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 disabled">
<div class="char-icon" style="background:#555;">?</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 = 15, ROWS = 15, 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 cats = [], traps = [], projectiles = [], groundEffects = [], floatingTexts = [], effects = [];
let paused = true;
let gameStarted = false;
let selectedChars = [];
class Cat {
constructor(name, x, y, color, hp, isRanged) {
this.name = name; this.x = x; this.y = y; this.color = color;
this.maxHp = hp; this.hp = hp; this.r = 14;
this.isRanged = isRanged; this.alive = true;
this.baseSpeed = isRanged ? 65 : (name === '火星' ? 85 : 75);
this.speed = this.baseSpeed;
this.atkCd = 0; this.skillCd = 0;
this.atkRange = isRanged ? 200 : 32;
this.atkDamage = isRanged ? 38 : (name === '火星' ? 28 : 30);
this.scoutRange = isRanged ? 220 : (name === '火星' ? 180 : 140);
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;
if (this.name === '火星') {
this.hasRevived = false;
this.invincibleTimer = 0;
this.dashTimer = 0;
this.damageDealt = 0;
this.leaderBuff = 0;
}
}
// ★ 增加 attacker 参数以便计算后撤方向
takeDamage(amount, isPoison = false, attacker = null) {
if (!this.alive || this.invincibleTimer > 0) return;
this.hp -= amount;
if (!isPoison) {
this.hitFlashTimer = 0.15;
// ★ 远程受击后撤判定
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' });
effects.push({ type: 'trail', x: this.x, y: this.y, timer: 0.4, maxTimer: 0.4, color: 'rgba(232, 211, 154, 0.6)' });
} 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' : '#ff4d4d'
});
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();
}
}
}
update(dt, enemy) {
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.poisonTimer > 0) {
this.poisonTimer -= dt;
this.poisonTickTimer -= dt;
if (this.poisonTickTimer <= 0) { this.poisonTickTimer = 1.0; this.takeDamage(1, true); }
}
for (let i = this.trapDebuffs.length - 1; i >= 0; i--) {
let debuff = this.trapDebuffs[i];
debuff.timeLeft -= dt;
if (debuff.timeLeft > 0) {
debuff.tickTimer -= dt;
if (debuff.tickTimer <= 0) {
debuff.tickTimer = 1.0;
debuff.tickCount++;
this.takeDamage(Math.pow(2, debuff.tickCount), true);
}
} else { this.trapDebuffs.splice(i, 1); }
}
// 击退位移结算
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 dx = enemy.x - this.x, dy = enemy.y - this.y;
let dist = Math.hypot(dx, dy);
let inSight = dist < this.scoutRange && enemy.alive;
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.isRanged && inSight) {
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 = 20 + Math.random() * 60;
}
let step = currentSpeed * 0.6 * dt;
if (step > this.wanderDistRemaining) step = this.wanderDistRemaining;
let nextX = this.x + Math.cos(this.wanderAngle) * step;
let 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 (dist <= this.atkRange && this.atkCd <= 0) {
let slashAngle = Math.atan2(dy, dx);
let slashColor = this.name === '火星' ? '#f97316' : '#e08b3a';
if (this.name === '火星') {
this.atkCd = 1.0;
let baseDmg = 28;
let extraDmg = baseDmg * 0.05 * this.leaderBuff;
let finalDmg = Math.round(baseDmg + extraDmg);
enemy.takeDamage(finalDmg, false, this); // ★ 传入攻击者 this
effects.push({ type: 'slash', x: enemy.x, y: enemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: 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;
enemy.takeDamage(this.atkDamage, false, this); // ★ 传入攻击者 this
effects.push({ type: 'slash', x: enemy.x, y: enemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: slashColor });
}
else if (this.isRanged) {
this.atkCd = 1.5;
projectiles.push({ x: this.x, y: this.y, target: enemy, speed: 250, damage: this.atkDamage, type: 'berry', owner: this });
}
}
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;
}
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);
ctx.beginPath(); ctx.arc(sx, sy, this.r, 0, Math.PI * 2);
ctx.fillStyle = this.hitFlashTimer > 0 ? '#ffffff' : this.color;
ctx.fill();
ctx.strokeStyle = '#000'; ctx.lineWidth = 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.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();
}
if (this.trapDebuffs.length >= 2) {
ctx.strokeStyle = `rgba(239, 68, 68, ${0.5 + 0.5 * Math.sin(Date.now() / 50)})`;
ctx.lineWidth = 1.5;
for (let debuff of this.trapDebuffs) {
ctx.beginPath(); ctx.moveTo(sx, sy); ctx.lineTo(WALL + debuff.center.x, WALL + debuff.center.y); 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');
cats = [];
let c1 = selectedChars[0], c2 = selectedChars[1];
function createChar(name, x, y) {
if (name === '鹰霜') return new Cat('鹰霜', x, y, '#e08b3a', 1030, false);
if (name === '黄牙') return new Cat('黄牙', x, y, '#e8d39a', 1000, true);
if (name === '火星') return new Cat('火星', x, y, '#f97316', 1050, false);
}
cats.push(createChar(c1, 80, 80));
cats.push(createChar(c2, 400, 400));
traps = []; projectiles = []; groundEffects = []; floatingTexts = []; effects = [];
paused = false;
document.getElementById('game-hud').innerText = '战斗中...';
}
function update(dt) {
if (paused || !gameStarted) return;
for (let cat of cats) {
let enemy = cats.find(c => c !== cat);
if (cat.alive && enemy.alive) cat.update(dt, enemy);
}
for (let i = traps.length - 1; i >= 0; i--) {
let t = traps[i];
if (!t.armed) continue;
for (let cat of cats) {
if (!cat.alive || cat === t.owner || cat.invincibleTimer > 0) continue;
if (Math.hypot(cat.x - t.x, cat.y - t.y) < cat.r + t.r) {
t.armed = false; traps.splice(i, 1);
cat.trapDebuffs.push({ center: { x: t.x, y: t.y }, timeLeft: 5.0, tickTimer: 1.0, tickCount: 0 });
cat.slowPercent = 0.3; cat.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];
if (!p.target.alive) { projectiles.splice(i, 1); continue; }
let dx = p.target.x - p.x, dy = p.target.y - p.y;
let dist = Math.hypot(dx, dy);
if (dist < p.speed * dt) {
// ★ 投射物命中,传入 p.owner 作为攻击者
p.target.takeDamage(p.damage, false, p.owner);
p.target.poisonTimer = 5.0;
p.target.poisonTickTimer = 1.0;
groundEffects.push({ x: p.target.x, y: p.target.y, r: 30, timer: 20, owner: p.owner, seed: Math.random() });
effects.push({ type: 'ring', x: p.target.x, y: p.target.y, timer: 0.3, maxTimer: 0.3, color: '#d946ef' });
projectiles.splice(i, 1);
} else {
p.x += (dx / dist) * p.speed * dt;
p.y += (dy / dist) * p.speed * dt;
}
}
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 cat of cats) {
if (!cat.alive || cat === g.owner || cat.invincibleTimer > 0) continue;
if (Math.hypot(cat.x - g.x, cat.y - g.y) < cat.r + g.r) {
if (cat.slowPercent < 0.2) { cat.slowPercent = 0.2; cat.slowTimer = 0.5; }
if (!cat.juiceTick) cat.juiceTick = 0;
cat.juiceTick -= dt;
if (cat.juiceTick <= 0) { cat.juiceTick = 1.0; cat.takeDamage(1, true); }
}
}
}
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;
let 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;
let 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();
ctx.beginPath(); ctx.arc(WALL + p.x - 5, WALL + p.y, 3, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(217, 70, 239, 0.4)'; ctx.fill();
}
if (gameStarted) {
for (let cat of cats) cat.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 (!cats[0].alive || !cats[1].alive) {
paused = true;
let winner = cats[0].alive ? cats[0].name : (cats[1].alive ? cats[1].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:not(.disabled)');
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', () => {
// 恢复 UI 状态
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;
cats = []; 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 (686 lines, 25.9 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-mwhierdz" to link back to the original. Then publish at arcadelab.ai/publish.