AI小球对战游戏(可运行)
2026-11-06 17:41:42
发布于:江苏
<!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);
}
</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 id="overlayModal" class="absolute inset-0 bg-black/60 backdrop-blur-sm flex flex-col items-center justify-center z-50 rounded-2xl transition-opacity duration-300">
<div id="modalContent" class="text-center">
<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-dice text-6xl text-white relative z-10 animate-bounce"></i>
</div>
<h2 class="text-3xl font-bold text-white mb-2">准备就绪</h2>
<p class="text-gray-400 mb-8 max-w-md mx-auto">系统将从四个元素球中随机抽取两个进行对决。<br>观察它们的技能特性,预测胜者!</p>
<button onclick="startMatchmaking()" class="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>
<!-- 底部:角色预览与统计 -->
<footer class="absolute bottom-8 w-full max-w-4xl flex justify-center gap-8 z-20">
<div class="bg-black/40 backdrop-blur-md border border-white/5 rounded-2xl p-4 flex gap-8 items-center">
<div class="text-center px-4 border-r border-white/10">
<div class="text-xs text-gray-500 uppercase font-bold mb-1">Match Time</div>
<div id="timer" class="text-xl font-mono text-white">00:00</div>
</div>
<div class="text-center px-4">
<div class="text-xs text-gray-500 uppercase font-bold mb-1">Total Skills</div>
<div id="skillCount" class="text-xl font-mono text-indigo-400">0</div>
</div>
</div>
</footer>
<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次快速伤害)',
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: 1 // 子弹可弹墙次数
}
};
const CONFIG = {
baseSpeed: 3.5,
projectileSpeed: 8,
burnDamage: 3,
burnTickCount: 5,
burnInterval: 15,
freezeDuration: 90,
tripleSpread: 0.3 // 三连发散开角度
};
let gameLoopId;
let startTime;
let isGameOver = false;
let isMatchmaking = false;
let totalSkillsUsed = 0;
let ball1, ball2;
let projectiles = [];
let particles = [];
// 实体基类
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 Ball extends Entity {
constructor(x, y, roleConfig, playerNum) {
super(x, y, 28, roleConfig.color);
this.role = roleConfig;
this.playerNum = playerNum; // 1 or 2
// 运动
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;
// Debuffs
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));
}
// AI随机转向
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 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; // 可弹墙次数(反弹球为1,其余为0)
this.bouncesLeft = this.maxBounces;
this.age = 0; // 存活帧数,避免子弹出生瞬间误判撞墙
}
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 {
this.markedForDeletion = true;
return;
}
}
}
const target = this.owner === ball1 ? ball2 : ball1;
const dist = Math.hypot(this.x - target.x, this.y - target.y);
if (dist < this.radius + target.radius) {
target.takeDamage(this.role.directDmg, this.color);
target.applyDebuff(this.role.skillType);
createParticles(this.x, this.y, this.color, 8);
this.markedForDeletion = true;
}
}
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;
// P1
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>`;
// P2
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>`;
// Stats
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 startMatchmaking() {
const modal = document.getElementById('overlayModal');
const content = document.getElementById('modalContent');
// 模拟随机过程
let shuffleCount = 0;
const keys = Object.keys(ROLES);
const interval = setInterval(() => {
const r1 = keys[Math.floor(Math.random() * keys.length)];
const r2 = keys.filter(k => k !== r1)[Math.floor(Math.random() * (keys.length - 1))];
updateCardPreview(1, ROLES[r1]);
updateCardPreview(2, ROLES[r2]);
shuffleCount++;
if (shuffleCount > 10) {
clearInterval(interval);
modal.classList.add('opacity-0', 'pointer-events-none');
initGame(ROLES[r1], ROLES[r2]);
}
}, 100);
}
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) {
// 重置DOM状态
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 = [];
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();
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);
</script>
</body>
</html>
这里空空如也


















有帮助,赞一个