AI小球对战游戏(六角色最新版)
2026-11-07 19:01:02
发布于:浙江
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>元素球球大乱斗 - 手动选角对决</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
body {
background: radial-gradient(circle at center, #1a202c 0%, #000000 100%);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
overflow: hidden;
color: white;
}
.game-canvas {
box-shadow: 0 0 50px rgba(0, 0, 0, 0.8);
border: 4px solid #2d3748;
border-radius: 16px;
background-color: #111827;
display: block;
}
.status-card {
backdrop-filter: blur(12px);
background: rgba(255, 255, 255, 0.03);
border: 1px solid rgba(255, 255, 255, 0.08);
transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}
.hp-bar-fill {
transition: width 0.2s linear;
}
.damage-text {
position: absolute;
font-weight: 900;
pointer-events: none;
animation: floatUpDamage 0.8s forwards;
text-shadow: 0 2px 4px rgba(0,0,0,0.8);
z-index: 50;
}
@keyframes floatUpDamage {
0% { transform: translateY(0) scale(0.8); opacity: 0; }
20% { transform: translateY(-10px) scale(1.2); opacity: 1; }
100% { transform: translateY(-50px) scale(1); opacity: 0; }
}
.card-selected {
border-color: #fbbf24;
box-shadow: 0 0 20px rgba(251, 191, 36, 0.3);
transform: translateY(-5px);
}
.card-dimmed {
opacity: 0.4;
filter: grayscale(0.8);
cursor: not-allowed;
}
.role-card {
cursor: pointer;
transition: all 0.2s ease;
}
.role-card:hover:not(.card-dimmed) {
transform: translateY(-3px);
border-color: rgba(251, 191, 36, 0.6);
}
</style>
</head>
<body class="h-screen w-screen flex flex-col items-center justify-center relative selection:bg-indigo-500 selection:text-white">
<!-- 顶部标题 -->
<header class="absolute top-6 w-full max-w-6xl flex justify-between items-center px-8 z-20">
<div class="flex items-center gap-4">
<div class="w-10 h-10 bg-gradient-to-tr from-indigo-500 to-purple-600 rounded-lg flex items-center justify-center shadow-lg">
<i class="fa-solid fa-gamepad text-white"></i>
</div>
<h1 class="text-3xl font-black tracking-tight bg-clip-text text-transparent bg-gradient-to-r from-white to-gray-400">ELEMENTAL BATTLE</h1>
</div>
<div class="flex gap-4">
<button onclick="restartGame()" class="px-6 py-2 bg-white/10 hover:bg-white/20 border border-white/10 rounded-full text-sm font-bold transition-all flex items-center gap-2">
<i class="fa-solid fa-rotate-right"></i> 重新选角
</button>
</div>
</header>
<!-- 游戏主区域 -->
<main class="relative flex items-center gap-12 z-10">
<!-- 左侧:玩家1状态 -->
<div id="p1-card" class="status-card w-72 p-6 rounded-2xl flex flex-col gap-5 relative overflow-hidden">
<div class="absolute top-0 left-0 w-full h-1 bg-gradient-to-r from-transparent via-white/20 to-transparent"></div>
<div class="flex items-center justify-between">
<div class="flex items-center gap-4">
<div id="p1-icon-bg" class="w-14 h-14 rounded-xl bg-gray-700 flex items-center justify-center shadow-inner transition-colors duration-500">
<i id="p1-icon" class="fa-solid text-2xl text-white"></i>
</div>
<div>
<h2 id="p1-name" class="font-bold text-xl text-white">等待选角...</h2>
<p id="p1-skill-desc" class="text-xs text-gray-400 mt-1">--</p>
</div>
</div>
<div id="p1-debuffs" class="flex gap-2"></div>
</div>
<div class="space-y-3">
<div class="flex justify-between text-xs font-bold uppercase tracking-wider text-gray-400">
<span>HP Integrity</span>
<span id="p1-hp-val">100%</span>
</div>
<div class="h-4 bg-gray-800/50 rounded-full overflow-hidden border border-gray-700/50 relative">
<div id="p1-hp-bar" class="h-full bg-gray-600 hp-bar-fill relative" style="width: 100%">
<div class="absolute inset-0 bg-white/20 animate-[shimmer_2s_infinite]"></div>
</div>
</div>
</div>
<div class="space-y-2">
<div class="flex justify-between text-xs font-bold uppercase tracking-wider text-gray-400">
<span>Skill Charge</span>
<span id="p1-cd-val">Ready</span>
</div>
<div class="h-1.5 bg-gray-800 rounded-full overflow-hidden">
<div id="p1-cd-bar" class="h-full bg-indigo-500 hp-bar-fill" style="width: 100%"></div>
</div>
</div>
</div>
<!-- 中间:Canvas画布 -->
<div class="relative group">
<canvas id="gameCanvas" width="800" height="600" class="game-canvas"></canvas>
<!-- 右下角计时器+技能统计 -->
<div class="absolute bottom-4 right-4 bg-black/50 backdrop-blur-md border border-white/10 rounded-xl px-4 py-3 z-30 flex flex-col gap-2">
<div class="text-right">
<div class="text-xs text-gray-400 uppercase font-bold mb-0.5">Match Time</div>
<div id="timer" class="text-xl font-mono text-white font-bold">00:00</div>
</div>
<div class="text-right border-t border-white/10 pt-2">
<div class="text-xs text-gray-400 uppercase font-bold mb-0.5">Total Skills</div>
<div id="skillCount" class="text-lg font-mono text-indigo-400 font-bold">0</div>
</div>
</div>
<!-- 开始/选角/结束遮罩 -->
<div id="overlayModal" class="absolute inset-0 bg-black/70 backdrop-blur-sm flex flex-col items-center justify-center z-50 rounded-2xl transition-opacity duration-300">
<div id="modalContent" class="text-center w-full max-w-3xl px-6">
<div class="mb-6 relative">
<div class="absolute inset-0 bg-indigo-500 blur-3xl opacity-20 rounded-full"></div>
<i class="fa-solid fa-hand-pointer text-6xl text-white relative z-10 animate-bounce"></i>
</div>
<h2 id="selectTitle" class="text-3xl font-bold text-white mb-2">选择玩家1的角色</h2>
<p id="selectDesc" class="text-gray-400 mb-6 max-w-md mx-auto">点击下方卡片选择你想要使用的元素球</p>
<!-- 选角卡片容器 -->
<div id="roleSelectGrid" class="grid grid-cols-3 gap-4 mb-6">
<!-- 角色卡片通过JS动态生成 -->
</div>
<button id="confirmSelectBtn" onclick="confirmSelection()" class="hidden px-8 py-3 bg-indigo-600 hover:bg-indigo-500 text-white rounded-xl font-bold shadow-lg shadow-indigo-600/30 transition-all transform hover:scale-105 active:scale-95">
开始战斗
</button>
</div>
</div>
</div>
<!-- 右侧:玩家2状态 -->
<div id="p2-card" class="status-card w-72 p-6 rounded-2xl flex flex-col gap-5 relative overflow-hidden">
<div class="absolute top-0 left-0 w-full h-1 bg-gradient-to-r from-transparent via-white/20 to-transparent"></div>
<div class="flex items-center justify-between flex-row-reverse">
<div class="flex items-center gap-4">
<div id="p2-icon-bg" class="w-14 h-14 rounded-xl bg-gray-700 flex items-center justify-center shadow-inner transition-colors duration-500">
<i id="p2-icon" class="fa-solid text-2xl text-white"></i>
</div>
<div class="text-right">
<h2 id="p2-name" class="font-bold text-xl text-white">等待选角...</h2>
<p id="p2-skill-desc" class="text-xs text-gray-400 mt-1">--</p>
</div>
</div>
<div id="p2-debuffs" class="flex gap-2"></div>
</div>
<div class="space-y-3">
<div class="flex justify-between text-xs font-bold uppercase tracking-wider text-gray-400 flex-row-reverse">
<span>HP Integrity</span>
<span id="p2-hp-val">100%</span>
</div>
<div class="h-4 bg-gray-800/50 rounded-full overflow-hidden border border-gray-700/50 relative">
<div id="p2-hp-bar" class="h-full bg-gray-600 hp-bar-fill relative" style="width: 100%">
<div class="absolute inset-0 bg-white/20 animate-[shimmer_2s_infinite]"></div>
</div>
</div>
</div>
<div class="space-y-2">
<div class="flex justify-between text-xs font-bold uppercase tracking-wider text-gray-400 flex-row-reverse">
<span>Skill Charge</span>
<span id="p2-cd-val">Ready</span>
</div>
<div class="h-1.5 bg-gray-800 rounded-full overflow-hidden">
<div id="p2-cd-bar" class="h-full bg-indigo-500 hp-bar-fill" style="width: 100%"></div>
</div>
</div>
</div>
</main>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
// 游戏配置与角色定义
const ROLES = {
FIRE: {
id: 'fire',
name: '烈焰球',
color: '#ef4444',
icon: 'fa-fire',
desc: '被动: 击中附加灼烧(5次快速伤害,每次2点)',
skillType: 'burn',
directDmg: 2,
cooldown: 100
},
ICE: {
id: 'ice',
name: '寒冰球',
color: '#3b82f6',
icon: 'fa-snowflake',
desc: '被动: 击中附加冻结(完全静止1.5秒)',
skillType: 'freeze',
directDmg: 8,
cooldown: 100
},
GREEN: {
id: 'green',
name: '翠绿树',
color: '#22c55e',
icon: 'fa-tree',
desc: '技能: 连续发射3发子弹(每发4点伤害)',
skillType: 'triple',
directDmg: 4,
cooldown: 120
},
BOUNCE: {
id: 'bounce',
name: '反弹球',
color: '#a855f7',
icon: 'fa-arrows-rotate',
desc: '技能: 子弹撞墙可反弹三次(拐弯追击)',
skillType: 'bounce',
directDmg: 6,
cooldown: 110,
maxBounces: 3
},
NINJA: {
id: 'ninja',
name: '忍者球',
color: '#1e293b',
icon: 'fa-user-ninja',
desc: '被动: 命中敌人召唤分身,分身发射6发子弹后消失,受击也会消失',
skillType: 'clone',
directDmg: 4,
cooldown: 100
},
BOMB: {
id: 'bomb',
name: '爆破球',
color: '#f97316',
icon: 'fa-bomb',
desc: '被动: 子弹碰墙或碰到角色都会触发范围爆炸,半径为5个球范围,造成7点范围伤害,直接命中伤害为1',
skillType: 'explode',
directDmg: 1,
cooldown: 120,
explosionRadius: 140, // 28*5=140,5个主球大小范围
explosionDmg: 7
}
};
const CONFIG = {
baseSpeed: 3.5,
projectileSpeed: 8,
burnDamage: 2,
burnTickCount: 5,
burnInterval: 15,
freezeDuration: 90,
tripleSpread: 0.3
};
let gameLoopId;
let startTime;
let isGameOver = false;
let isSelecting = true;
let selectStep = 1; // 1=选p1, 2=选p2
let selectedP1 = null;
let selectedP2 = null;
let totalSkillsUsed = 0;
let ball1, ball2;
let projectiles = [];
let particles = [];
let clones = [];
let explosions = []; // 爆炸效果列表
// 选角逻辑初始化
function initRoleSelect() {
renderRoleCards();
document.getElementById('selectTitle').innerText = '选择玩家1的角色';
document.getElementById('selectDesc').innerText = '点击下方卡片为玩家1选择出战角色';
document.getElementById('confirmSelectBtn').classList.add('hidden');
}
function renderRoleCards(disabledId = null) {
const grid = document.getElementById('roleSelectGrid');
grid.innerHTML = '';
Object.values(ROLES).forEach(role => {
const isDisabled = role.id === disabledId;
const card = document.createElement('div');
card.className = `role-card bg-white/5 border border-white/10 rounded-xl p-4 flex flex-col items-center gap-2 ${isDisabled ? 'card-dimmed' : ''}`;
card.innerHTML = `
<div class="w-16 h-16 rounded-xl flex items-center justify-center shadow-lg" style="background-color: ${role.color}">
<i class="fa-solid ${role.icon} text-3xl text-white"></i>
</div>
<h3 class="font-bold text-lg text-white">${role.name}</h3>
<p class="text-xs text-gray-400 text-center leading-relaxed">${role.desc}</p>
<div class="text-xs font-bold mt-1" style="color: ${role.color}">
${role.skillType === 'explode' ? `爆炸伤害: ${role.explosionDmg}` : `普攻伤害: ${role.directDmg}`}
</div>
`;
if (!isDisabled) {
card.onclick = () => selectRole(role);
}
grid.appendChild(card);
});
}
function selectRole(role) {
// 清除所有选中状态
document.querySelectorAll('.role-card').forEach(c => c.classList.remove('card-selected'));
// 标记当前选中
event.currentTarget.classList.add('card-selected');
if (selectStep === 1) {
selectedP1 = role;
} else {
selectedP2 = role;
}
document.getElementById('confirmSelectBtn').classList.remove('hidden');
}
function confirmSelection() {
if (selectStep === 1 && selectedP1) {
// 进入选p2阶段
selectStep = 2;
document.getElementById('selectTitle').innerText = '选择玩家2的角色';
document.getElementById('selectDesc').innerText = '点击下方卡片为玩家2选择出战角色(不可与玩家1重复)';
renderRoleCards(selectedP1.id);
document.getElementById('confirmSelectBtn').classList.add('hidden');
// 预览p1卡片
updateCardPreview(1, selectedP1);
} else if (selectStep === 2 && selectedP2) {
// 两个都选完了,开始游戏
document.getElementById('overlayModal').classList.add('opacity-0', 'pointer-events-none');
isSelecting = false;
updateCardPreview(2, selectedP2);
initGame(selectedP1, selectedP2);
}
}
// 实体基类
class Entity {
constructor(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.markedForDeletion = false;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
}
// 爆炸效果类
class Explosion {
constructor(x, y, radius, color) {
this.x = x;
this.y = y;
this.maxRadius = radius;
this.currentRadius = 0;
this.color = color;
this.life = 1.0;
}
update() {
this.currentRadius += 8;
this.life -= 0.07;
if (this.life <= 0) this.markedForDeletion = true;
}
draw() {
ctx.globalAlpha = this.life * 0.6;
ctx.beginPath();
ctx.arc(this.x, this.y, this.currentRadius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.strokeStyle = '#fff';
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
ctx.globalAlpha = 1;
}
}
// 球球类
class Ball extends Entity {
constructor(x, y, roleConfig, playerNum) {
super(x, y, 28, roleConfig.color);
this.role = roleConfig;
this.playerNum = playerNum;
this.vx = (playerNum === 1 ? 1 : -1) * CONFIG.baseSpeed;
this.vy = (Math.random() > 0.5 ? 1 : -1) * CONFIG.baseSpeed;
this.hp = 100;
this.maxHp = 100;
this.cooldown = 0;
this.burnStacks = 0;
this.burnTimer = 0;
this.freezeTimer = 0;
}
update() {
if (isGameOver) return;
if (this.burnStacks > 0) {
this.burnTimer++;
if (this.burnTimer >= CONFIG.burnInterval) {
this.takeDamage(CONFIG.burnDamage, '#ff4500');
this.burnStacks--;
this.burnTimer = 0;
createParticles(this.x, this.y, '#ff4500', 3);
}
}
let canMove = true;
if (this.freezeTimer > 0) {
canMove = false;
this.freezeTimer--;
if (this.freezeTimer % 10 === 0) createParticles(this.x, this.y, '#a5f3fc', 2);
}
if (canMove) {
this.x += this.vx;
this.y += this.vy;
if (this.x - this.radius < 0 || this.x + this.radius > WIDTH) {
this.vx = -this.vx;
this.x = Math.max(this.radius, Math.min(WIDTH - this.radius, this.x));
}
if (this.y - this.radius < 0 || this.y + this.radius > HEIGHT) {
this.vy = -this.vy;
this.y = Math.max(this.radius, Math.min(HEIGHT - this.radius, this.y));
}
if (Math.random() < 0.02) {
const angle = Math.atan2(this.vy, this.vx) + (Math.random() - 0.5) * 1.5;
this.vx = Math.cos(angle) * CONFIG.baseSpeed;
this.vy = Math.sin(angle) * CONFIG.baseSpeed;
}
}
if (this.cooldown > 0) this.cooldown--;
else this.useSkill();
if (this.hp <= 0) {
this.hp = 0;
endGame(this.playerNum === 1 ? ball2.role.name : ball1.role.name);
}
}
useSkill() {
this.cooldown = this.role.cooldown;
totalSkillsUsed++;
const target = this.playerNum === 1 ? ball2 : ball1;
const baseAngle = Math.atan2(target.y - this.y, target.x - this.x);
if (this.role.id === 'green') {
for (let i = -1; i <= 1; i++) {
const angle = baseAngle + i * CONFIG.tripleSpread;
projectiles.push(new Projectile(this.x, this.y, angle, this.role, this.color, this));
}
} else {
projectiles.push(new Projectile(this.x, this.y, baseAngle, this.role, this.color, this));
}
}
takeDamage(amount, color) {
this.hp -= amount;
showFloatingText(this.x, this.y - 30, `-${Math.ceil(amount)}`, color || '#fff');
}
applyDebuff(type) {
if (type === 'burn') {
this.burnStacks = CONFIG.burnTickCount;
this.burnTimer = 0;
} else if (type === 'freeze') {
this.freezeTimer = CONFIG.freezeDuration;
}
}
draw() {
super.draw();
ctx.beginPath();
ctx.arc(this.x - 8, this.y - 8, this.radius * 0.3, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(255,255,255,0.4)';
ctx.fill();
ctx.closePath();
if (this.burnStacks > 0) {
ctx.strokeStyle = '#ff4500';
ctx.lineWidth = 3;
ctx.setLineDash([4, 4]);
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius + 6, 0, Math.PI*2);
ctx.stroke();
ctx.setLineDash([]);
ctx.closePath();
}
if (this.freezeTimer > 0) {
ctx.fillStyle = 'rgba(165, 243, 252, 0.3)';
ctx.strokeStyle = '#fff';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius + 5, 0, Math.PI*2);
ctx.fill();
ctx.stroke();
ctx.closePath();
}
}
}
// 分身实体类
class Clone extends Entity {
constructor(x, y, ownerPlayerNum) {
super(x, y, 20, '#64748b');
this.ownerPlayerNum = ownerPlayerNum;
this.hp = 1;
this.maxHp = 1;
this.shotsFired = 0;
this.vx = (Math.random() - 0.5) * CONFIG.baseSpeed * 0.8;
this.vy = (Math.random() - 0.5) * CONFIG.baseSpeed * 0.8;
this.cooldown = 60;
}
update() {
if (isGameOver) {
this.markedForDeletion = true;
return;
}
this.x += this.vx;
this.y += this.vy;
if (this.x - this.radius < 0 || this.x + this.radius > WIDTH) {
this.vx = -this.vx;
this.x = Math.max(this.radius, Math.min(WIDTH - this.radius, this.x));
}
if (this.y - this.radius < 0 || this.y + this.radius > HEIGHT) {
this.vy = -this.vy;
this.y = Math.max(this.radius, Math.min(HEIGHT - this.radius, this.y));
}
if (Math.random() < 0.03) {
const target = this.ownerPlayerNum === 1 ? ball2 : ball1;
const angle = Math.atan2(target.y - this.y, target.x - this.x) + (Math.random() - 0.5) * 2;
this.vx = Math.cos(angle) * CONFIG.baseSpeed * 0.7;
this.vy = Math.sin(angle) * CONFIG.baseSpeed * 0.7;
}
if (this.cooldown > 0) {
this.cooldown--;
} else {
this.cooldown = 80;
const target = this.ownerPlayerNum === 1 ? ball2 : ball1;
const angle = Math.atan2(target.y - this.y, target.x - this.x);
const cloneBulletRole = {
directDmg: 3,
skillType: 'normal',
maxBounces: 0
};
projectiles.push(new Projectile(this.x, this.y, angle, cloneBulletRole, '#94a3b8', this));
this.shotsFired++;
if (this.shotsFired >= 6) {
this.markedForDeletion = true;
createParticles(this.x, this.y, '#94a3b8', 10);
}
}
}
takeDamage(amount, color) {
this.hp -= amount;
showFloatingText(this.x, this.y - this.radius - 15, `-${Math.ceil(amount)}`, color || '#94a3b8');
if (this.hp <= 0) {
this.markedForDeletion = true;
createParticles(this.x, this.y, '#94a3b8', 10);
}
}
draw() {
super.draw();
ctx.beginPath();
ctx.arc(this.x - 6, this.y - 6, this.radius * 0.3, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(255,255,255,0.3)';
ctx.fill();
ctx.closePath();
const barWidth = 30;
const barHeight = 4;
ctx.fillStyle = 'rgba(0,0,0,0.6)';
ctx.fillRect(this.x - barWidth/2, this.y - this.radius - 10, barWidth, barHeight);
ctx.fillStyle = '#22c55e';
ctx.fillRect(this.x - barWidth/2, this.y - this.radius - 10, barWidth * Math.max(0, this.hp/this.maxHp), barHeight);
}
}
// 投射物类
class Projectile extends Entity {
constructor(x, y, angle, role, color, owner) {
super(x, y, 5, color);
this.role = role;
this.owner = owner;
this.vx = Math.cos(angle) * CONFIG.projectileSpeed;
this.vy = Math.sin(angle) * CONFIG.projectileSpeed;
this.trail = [];
this.maxBounces = role.maxBounces || 0;
this.bouncesLeft = this.maxBounces;
this.age = 0;
}
explode() {
const explosionRadius = this.role.explosionRadius || 28;
const explosionDmg = this.role.explosionDmg || 5;
// 添加爆炸特效
explosions.push(new Explosion(this.x, this.y, explosionRadius, this.color));
createParticles(this.x, this.y, this.color, 20);
// 判定阵营
let ownerFaction;
if (this.owner instanceof Ball) {
ownerFaction = this.owner.playerNum;
} else if (this.owner instanceof Clone) {
ownerFaction = this.owner.ownerPlayerNum;
}
const enemyFaction = 3 - ownerFaction;
const enemyMain = enemyFaction === 1 ? ball1 : ball2;
// 对敌方主球造成范围伤害
const distMain = Math.hypot(this.x - enemyMain.x, this.y - enemyMain.y);
if (distMain < explosionRadius + enemyMain.radius) {
enemyMain.takeDamage(explosionDmg, this.color);
createParticles(enemyMain.x, enemyMain.y, this.color, 8);
}
// 对范围内敌方分身造成伤害
for (const clone of clones) {
if (clone.ownerPlayerNum === enemyFaction && !clone.markedForDeletion) {
const distClone = Math.hypot(this.x - clone.x, this.y - clone.y);
if (distClone < explosionRadius + clone.radius) {
clone.takeDamage(explosionDmg, this.color);
createParticles(clone.x, clone.y, this.color, 5);
}
}
}
}
update() {
this.age++;
this.x += this.vx;
this.y += this.vy;
this.trail.push({x: this.x, y: this.y});
if (this.trail.length > 6) this.trail.shift();
if (this.age > 1) {
const hitX = this.x - this.radius < 0 || this.x + this.radius > WIDTH;
const hitY = this.y - this.radius < 0 || this.y + this.radius > HEIGHT;
if (hitX || hitY) {
if (this.bouncesLeft > 0) {
this.bouncesLeft--;
if (hitX) {
this.vx = -this.vx;
this.x = (this.x - this.radius < 0) ? this.radius : WIDTH - this.radius;
}
if (hitY) {
this.vy = -this.vy;
this.y = (this.y - this.radius < 0) ? this.radius : HEIGHT - this.radius;
}
createParticles(this.x, this.y, this.color, 6);
} else {
// 爆破球撞墙触发爆炸
if (this.role.skillType === 'explode') {
this.explode();
}
this.markedForDeletion = true;
return;
}
}
}
let ownerFaction;
if (this.owner instanceof Ball) {
ownerFaction = this.owner.playerNum;
} else if (this.owner instanceof Clone) {
ownerFaction = this.owner.ownerPlayerNum;
}
const enemyFaction = 3 - ownerFaction;
const enemyMain = enemyFaction === 1 ? ball1 : ball2;
let hitTarget = false;
const distMain = Math.hypot(this.x - enemyMain.x, this.y - enemyMain.y);
if (distMain < this.radius + enemyMain.radius) {
if (this.role.skillType === 'explode') {
// 爆破球碰到角色直接触发爆炸,没有直接伤害
this.explode();
} else {
enemyMain.takeDamage(this.role.directDmg, this.color);
if (typeof enemyMain.applyDebuff === 'function' && this.role.skillType !== 'normal') {
enemyMain.applyDebuff(this.role.skillType);
}
}
createParticles(this.x, this.y, this.color, 8);
this.markedForDeletion = true;
hitTarget = true;
if (this.role.id === 'ninja') {
const offsetX = (Math.random() - 0.5) * 50;
const offsetY = (Math.random() - 0.5) * 50;
const cloneX = Math.max(30, Math.min(WIDTH - 30, this.x + offsetX));
const cloneY = Math.max(30, Math.min(HEIGHT - 30, this.y + offsetY));
clones.push(new Clone(cloneX, cloneY, ownerFaction));
createParticles(cloneX, cloneY, '#94a3b8', 15);
}
}
if (!hitTarget) {
for (const clone of clones) {
if (clone.ownerPlayerNum === enemyFaction && !clone.markedForDeletion) {
const distClone = Math.hypot(this.x - clone.x, this.y - clone.y);
if (distClone < this.radius + clone.radius) {
if (this.role.skillType === 'explode') {
// 爆破球碰到分身也触发爆炸
this.explode();
} else {
clone.takeDamage(this.role.directDmg, this.color);
}
createParticles(this.x, this.y, this.color, 5);
this.markedForDeletion = true;
hitTarget = true;
break;
}
}
}
}
}
draw() {
this.trail.forEach((pos, i) => {
ctx.globalAlpha = i / this.trail.length;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(pos.x, pos.y, this.radius * (i/this.trail.length), 0, Math.PI*2);
ctx.fill();
ctx.closePath();
});
ctx.globalAlpha = 1;
ctx.shadowBlur = 8;
ctx.shadowColor = this.color;
super.draw();
ctx.shadowBlur = 0;
if (this.bouncesLeft > 0) {
ctx.globalAlpha = 0.55;
ctx.strokeStyle = this.color;
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius + 4, 0, Math.PI * 2);
ctx.stroke();
ctx.closePath();
ctx.globalAlpha = 1;
}
}
}
// 粒子系统
class Particle {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.color = color;
this.size = Math.random() * 3 + 1;
this.vx = (Math.random() - 0.5) * 5;
this.vy = (Math.random() - 0.5) * 5;
this.life = 1.0;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.life -= 0.04;
}
draw() {
ctx.globalAlpha = this.life;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
ctx.globalAlpha = 1;
}
}
function createParticles(x, y, color, count) {
for(let i=0; i<count; i++) particles.push(new Particle(x, y, color));
}
function showFloatingText(x, y, text, color) {
const el = document.createElement('div');
el.className = 'damage-text';
el.innerText = text;
el.style.left = (canvas.offsetLeft + x) + 'px';
el.style.top = (canvas.offsetTop + y) + 'px';
el.style.color = color;
document.body.appendChild(el);
setTimeout(() => el.remove(), 800);
}
// UI更新
function updateUI() {
if (!ball1 || !ball2) return;
document.getElementById('p1-hp-bar').style.width = `${Math.max(0, ball1.hp)}%`;
document.getElementById('p1-hp-val').innerText = `${Math.ceil(ball1.hp)}%`;
document.getElementById('p1-cd-bar').style.width = `${100 - (ball1.cooldown / ball1.role.cooldown * 100)}%`;
document.getElementById('p1-cd-val').innerText = ball1.cooldown <= 0 ? 'READY' : 'CHARGING';
const p1Debuffs = document.getElementById('p1-debuffs');
p1Debuffs.innerHTML = '';
if (ball1.burnStacks > 0) p1Debuffs.innerHTML += `<i class="fa-solid fa-fire text-orange-500 animate-pulse"></i>`;
if (ball1.freezeTimer > 0) p1Debuffs.innerHTML += `<i class="fa-solid fa-icicles text-cyan-300 animate-pulse"></i>`;
document.getElementById('p2-hp-bar').style.width = `${Math.max(0, ball2.hp)}%`;
document.getElementById('p2-hp-val').innerText = `${Math.ceil(ball2.hp)}%`;
document.getElementById('p2-cd-bar').style.width = `${100 - (ball2.cooldown / ball2.role.cooldown * 100)}%`;
document.getElementById('p2-cd-val').innerText = ball2.cooldown <= 0 ? 'READY' : 'CHARGING';
const p2Debuffs = document.getElementById('p2-debuffs');
p2Debuffs.innerHTML = '';
if (ball2.burnStacks > 0) p2Debuffs.innerHTML += `<i class="fa-solid fa-fire text-orange-500 animate-pulse"></i>`;
if (ball2.freezeTimer > 0) p2Debuffs.innerHTML += `<i class="fa-solid fa-icicles text-cyan-300 animate-pulse"></i>`;
const elapsed = Math.floor((Date.now() - startTime) / 1000);
const mins = String(Math.floor(elapsed / 60)).padStart(2, '0');
const secs = String(elapsed % 60).padStart(2, '0');
document.getElementById('timer').innerText = `${mins}:${secs}`;
document.getElementById('skillCount').innerText = totalSkillsUsed;
}
function updateCardPreview(num, role) {
const prefix = `p${num}`;
const card = document.getElementById(`${prefix}-card`);
const iconBg = document.getElementById(`${prefix}-icon-bg`);
const icon = document.getElementById(`${prefix}-icon`);
const name = document.getElementById(`${prefix}-name`);
const desc = document.getElementById(`${prefix}-skill-desc`);
const hpBar = document.getElementById(`${prefix}-hp-bar`);
card.className = "status-card w-72 p-6 rounded-2xl flex flex-col gap-5 relative overflow-hidden card-selected";
iconBg.className = `w-14 h-14 rounded-xl flex items-center justify-center shadow-inner transition-colors duration-500`;
iconBg.style.backgroundColor = role.color;
icon.className = `fa-solid ${role.icon} text-2xl text-white`;
name.innerText = role.name;
desc.innerText = role.desc;
hpBar.className = `h-full hp-bar-fill relative`;
hpBar.style.backgroundColor = role.color;
}
function initGame(role1, role2) {
document.getElementById('p1-card').className = "status-card w-72 p-6 rounded-2xl flex flex-col gap-5 relative overflow-hidden";
document.getElementById('p2-card').className = "status-card w-72 p-6 rounded-2xl flex flex-col gap-5 relative overflow-hidden";
ball1 = new Ball(200, 300, role1, 1);
ball2 = new Ball(600, 300, role2, 2);
projectiles = [];
particles = [];
clones = [];
explosions = [];
isGameOver = false;
startTime = Date.now();
totalSkillsUsed = 0;
loop();
}
function endGame(winnerName) {
isGameOver = true;
const modal = document.getElementById('overlayModal');
const content = document.getElementById('modalContent');
content.innerHTML = `
<div class="mb-6">
<i class="fa-solid fa-crown text-6xl text-yellow-400 animate-bounce"></i>
</div>
<h2 class="text-4xl font-bold text-white mb-2">${winnerName} 获胜!</h2>
<p class="text-gray-400 mb-8">战斗历时 ${document.getElementById('timer').innerText}</p>
<button onclick="location.reload()" class="px-8 py-3 bg-white text-black hover:bg-gray-200 rounded-xl font-bold transition-all">
再来一局
</button>
`;
modal.classList.remove('opacity-0', 'pointer-events-none');
}
function restartGame() {
location.reload();
}
function loop() {
if (isGameOver) return;
ctx.clearRect(0, 0, WIDTH, HEIGHT);
ctx.strokeStyle = 'rgba(255,255,255,0.03)';
ctx.lineWidth = 1;
for(let i=0; i<WIDTH; i+=40) {
ctx.beginPath(); ctx.moveTo(i,0); ctx.lineTo(i,HEIGHT); ctx.stroke();
}
for(let i=0; i<HEIGHT; i+=40) {
ctx.beginPath(); ctx.moveTo(0,i); ctx.lineTo(WIDTH,i); ctx.stroke();
}
ball1.update();
ball1.draw();
ball2.update();
ball2.draw();
clones.forEach((c, i) => {
c.update();
c.draw();
if (c.markedForDeletion) clones.splice(i, 1);
});
// 绘制爆炸效果
explosions.forEach((e, i) => {
e.update();
e.draw();
if (e.markedForDeletion) explosions.splice(i, 1);
});
projectiles.forEach((p, i) => {
p.update();
p.draw();
if (p.markedForDeletion) projectiles.splice(i, 1);
});
particles.forEach((p, i) => {
p.update();
p.draw();
if (p.life <= 0) particles.splice(i, 1);
});
updateUI();
gameLoopId = requestAnimationFrame(loop);
}
// 初始化选角界面
ctx.fillStyle = '#111827';
ctx.fillRect(0,0,WIDTH,HEIGHT);
initRoleSelect();
</script>
</body>
</html>
全部评论 2
- 置顶
复制后放到新建文本文档里,保存后吧文件名的后缀从“txt”改成“html”就可以打开了
2026-11-07 来自 江苏
1 好玩就点个赞吧
2026-11-07 来自 江苏
1


















有帮助,赞一个