

这是您要的“奥特曼大战怪兽”游戏HTML代码,这是一个使用Canvas和JavaScript编写的简单2D动作游戏。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">随机奥特曼大战怪兽</title>
<style>
* {
box-sizing: border-box;
user-select: none;
}
body {
margin: 0;
min-height: 100vh;
background: linear-gradient(45deg, #0a0f1e, #1a1f2e);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: 'Segoe UI', Roboto, system-ui, sans-serif;
}
.game-container {
background: #0b0e17;
border-radius: 2rem;
padding: 1.5rem 1.5rem 1rem;
box-shadow: 0 20px 30px rgba(0, 0, 0, 0.7), inset 0 0 20px rgba(255, 50, 50, 0.3);
border: 2px solid #ff4d4d;
}
canvas {
display: block;
width: 800px;
height: 500px;
border-radius: 1rem;
background: radial-gradient(circle at 20% 20%, #1f2a3f, #0a0e14);
box-shadow: inset 0 0 40px #00000088, 0 0 20px #ff4d4d;
cursor: crosshair;
border: 2px solid #ffb347;
}
.hud {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 1rem;
color: #ffeecc;
text-shadow: 0 0 8px #ff6600;
font-weight: bold;
font-size: 1.3rem;
letter-spacing: 2px;
}
.stats {
display: flex;
gap: 2.5rem;
background: #1e1e2e;
padding: 0.6rem 2rem;
border-radius: 3rem;
border: 1px solid #ffaa33;
box-shadow: 0 0 15px #ff7700;
}
.stats span {
color: #ffd966;
font-size: 1.6rem;
margin-left: 8px;
text-shadow: 0 0 12px #ffbb00;
}
button {
background: #ff3b3b;
border: none;
font-weight: bold;
font-size: 1.2rem;
padding: 0.7rem 2.2rem;
border-radius: 3rem;
color: #fff2d7;
letter-spacing: 2px;
cursor: pointer;
box-shadow: 0 6px 0 #8b0000, 0 8px 18px black;
transition: all 0.07s;
text-shadow: 0 0 6px gold;
border: 1px solid #ffaa00;
}
button:active {
transform: translateY(6px);
box-shadow: 0 2px 0 #8b0000, 0 5px 12px black;
}
button:disabled {
opacity: 0.5;
transform: translateY(4px);
box-shadow: 0 2px 0 #4a1a1a;
pointer-events: none;
filter: grayscale(0.6);
}
.message {
color: #ffaa33;
font-size: 1.1rem;
font-weight: 600;
text-shadow: 0 0 8px darkred;
background: #00000055;
padding: 0.3rem 1.5rem;
border-radius: 2rem;
}
</style>
</head>
<body>
<div class="game-container">
<canvas id="gameCanvas" width="800" height="500"></canvas>
<div class="hud">
<div class="stats">
<div>⚡奥特曼 <span id="ultraScore">0</span></div>
<div>👹怪兽 <span id="monsterScore">0</span></div>
</div>
<button id="resetButton">🔄 随机战场</button>
</div>
<div class="message" id="statusMessage">点击光束攻击怪兽!</div>
</div>
<script>
(function() {
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// 比分显示
const ultraScoreSpan = document.getElementById('ultraScore');
const monsterScoreSpan = document.getElementById('monsterScore');
const statusMessage = document.getElementById('statusMessage');
// 游戏状态
let ultraman = {
x: 150,
y: 300,
radius: 36,
hp: 100,
maxHp: 100,
color: '#3b9eff',
name: '奥特曼',
attackCooldown: 0,
attackCooldownMax: 25, // 帧数间隔
beamPower: 25,
invincibleFrames: 0
};
let monster = {
x: 650,
y: 300,
radius: 42,
hp: 120,
maxHp: 120,
color: '#c0392b',
name: '怪兽',
attackCooldown: 0,
attackCooldownMax: 45,
beamPower: 18,
invincibleFrames: 0
};
// 随机变化种子
let ultraScore = 0;
let monsterScore = 0;
// 光束特效
let beams = []; // 每个光束 { from, to, progress, maxProgress, color, width, damage }
// 游戏是否结束
let gameOver = false;
// 帧计数器 (用于冷却和动画)
let frame = 0;
// 鼠标位置,用于瞄准 (可选但没用上,保留为视觉效果)
let mouseX = 0, mouseY = 0;
// ---------- 随机参数生成 ----------
function randomizeGame() {
// 重置游戏结束标记
gameOver = false;
statusMessage.innerText = '点击光束攻击怪兽!';
// 随机奥特曼属性
ultraman.hp = 90 + Math.floor(Math.random() * 60); // 90~150
ultraman.maxHp = ultraman.hp;
ultraman.beamPower = 15 + Math.floor(Math.random() * 21); // 15~35
ultraman.attackCooldownMax = 15 + Math.floor(Math.random() * 25); // 15~40
ultraman.attackCooldown = 0;
ultraman.invincibleFrames = 0;
// 随机位置:偏左但Y轴随机
ultraman.x = 130 + Math.random() * 100;
ultraman.y = 150 + Math.random() * 200;
ultraman.radius = 32 + Math.floor(Math.random() * 12); // 32~43
// 随机颜色 (奥特曼还是银红蓝调,但有点变化)
const ultraHue = 200 + Math.floor(Math.random() * 40); // 200~240 蓝色系
ultraman.color = `hsl(${ultraHue}, 90%, 65%)`;
// 随机怪兽属性
monster.hp = 100 + Math.floor(Math.random() * 80); // 100~180
monster.maxHp = monster.hp;
monster.beamPower = 12 + Math.floor(Math.random() * 20); // 12~31
monster.attackCooldownMax = 30 + Math.floor(Math.random() * 35); // 30~64
monster.attackCooldown = 0;
monster.invincibleFrames = 0;
// 随机位置偏右
monster.x = 580 + Math.random() * 120;
monster.y = 150 + Math.random() * 200;
monster.radius = 36 + Math.floor(Math.random() * 16); // 36~51
// 随机怪兽颜色 (红/紫/暗绿)
const monsterHue = Math.random() < 0.7 ? 0 + Math.floor(Math.random() * 30) : 280 + Math.floor(Math.random() * 60);
monster.color = `hsl(${monsterHue}, 85%, 45%)`;
// 清空光束
beams = [];
// 重置分数? 重置游戏时分数归零
ultraScore = 0;
monsterScore = 0;
updateScoreDisplay();
// 状态消息微调
if (ultraman.beamPower > monster.beamPower) {
statusMessage.innerText = '⚡奥特曼光束更强!主动进攻!';
} else {
statusMessage.innerText = '👹怪兽力量强大,小心应对!';
}
}
// 更新UI分数
function updateScoreDisplay() {
ultraScoreSpan.textContent = ultraScore;
monsterScoreSpan.textContent = monsterScore;
}
// 重置分数与随机
function resetGame() {
randomizeGame();
}
// ---------- 攻击逻辑 ----------
// 奥特曼攻击怪兽 (玩家点击canvas触发/或者自动?但是我们让玩家点击canvas触发奥特曼攻击)
function ultramanAttack() {
if (gameOver) return;
if (ultraman.attackCooldown > 0) {
statusMessage.innerText = `⏳ 奥特曼蓄力中... (${Math.ceil(ultraman.attackCooldown / 10) / 10}s)`;
return;
}
if (ultraman.hp <= 0) {
statusMessage.innerText = '💀 奥特曼已经倒下了... 重置战场吧';
return;
}
if (monster.hp <= 0) {
statusMessage.innerText = '🏆 怪兽已被击败!点击重置继续';
return;
}
// 奥特曼发起攻击:生成光束
const damage = ultraman.beamPower;
// 光束从奥特曼位置到怪兽位置
beams.push({
fromX: ultraman.x,
fromY: ultraman.y,
toX: monster.x,
toY: monster.y,
progress: 0,
maxProgress: 1.0,
speed: 0.04 + Math.random() * 0.03,
color: ultraman.color,
width: 8 + Math.random() * 6,
damage: damage,
owner: 'ultra'
});
// 设置冷却
ultraman.attackCooldown = ultraman.attackCooldownMax;
// 显示消息
statusMessage.innerText = `🔵 奥特曼发射光线! (伤害 ${damage})`;
}
// 怪兽攻击奥特曼 (怪兽AI: 当冷却为0且游戏未结束时自动攻击)
function monsterAttack() {
if (gameOver) return;
if (monster.hp <= 0) return;
if (ultraman.hp <= 0) return;
// 生成怪兽光束
const damage = monster.beamPower;
beams.push({
fromX: monster.x,
fromY: monster.y,
toX: ultraman.x,
toY: ultraman.y,
progress: 0,
maxProgress: 1.0,
speed: 0.03 + Math.random() * 0.03,
color: monster.color,
width: 10 + Math.random() * 8,
damage: damage,
owner: 'monster'
});
monster.attackCooldown = monster.attackCooldownMax;
statusMessage.innerText = `🔥 怪兽反击! (伤害 ${damage})`;
}
// ---------- 更新逻辑(每帧)----------
function update() {
if (gameOver) return;
// 减少冷却
if (ultraman.attackCooldown > 0) ultraman.attackCooldown--;
if (monster.attackCooldown > 0) monster.attackCooldown--;
// 怪兽AI: 如果冷却为0且双方都活着,有概率攻击(每次冷却结束一定攻击)
if (monster.attackCooldown === 0 && monster.hp > 0 && ultraman.hp > 0 && !gameOver) {
// 稍微加点随机延迟,但整体仍然是有节奏的
if (Math.random() < 0.6) { // 60%概率攻击,避免过于频繁
monsterAttack();
} else {
// 如果没攻击,等下一帧再试,但冷却会保持0,所以可以快速再试
}
}
// 移动光束进度并应用伤害
for (let i = beams.length - 1; i >= 0; i--) {
const b = beams[i];
b.progress += b.speed;
// 当光束到达终点 (progress >= 1)
if (b.progress >= 1.0) {
// 应用伤害
if (b.owner === 'ultra') {
// 奥特曼攻击怪兽
if (monster.hp > 0) {
monster.hp = Math.max(0, monster.hp - b.damage);
ultraScore += b.damage;
updateScoreDisplay();
statusMessage.innerText = `💥 命中怪兽!造成 ${b.damage} 伤害`;
// 检查怪兽是否死亡
if (monster.hp <= 0) {
monster.hp = 0;
monsterScore = 0; // 为了让分数看起来有趣,但保留怪兽得分显示?我们保留比分展示但不清零对手分数。
// 游戏结束,但别让玩家继续攻击
gameOver = true;
statusMessage.innerText = '🎉 奥特曼胜利!点击重置继续战斗';
// 加分
ultraScore += 50; // 击杀奖励
updateScoreDisplay();
}
}
} else {
// 怪兽攻击奥特曼
if (ultraman.hp > 0) {
ultraman.hp = Math.max(0, ultraman.hp - b.damage);
monsterScore += b.damage;
updateScoreDisplay();
statusMessage.innerText = `💢 奥特曼受到 ${b.damage} 伤害!`;
if (ultraman.hp <= 0) {
ultraman.hp = 0;
gameOver = true;
statusMessage.innerText = '😵 怪兽胜利!点击重置再次挑战';
monsterScore += 50; // 击杀奖励
updateScoreDisplay();
}
}
}
// 移除光束
beams.splice(i, 1);
}
}
// 检查胜负后再次确认(可能血量归零后gameOver已经设置,这里无需重复)
if (ultraman.hp <= 0 && !gameOver) {
gameOver = true;
statusMessage.innerText = '😵 怪兽胜利!点击重置再次挑战';
monsterScore += 50;
updateScoreDisplay();
} else if (monster.hp <= 0 && !gameOver) {
gameOver = true;
statusMessage.innerText = '🎉 奥特曼胜利!点击重置继续战斗';
ultraScore += 50;
updateScoreDisplay();
}
// 一点点趣味:如果双方都活着但血量低,显示提示
if (!gameOver && ultraman.hp > 0 && monster.hp > 0) {
if (ultraman.hp < ultraman.maxHp * 0.3) {
statusMessage.innerText = '⚠️ 奥特曼能量不足!小心!';
} else if (monster.hp < monster.maxHp * 0.3) {
statusMessage.innerText = '⚡ 怪兽虚弱!继续攻击!';
}
}
}
// ---------- 渲染 ----------
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制星空背景
ctx.fillStyle = '#0a0e17';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < 100; i++) {
if (i % 2 === 0) {
ctx.fillStyle = `rgba(255, 255, 200, ${0.3 + Math.sin(frame * 0.02 + i) * 0.2})`;
} else {
ctx.fillStyle = `rgba(180, 220, 255, ${0.2 + Math.cos(frame * 0.03 + i) * 0.2})`;
}
ctx.beginPath();
ctx.arc((i * 23) % 800, (i * 13) % 500, 1.5, 0, Math.PI * 2);
ctx.fill();
}
// 绘制地面线
ctx.beginPath();
ctx.strokeStyle = '#ffaa3355';
ctx.lineWidth = 4;
ctx.setLineDash([20, 30]);
ctx.moveTo(0, 420);
ctx.lineTo(800, 420);
ctx.stroke();
ctx.setLineDash([]);
// 绘制奥特曼 (具有能量光环)
drawUltraman(ultraman);
// 绘制怪兽
drawMonster(monster);
// 绘制所有光束
for (let b of beams) {
// 光束进度: 从起点到终点插值
const curX = b.fromX + (b.toX - b.fromX) * b.progress;
const curY = b.fromY + (b.toY - b.fromY) * b.progress;
// 绘制光束轨迹 (发光效果)
ctx.beginPath();
ctx.moveTo(b.fromX, b.fromY);
ctx.lineTo(curX, curY);
ctx.strokeStyle = b.color;
ctx.lineWidth = b.width;
ctx.shadowColor = b.color;
ctx.shadowBlur = 20;
ctx.stroke();
// 光束头部光晕
ctx.beginPath();
ctx.arc(curX, curY, b.width * 0.9, 0, Math.PI * 2);
ctx.fillStyle = '#fffbe6';
ctx.shadowBlur = 35;
ctx.shadowColor = b.color;
ctx.fill();
// 重置阴影
ctx.shadowBlur = 0;
}
// 绘制血条 (放在上方)
drawHealthBar(ultraman, 30, 40, '奥特曼');
drawHealthBar(monster, 550, 40, '怪兽');
// 绘制额外装饰
ctx.shadowBlur = 0;
// 闪烁效果表示gameover
if (gameOver) {
ctx.font = 'bold 44px "Segoe UI", system-ui';
ctx.fillStyle = '#ffd966';
ctx.shadowColor = '#ff0000';
ctx.shadowBlur = 30;
ctx.fillText('GAME OVER', 250, 250);
ctx.shadowBlur = 0;
}
}
function drawUltraman(u) {
// 绘制奥特曼身体(圆 + 发光)
ctx.shadowColor = u.color;
ctx.shadowBlur = 28;
// 能量核心
ctx.beginPath();
ctx.arc(u.x, u.y, u.radius, 0, Math.PI * 2);
ctx.fillStyle = u.color;
ctx.fill();
// 眼睛
ctx.shadowBlur = 15;
ctx.beginPath();
ctx.arc(u.x - 12, u.y - 10, 8, 0, Math.PI * 2);
ctx.fillStyle = '#fff9e6';
ctx.fill();
ctx.beginPath();
ctx.arc(u.x + 12, u.y - 10, 8, 0, Math.PI * 2);
ctx.fill();
// 黑色眼珠
ctx.shadowBlur = 0;
ctx.beginPath();
ctx.arc(u.x - 12, u.y - 12, 4, 0, Math.PI * 2);
ctx.fillStyle = '#111';
ctx.fill();
ctx.beginPath();
ctx.arc(u.x + 12, u.y - 12, 4, 0, Math.PI * 2);
ctx.fill();
// 胸口计时器
ctx.beginPath();
ctx.arc(u.x, u.y + 10, 8, 0, Math.PI * 2);
ctx.fillStyle = ultraman.hp < ultraman.maxHp * 0.3 ? '#ff3333' : '#00ddff';
ctx.shadowBlur = 25;
ctx.fill();
// 文字标签
ctx.font = 'bold 18px "Segoe UI"';
ctx.fillStyle = '#fff1b8';
ctx.shadowBlur = 18;
ctx.shadowColor = u.color;
ctx.fillText('奥特曼', u.x - 40, u.y - u.radius - 18);
ctx.shadowBlur = 0;
}
function drawMonster(m) {
ctx.shadowColor = m.color;
ctx.shadowBlur = 30;
// 怪兽不规则形状 (圆+角)
ctx.beginPath();
ctx.arc(m.x, m.y, m.radius, 0, Math.PI * 2);
ctx.fillStyle = m.color;
ctx.fill();
// 怪兽角
ctx.shadowBlur = 20;
ctx.beginPath();
ctx.moveTo(m.x - 30, m.y - 35);
ctx.lineTo(m.x - 10, m.y - 65);
ctx.lineTo(m.x - 40, m.y - 50);
ctx.fillStyle = '#3a1a1a';
ctx.fill();
ctx.beginPath();
ctx.moveTo(m.x + 30, m.y - 35);
ctx.lineTo(m.x + 10, m.y - 65);
ctx.lineTo(m.x + 40, m.y - 50);
ctx.fill();
// 眼睛
ctx.shadowBlur = 12;
ctx.beginPath();
ctx.arc(m.x - 16, m.y - 15, 9, 0, Math.PI * 2);
ctx.fillStyle = '#ffd966';
ctx.fill();
ctx.beginPath();
ctx.arc(m.x + 16, m.y - 15, 9, 0, Math.PI * 2);
ctx.fill();
// 瞳孔
ctx.shadowBlur = 0;
ctx.beginPath();
ctx.arc(m.x - 16, m.y - 15, 5, 0, Math.PI * 2);
ctx.fillStyle = '#000';
ctx.fill();
ctx.beginPath();
ctx.arc(m.x + 16, m.y - 15, 5, 0, Math.PI * 2);
ctx.fill();
// 怪兽文字
ctx.font = 'bold 20px "Segoe UI"';
ctx.fillStyle = '#ffb3b3';
ctx.shadowBlur = 18;
ctx.shadowColor = m.color;
ctx.fillText('怪兽', m.x - 35, m.y - m.radius - 25);
ctx.shadowBlur = 0;
}
function drawHealthBar(entity, x, y, label) {
const width = 180;
const height = 18;
const hpPercent = entity.hp / entity.maxHp;
// 背景
ctx.shadowBlur = 12;
ctx.shadowColor = '#000';
ctx.fillStyle = '#1e1e2e';
ctx.beginPath();
ctx.roundRect(x, y, width, height, 10);
ctx.fill();
// 血量条
let gradient = ctx.createLinearGradient(x, y, x + width * hpPercent, y);
if (entity === ultraman) {
gradient.addColorStop(0, '#4aaaff');
gradient.addColorStop(1, '#aaddff');
} else {
gradient.addColorStop(0, '#ff5e5e');
gradient.addColorStop(1, '#ffaa33');
}
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.roundRect(x, y, width * hpPercent, height, 10);
ctx.fill();
// 边框
ctx.shadowBlur = 0;
ctx.strokeStyle = '#ffe6b0';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.roundRect(x, y, width, height, 10);
ctx.stroke();
// 文字
ctx.font = 'bold 14px "Segoe UI"';
ctx.fillStyle = '#ffefc0';
ctx.shadowBlur = 8;
ctx.shadowColor = 'black';
ctx.fillText(`${label} ${entity.hp}/${entity.maxHp}`, x + 10, y + 15);
ctx.shadowBlur = 0;
}
// 工具: Canvas roundRect
CanvasRenderingContext2D.prototype.roundRect = function(x, y, w, h, r) {
if (w < 2 * r) r = w / 2;
if (h < 2 * r) r = h / 2;
this.moveTo(x + r, y);
this.lineTo(x + w - r, y);
this.quadraticCurveTo(x + w, y, x + w, y + r);
this.lineTo(x + w, y + h - r);
this.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
this.lineTo(x + r, y + h);
this.quadraticCurveTo(x, y + h, x, y + h - r);
this.lineTo(x, y + r);
this.quadraticCurveTo(x, y, x + r, y);
return this;
};
// ---------- 事件绑定 ----------
canvas.addEventListener('click', (e) => {
// 玩家点击画布,奥特曼攻击
ultramanAttack();
});
// 动态更新鼠标位置 (没用但保留给视觉反馈)
canvas.addEventListener('mousemove', (e) => {
const rect = canvas.getBoundingClientRect();
const scaleX = canvas.width / rect.width;
const scaleY = canvas.height / rect.height;
mouseX = (e.clientX - rect.left) * scaleX;
mouseY = (e.clientY - rect.top) * scaleY;
});
// 重置按钮
document.getElementById('resetButton').addEventListener('click', () => {
resetGame();
});
// 游戏主循环
function gameLoop() {
if (!gameOver) {
update();
} else {
// 游戏结束时,继续更新光束?但可以保持光束动画完结,但是我们已经停止update里的攻击了
// 为了让光束继续飞完,但避免伤害已经结束,我们允许beam继续移动但不再造成伤害?
// 为了视觉好看,我们让光束继续移动但不再造成伤害?但为了简单,在gameOver时不再更新光束了(已经停止伤害)
// 但为了残留光束继续飞一点点? 忽略吧
// 为了不显得卡住,我们可以让一些残留光束继续飞行但不造成伤害,但我们不会往数组加新光束了
// 简单期间,不再更新beams,但可以继续绘制,不过光束会停滞,但无伤大雅,为了效果,我们允许光束速度变慢并移除? 保持简单。
// 实际上可以在gameOver时继续移动光束但不应用伤害?但为了简单我不做处理。
}
// 即使游戏结束,帧计数也要增加
frame++;
draw();
requestAnimationFrame(gameLoop);
}
// 页面初始化
randomizeGame();
gameLoop();
// 窗口调整保持清晰度不处理了 固定canvas尺寸
// 附加: 按住空格也可以攻击(可选彩蛋)
window.addEventListener('keydown', (e) => {
if (e.code === 'Space') {
e.preventDefault();
if (!gameOver) ultramanAttack();
}
});
})();
</script>
</body>
</html>
玩法与核心机制
游戏围绕奥特曼与怪兽之间的回合制光束对决展开,您可以通过点击或按空格键来操控奥特曼发起攻击。
- 对战逻辑:游戏开始时,双方的生命值、光束伤害和攻击冷却都会随机生成,让每局战斗都充满变数,您需要把握攻击时机,在怪兽攻击的间隙发动反击。
- 操作方式:您只需点击游戏画面或按下空格键,奥特曼就会向怪兽发射光束,每次攻击后需要等待冷却结束才能再次攻击,怪兽也会自动向您发起攻击。
- 胜负与反馈:屏幕上方会显示双方的血量条和实时得分,当一方生命值归零时,游戏结束并显示胜负结果,您可以通过“随机战场”按钮重置游戏,所有属性都会重新随机生成。

