

这是为您生成的一个可直接运行的“打海盗”小游戏HTML文件,它是一款点击射击风格的休闲游戏,您需要快速点击海盗来得分。
<!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(145deg, #0a1f2e 0%, #1b3a4b 100%);
display: flex;
justify-content: center;
align-items: center;
font-family: 'Impact', 'Arial Black', 'Franklin Gothic Bold', sans-serif;
padding: 16px;
}
.game-container {
background: #3e2b1b;
padding: 24px 24px 30px;
border-radius: 80px 80px 40px 40px;
box-shadow: 0 25px 35px rgba(0, 0, 0, 0.8), inset 0 0 0 6px #6b4f2e, inset 0 0 20px #00000088;
border: 4px solid #c9a96b;
}
canvas {
display: block;
width: 800px;
height: 500px;
border-radius: 28px;
box-shadow: 0 0 0 6px #8b6f47, 0 20px 30px black;
cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><circle cx="16" cy="16" r="12" fill="none" stroke="%23ffd966" stroke-width="3"/><circle cx="16" cy="16" r="4" fill="%23ff3333"/><line x1="2" y1="16" x2="10" y2="16" stroke="%23ffd966" stroke-width="3"/><line x1="22" y1="16" x2="30" y2="16" stroke="%23ffd966" stroke-width="3"/><line x1="16" y1="2" x2="16" y2="10" stroke="%23ffd966" stroke-width="3"/><line x1="16" y1="22" x2="16" y2="30" stroke="%23ffd966" stroke-width="3"/></svg>') 16 16, crosshair;
background: #97b9d0;
}
.ui-panel {
display: flex;
justify-content: space-between;
align-items: center;
margin: 16px 8px 0;
color: #f5e7c8;
text-shadow: 3px 3px 0 #1e2b1c, 5px 5px 0 #00000055;
letter-spacing: 1px;
}
.score-board {
font-size: 2rem;
background: #1e2b1c;
padding: 8px 28px;
border-radius: 50px;
box-shadow: inset 0 -6px 0 #00000066, 0 6px 0 #4f3e2b;
border: 2px solid #d4af37;
color: #ffd966;
display: flex;
align-items: center;
gap: 14px;
}
.score-board span {
color: #fff4d6;
font-size: 2.4rem;
min-width: 80px;
text-align: center;
}
.restart-btn {
font-family: inherit;
font-size: 1.6rem;
background: #b8863d;
border: none;
border-radius: 50px;
padding: 10px 32px;
color: #1e2b1c;
text-shadow: 1px 1px 0 #ffdd99;
box-shadow: 0 9px 0 #4d2e1b, 0 10px 15px black;
cursor: pointer;
transition: all 0.05s ease-in;
letter-spacing: 1px;
border: 2px solid #f5d742;
font-weight: bold;
}
.restart-btn:active {
transform: translateY(7px);
box-shadow: 0 2px 0 #4d2e1b, 0 5px 12px black;
}
.lives {
font-size: 2rem;
display: flex;
align-items: center;
gap: 6px;
background: #1e2b1c;
padding: 8px 28px;
border-radius: 50px;
box-shadow: inset 0 -6px 0 #00000066, 0 6px 0 #4f3e2b;
border: 2px solid #d4af37;
}
.lives span {
font-size: 2.4rem;
color: #ff5e5e;
filter: drop-shadow(0 0 6px #ff0000aa);
}
.game-over-message {
position: absolute;
background: #000000dd;
color: #ffd966;
font-size: 3rem;
padding: 24px 56px;
border-radius: 40px;
border: 5px solid #b8863d;
text-align: center;
backdrop-filter: blur(4px);
pointer-events: none;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
white-space: nowrap;
box-shadow: 0 0 40px #000000;
display: none;
font-family: 'Impact', 'Arial Black', sans-serif;
letter-spacing: 3px;
}
.game-wrapper {
position: relative;
display: inline-block;
}
</style>
</head>
<body>
<div class="game-wrapper">
<div class="game-container">
<canvas id="gameCanvas" width="800" height="500"></canvas>
<div class="ui-panel">
<div class="score-board">⚔️ 得分 <span id="scoreDisplay">0</span></div>
<div class="lives">❤️ <span id="livesDisplay">3</span></div>
<button class="restart-btn" id="restartButton">⚓ 重新开炮</button>
</div>
</div>
<div class="game-over-message" id="gameOverMessage">💀 海盗获胜 💀</div>
</div>
<script>
(function() {
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.getElementById('scoreDisplay');
const livesDisplay = document.getElementById('livesDisplay');
const restartButton = document.getElementById('restartButton');
const gameOverMessage = document.getElementById('gameOverMessage');
// 游戏配置
const MAX_LIVES = 3;
const SPAWN_INTERVAL = 480; // 毫秒
const PIRATE_LIFETIME = 1100; // 海盗停留时间 (ms)
const MIN_RADIUS = 39; // 海盗最小半径
const MAX_RADIUS = 52; // 海盗最大半径
// 游戏状态
let score = 0;
let lives = MAX_LIVES;
let gameActive = false;
let gameOverTimer = null; // 用于自动隐藏信息
// 动态实体
let pirates = []; // { x, y, radius, spawnTime, id }
let nextPirateId = 0;
// 计时器
let spawnInterval = null;
let animationFrame = null;
let lastTimestamp = 0;
// ------------------------- 辅助函数 -------------------------
function random(min, max) {
return Math.random() * (max - min) + min;
}
// 重置所有游戏状态
function resetGame() {
// 清除所有定时器/动画
if (spawnInterval) {
clearInterval(spawnInterval);
spawnInterval = null;
}
if (animationFrame) {
cancelAnimationFrame(animationFrame);
animationFrame = null;
}
if (gameOverTimer) {
clearTimeout(gameOverTimer);
gameOverTimer = null;
}
// 重置数据
pirates = [];
score = 0;
lives = MAX_LIVES;
gameActive = true;
nextPirateId = 0;
updateUI();
// 隐藏游戏结束信息
gameOverMessage.style.display = 'none';
// 开始生成海盗
startSpawner();
// 开始渲染循环
lastTimestamp = performance.now();
animationFrame = requestAnimationFrame(gameLoop);
}
// 开始生成海盗的计时器
function startSpawner() {
if (spawnInterval) clearInterval(spawnInterval);
spawnInterval = setInterval(() => {
if (!gameActive) return;
spawnPirate();
}, SPAWN_INTERVAL);
}
// 生成一个新海盗 (出现在画布内,避开边缘)
function spawnPirate() {
const radius = random(MIN_RADIUS, MAX_RADIUS);
const padding = radius + 10;
const x = random(padding, canvas.width - padding);
const y = random(padding, canvas.height - padding);
pirates.push({
x,
y,
radius,
spawnTime: performance.now(),
id: nextPirateId++
});
}
// 更新UI (得分/生命)
function updateUI() {
scoreDisplay.textContent = score;
livesDisplay.textContent = lives;
}
// 结束游戏 (失败)
function endGame() {
if (!gameActive) return; // 已结束
gameActive = false;
// 停止生成海盗
if (spawnInterval) {
clearInterval(spawnInterval);
spawnInterval = null;
}
// 显示游戏结束信息 (停留一会儿)
gameOverMessage.style.display = 'block';
if (gameOverTimer) clearTimeout(gameOverTimer);
gameOverTimer = setTimeout(() => {
gameOverMessage.style.display = 'none';
gameOverTimer = null;
}, 2000);
// 停止渲染循环? (我们保留渲染循环,但不更新位置/状态,只绘制静止画面)
// 继续绘制,但gameActive为false会阻止逻辑更新。
}
// 减少生命,检查死亡
function loseLife() {
if (!gameActive) return;
lives -= 1;
updateUI();
if (lives <= 0) {
lives = 0;
updateUI();
endGame();
}
}
// 增加得分
function addScore() {
if (!gameActive) return;
score += 1;
updateUI();
}
// 从数组中移除过期或点击的海盗
function removePirate(pirateId) {
pirates = pirates.filter(p => p.id !== pirateId);
}
// ------------------------- 点击处理 (攻击) -------------------------
function handleCanvasClick(e) {
if (!gameActive) return;
const rect = canvas.getBoundingClientRect();
const scaleX = canvas.width / rect.width; // canvas物理像素比
const scaleY = canvas.height / rect.height;
const mouseX = (e.clientX - rect.left) * scaleX;
const mouseY = (e.clientY - rect.top) * scaleY;
// 从后向前检测 (上层海盗优先)
for (let i = pirates.length - 1; i >= 0; i--) {
const p = pirates[i];
const dx = mouseX - p.x;
const dy = mouseY - p.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist <= p.radius) {
// 命中海盗
addScore();
removePirate(p.id);
break; // 一枪只打中一个海盗
}
}
}
// ------------------------- 更新逻辑 (每帧) -------------------------
function updateGame(now) {
if (!gameActive) return;
// 1. 移除超时的海盗 (用反向遍历来安全删除)
for (let i = pirates.length - 1; i >= 0; i--) {
const p = pirates[i];
if (now - p.spawnTime >= PIRATE_LIFETIME) {
// 海盗逃跑了 -> 失去一颗心
loseLife();
pirates.splice(i, 1);
}
}
// 如果生命归零,endGame已经在loseLife里调用了,不用继续更新
}
// ------------------------- 绘制 -------------------------
function draw(now) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制大海背景 + 波浪效果
const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, '#2b5f7a');
gradient.addColorStop(0.6, '#1d4a5c');
gradient.addColorStop(1, '#0e2a36');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 波浪/云 (简单装饰)
ctx.fillStyle = '#ffffff10';
for (let i = 0; i < 8; i++) {
ctx.beginPath();
ctx.arc(100 + i * 120, 80 + Math.sin(i * 1.5 + now / 900) * 30, 60, 0, Math.PI * 2);
ctx.fill();
}
// 绘制所有海盗
pirates.forEach(p => {
const age = now - p.spawnTime;
let alpha = 1;
// 250ms 闪烁警告即将消失
if (age > PIRATE_LIFETIME - 250) {
const blink = Math.sin(now * 0.03) * 0.5 + 0.5;
alpha = 0.5 + blink * 0.5;
}
ctx.save();
ctx.globalAlpha = alpha;
// 绘制阴影
ctx.shadowColor = '#000000aa';
ctx.shadowBlur = 20;
ctx.shadowOffsetY = 6;
ctx.shadowOffsetX = 6;
// 海盗脸 (圆形)
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
// 皮肤色调
ctx.fillStyle = '#d4a373';
ctx.fill();
ctx.shadowBlur = 0; // 重置阴影避免文字阴影太重
ctx.shadowOffsetY = 0;
ctx.shadowOffsetX = 0;
// 海盗头巾 (红色)
ctx.beginPath();
ctx.arc(p.x, p.y - p.radius * 0.15, p.radius * 0.9, Math.PI * 1.1, Math.PI * 1.9);
ctx.lineWidth = p.radius * 0.25;
ctx.strokeStyle = '#9e2b2b';
ctx.stroke();
// 头巾结 / 小三角
ctx.beginPath();
ctx.moveTo(p.x + p.radius * 0.5, p.y - p.radius * 0.65);
ctx.lineTo(p.x + p.radius * 0.9, p.y - p.radius * 0.8);
ctx.lineTo(p.x + p.radius * 0.75, p.y - p.radius * 0.45);
ctx.fillStyle = '#b33e3e';
ctx.fill();
// 眼睛 (黑色眼罩 + 眼睛)
// 眼罩带子
ctx.beginPath();
ctx.strokeStyle = '#222222';
ctx.lineWidth = p.radius * 0.18;
ctx.moveTo(p.x - p.radius * 0.7, p.y - p.radius * 0.1);
ctx.lineTo(p.x + p.radius * 0.7, p.y - p.radius * 0.3);
ctx.stroke();
// 左眼
ctx.beginPath();
ctx.arc(p.x - p.radius * 0.28, p.y - p.radius * 0.05, p.radius * 0.15, 0, Math.PI * 2);
ctx.fillStyle = '#f0f0f0';
ctx.fill();
ctx.beginPath();
ctx.arc(p.x - p.radius * 0.3, p.y - p.radius * 0.08, p.radius * 0.06, 0, Math.PI * 2);
ctx.fillStyle = '#1a1a1a';
ctx.fill();
// 右眼 (眼罩遮盖)
ctx.beginPath();
ctx.arc(p.x + p.radius * 0.25, p.y - p.radius * 0.15, p.radius * 0.2, 0, Math.PI * 2);
ctx.fillStyle = '#222222';
ctx.fill();
// 眼罩高光
ctx.beginPath();
ctx.arc(p.x + p.radius * 0.18, p.y - p.radius * 0.22, p.radius * 0.05, 0, Math.PI * 2);
ctx.fillStyle = '#555555';
ctx.fill();
// 嘴巴 (微笑/邪恶)
ctx.beginPath();
ctx.strokeStyle = '#4a2e1e';
ctx.lineWidth = p.radius * 0.12;
ctx.arc(p.x, p.y + p.radius * 0.25, p.radius * 0.4, 0.1, Math.PI - 0.1);
ctx.stroke();
// 胡子 (几根线条)
ctx.beginPath();
ctx.strokeStyle = '#51321b';
ctx.lineWidth = p.radius * 0.09;
ctx.moveTo(p.x - p.radius * 0.35, p.y + p.radius * 0.45);
ctx.lineTo(p.x - p.radius * 0.1, p.y + p.radius * 0.6);
ctx.moveTo(p.x + p.radius * 0.35, p.y + p.radius * 0.4);
ctx.lineTo(p.x + p.radius * 0.1, p.y + p.radius * 0.6);
ctx.stroke();
// 耳环 (右侧)
ctx.beginPath();
ctx.arc(p.x + p.radius * 0.75, p.y + p.radius * 0.2, p.radius * 0.12, 0, Math.PI * 2);
ctx.fillStyle = '#d4af37';
ctx.fill();
ctx.beginPath();
ctx.arc(p.x + p.radius * 0.75, p.y + p.radius * 0.2, p.radius * 0.06, 0, Math.PI * 2);
ctx.fillStyle = '#f0e68c';
ctx.fill();
// 如果即将消失, 额外红色闪烁光环
if (age > PIRATE_LIFETIME - 250) {
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius + 6, 0, Math.PI * 2);
ctx.strokeStyle = '#ff3333';
ctx.lineWidth = 3;
ctx.stroke();
}
ctx.restore();
});
// 绘制准星跟随鼠标? (我们已经在CSS中使用了自定义光标)
}
// 游戏主循环
function gameLoop(now) {
if (!gameActive && gameOverMessage.style.display === 'block') {
// 如果游戏结束,我们仍绘制静态画面(海盗不动也不消失),但是不再更新逻辑
draw(now);
animationFrame = requestAnimationFrame(gameLoop);
return;
}
// 更新逻辑 (仅当游戏活跃)
if (gameActive) {
updateGame(now);
}
// 绘制所有内容
draw(now);
// 继续下一帧
animationFrame = requestAnimationFrame(gameLoop);
}
// ------------------------- 初始化 & 事件绑定 -------------------------
function init() {
// 重置并开始游戏
resetGame();
// 点击canvas攻击
canvas.addEventListener('click', handleCanvasClick);
// 重新开始按钮
restartButton.addEventListener('click', () => {
resetGame();
});
// 窗口尺寸变化不会影响 canvas 内部尺寸(固定800x500),但CSS可能缩放,点击坐标计算已经处理。
}
// 页面加载后启动
window.addEventListener('load', init);
// 清理工作(理论上单页应用, 但防止内存泄漏)
window.addEventListener('beforeunload', () => {
if (spawnInterval) clearInterval(spawnInterval);
if (animationFrame) cancelAnimationFrame(animationFrame);
if (gameOverTimer) clearTimeout(gameOverTimer);
});
})();
</script>
</body>
</html>
玩法与操作说明
游戏规则简单直接,您只需瞄准并点击海盗即可,它的核心乐趣在于快速反应和精准点击。
- 攻击海盗:使用您的鼠标(光标会变为瞄准镜)点击画布上出现的海盗,每击中一个,左上角的得分就会增加。
- 管理生命:每个海盗只会停留很短的时间,如果未能及时击中,海盗会逃跑,您将失去一颗心(生命值),当三颗心全部失去时,游戏结束。
- 重新开始:游戏结束后,点击下方的“重新开炮”按钮,即可重置分数和生命,开始新的一局。

