🎮ArcadeLab

族群之争 · 电子斗蛐蛐

by EpicCoder88
797 lines30.7 KB
▶ Play
<!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; /* 匹配 10x10 的尺寸 (320 + 边框48) */
    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-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" data-char="长鞭">
        <div class="char-icon" style="background:#1a1a1a; color:#ff0000;">🩸</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>
(() => {
  // ★ 修改为 10×10 地图
  const CELL = 32, COLS = 10, ROWS = 10, WALL = 24;
  const ARENA_W = CELL * COLS, ARENA_H = CELL * ROWS; // 320 x 320
  const CANVAS_W = ARENA_W + WALL * 2, CANVAS_H = ARENA_H + WALL * 2; // 368 x 368

  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; 
  let gameStarted = false;
  let 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 = isRanged ? 65 : (name === '火星' ? 85 : (name === '长鞭' ? 90 : 75)); 
      this.speed = this.baseSpeed;
      this.atkCd = 0; this.skillCd = 0;
      
      this.atkRange = isRanged ? 200 : (name === '长鞭' ? 16 : 32);
      this.atkDamage = isRanged ? 38 : (name === '长鞭' ? 150 : (name === '火星' ? 28 : (isMinion ? 45 : 30)));
      this.scoutRange = isRanged ? 220 : (name === '火星' ? 180 : (name === '长鞭' ? 150 : (isMinion ? 120 : 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; 

      this.lockOnTimer = 0;
      this.lockOnTarget = null;

      if (this.name === '火星') {
        this.hasRevived = false;
        this.invincibleTimer = 0;
        this.dashTimer = 0;      
        this.damageDealt = 0;    
        this.leaderBuff = 0;     
      }

      if (this.name === '长鞭') {
        this.invincibleTimer = 0;
        this.damageTakenTracker = 0; 
        this.baseCritRate = 0.05;
      }
    }

    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' });
          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' : (amount > 200 ? '#ff0000' : '#ff4d4d'),
        isBig: amount > 200
      });

      if (this.name === '长鞭') {
        this.damageTakenTracker += amount;
        while (this.damageTakenTracker >= 100) {
          this.damageTakenTracker -= 100;
          this.summonMinions();
        }
      }

      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;
        let 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));
        
        let minion = new Cat('血族', this.team, mx, my, '#1a1a1a', 100, false, true);
        entities.push(minion);
        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.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 (this.lockOnTimer > 0) {
        this.lockOnTimer -= dt;
        if (this.lockOnTimer <= 0 || !this.lockOnTarget || !this.lockOnTarget.alive) {
          this.lockOnTimer = 0;
          this.lockOnTarget = null;
        }
      }

      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;
      let 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;

      let moveTarget = null;
      if (this.lockOnTimer > 0 && this.lockOnTarget) {
        moveTarget = this.lockOnTarget; 
      } else if (!this.isRanged && inSight && nearestEnemy) {
        moveTarget = nearestEnemy; 
      }

      if (moveTarget) {
        let dx = moveTarget.x - this.x, dy = moveTarget.y - this.y;
        let 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 {
        // 10x10 小地图,缩短游走距离
        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;
        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 (nearestEnemy && minDist <= this.atkRange && this.atkCd <= 0) {
        let slashAngle = Math.atan2(nearestEnemy.y - this.y, nearestEnemy.x - this.x);
        let slashColor = this.name === '火星' ? '#f97316' : (this.name === '长鞭' ? '#ff0000' : (this.isMinion ? '#333' : '#e08b3a'));

        if (this.name === '长鞭') {
          this.atkCd = 1.2;
          let hpRatio = this.hp / this.maxHp;
          let currentCrit = this.baseCritRate + (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: 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 baseDmg = 28;
          let extraDmg = baseDmg * 0.05 * this.leaderBuff;
          let finalDmg = Math.round(baseDmg + extraDmg);
          
          nearestEnemy.takeDamage(finalDmg, false, this); 
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.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;
          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; 
          projectiles.push({ x: this.x, y: this.y, target: nearestEnemy, speed: 250, damage: this.atkDamage, type: 'berry', owner: this });
        }
        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.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');
    
    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', 950, false);
    }

    // 10x10 地图的出生点
    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 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];
      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.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 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;
        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 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: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', () => {
    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 (797 lines, 30.7 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-mwhlh5xu" to link back to the original. Then publish at arcadelab.ai/publish.