打海盗小游戏,怒海炮火,海盗歼灭战

打海盗小游戏

打海盗小游戏

这是为您生成的一个可直接运行的“打海盗”小游戏HTML文件,它是一款基于Canvas的射击游戏,您可以通过鼠标点击来击退来袭的海盗船。

<!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, #0b1a2e, #1c3a5f);
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: 'Segoe UI', Roboto, system-ui, sans-serif;
        }
        .game-wrapper {
            background: #0d2b3e;
            padding: 20px 20px 10px;
            border-radius: 40px 40px 28px 28px;
            box-shadow: 0 20px 30px rgba(0, 0, 0, 0.7), 0 0 0 2px #6b4d2b inset, 0 0 15px #e6b80080;
        }
        canvas {
            display: block;
            width: 800px;
            height: 500px;
            border-radius: 24px;
            cursor: crosshair;
            box-shadow: inset 0 0 15px #00000099, 0 0 20px #000000;
            background: #162b38;
        }
        .top-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 12px;
            padding: 6px 16px 8px;
            color: #f2e2b3;
            text-shadow: 2px 2px 0 #1a1a1a;
            font-weight: bold;
            font-size: 1.3rem;
            letter-spacing: 2px;
        }
        .score-board {
            background: #1d3e4e;
            padding: 6px 24px;
            border-radius: 40px;
            border: 2px solid #e7b42c;
            box-shadow: inset 0 -4px 0 #0a1e2a, 0 4px 0 #0a1e2a;
            color: #ffd966;
        }
        .health-board {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .heart {
            color: #ff4d4d;
            font-size: 1.9rem;
            line-height: 1;
            filter: drop-shadow(0 0 6px #ff0000aa);
        }
        .restart-btn {
            background: #c99b3b;
            border: none;
            border-radius: 40px;
            padding: 8px 28px;
            font-weight: bold;
            font-size: 1.2rem;
            color: #1a2a32;
            box-shadow: 0 6px 0 #7a5e2a, 0 6px 8px black;
            cursor: pointer;
            transition: 0.05s linear;
            letter-spacing: 1px;
            border: 2px solid #ffe27a;
        }
        .restart-btn:active {
            transform: translateY(6px);
            box-shadow: 0 0 0 #7a5e2a, 0 6px 8px black;
        }
        .instructions {
            display: flex;
            justify-content: center;
            gap: 30px;
            font-size: 0.9rem;
            color: #b9cfd9;
            padding: 0 0 6px;
        }
        .instructions span {
            background: #1b2f3b;
            padding: 4px 12px;
            border-radius: 30px;
            border: 1px solid #9c864d;
        }
    </style>
</head>
<body>
<div class="game-wrapper">
    <canvas id="gameCanvas" width="800" height="500"></canvas>
    <div class="top-bar">
        <div class="score-board">💥 得分 <span id="scoreDisplay">0</span></div>
        <div class="health-board" id="healthDisplay">
            <span class="heart">❤️</span><span class="heart">❤️</span><span class="heart">❤️</span>
        </div>
        <button class="restart-btn" id="restartButton">⚓ 重新开战</button>
    </div>
    <div class="instructions">
        <span>🖱️ 点击海盗船开火</span>
        <span>⚡ 海盗会反击</span>
    </div>
</div>
<script>
    (function() {
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        const scoreSpan = document.getElementById('scoreDisplay');
        const healthDisplay = document.getElementById('healthDisplay');
        const restartBtn = document.getElementById('restartButton');
        // ---------- 游戏尺寸常量 ----------
        const CW = canvas.width;   // 800
        const CH = canvas.height;  // 500
        // ---------- 游戏状态 ----------
        let score = 0;
        let health = 3;           // 最大3颗心
        let gameOver = false;
        let gameWin = false;      // 可选胜利条件 (本游戏简化:生存并累积得分)
        let enemies = [];
        let particles = [];       // 爆炸/水花效果
        let frameCounter = 0;     // 用于生成敌人计时
        // 生成敌人参数 (每帧概率)
        const ENEMY_SPAWN_RATE = 0.022;  // 每帧约2.2%概率生成一个海盗
        const MAX_ENEMIES = 19;
        // ---------- 玩家船只 (用于视觉表现) ----------
        const playerShip = {
            x: CW / 2,
            y: CH - 40,
            width: 56,
            height: 30
        };
        // ---------- 敌人(海盗船)类 ----------
        class Enemy {
            constructor() {
                this.width = 44 + Math.random() * 18;   // 44~62
                this.height = 32 + Math.random() * 14;  // 32~46
                // 从左右两侧进入,或从上方?海盗船从两侧来更有感觉
                if (Math.random() < 0.5) {
                    // 从左方
                    this.x = -this.width - Math.random() * 80;
                } else {
                    // 从右方
                    this.x = CW + this.width + Math.random() * 80;
                }
                // 垂直方向随机,尽量避开底部(玩家区域)
                this.y = 60 + Math.random() * (CH - 180);
                // 速度朝画面中央大致方向
                const targetX = CW / 2 + (Math.random() - 0.5) * 200;
                const targetY = CH / 2 + (Math.random() - 0.5) * 140;
                const dx = targetX - this.x;
                const dy = targetY - this.y;
                const len = Math.hypot(dx, dy);
                const speed = 0.9 + Math.random() * 0.8; // 0.9~1.7
                this.vx = (dx / len) * speed;
                this.vy = (dy / len) * speed;
                this.hp = 1;          // 一击必杀
                this.radius = Math.max(this.width, this.height) * 0.45; // 用于点击检测
                // 绘制稍微变化
                this.angle = Math.atan2(this.vy, this.vx);
            }
            // 更新位置
            update() {
                this.x += this.vx;
                this.y += this.vy;
                // 如果完全离开画面(超出边界)则标记为可移除 (避免卡在边缘)
                const margin = 80;
                if (this.x < -margin || this.x > CW + margin || this.y < -margin || this.y > CH + margin) {
                    return false; // 标记删除
                }
                return true;
            }
            // 检查点击是否击中 (基于圆形检测)
            isHit(clickX, clickY) {
                const dx = clickX - this.x;
                const dy = clickY - this.y;
                const dist = Math.sqrt(dx * dx + dy * dy);
                return dist < this.radius;
            }
            // 绘制海盗船
            draw() {
                const x = this.x, y = this.y;
                const w = this.width, h = this.height;
                const halfW = w / 2, halfH = h / 2;
                // 船身 (深棕色)
                ctx.fillStyle = '#5a3e1b';
                ctx.shadowColor = '#000000aa';
                ctx.shadowBlur = 12;
                ctx.beginPath();
                ctx.ellipse(x, y, halfW * 0.9, halfH * 0.8, 0, 0, Math.PI * 2);
                ctx.fill();
                // 甲板/船舱 (暗红色)
                ctx.fillStyle = '#8b3a1a';
                ctx.shadowBlur = 8;
                ctx.beginPath();
                ctx.ellipse(x - 2, y - 3, halfW * 0.7, halfH * 0.6, 0, 0, Math.PI * 2);
                ctx.fill();
                // 骷髅旗帜 (黑帆+白骷髅)
                ctx.shadowBlur = 10;
                // 主帆
                ctx.fillStyle = '#1f1f1f';
                ctx.beginPath();
                ctx.moveTo(x + 6, y - halfH * 0.7);
                ctx.lineTo(x + 20, y - halfH * 0.9);
                ctx.lineTo(x + 12, y - halfH * 0.3);
                ctx.closePath();
                ctx.fill();
                // 白色骷髅简化标志
                ctx.fillStyle = '#f0f0f0';
                ctx.beginPath();
                ctx.arc(x + 13, y - halfH * 0.55, 4, 0, Math.PI * 2);
                ctx.fill();
                ctx.fillStyle = '#1f1f1f';
                ctx.beginPath();
                ctx.arc(x + 11, y - halfH * 0.57, 1.2, 0, Math.PI * 2);
                ctx.arc(x + 15, y - halfH * 0.57, 1.2, 0, Math.PI * 2);
                ctx.fill();
                // 海盗眼罩/红点
                ctx.fillStyle = '#c44';
                ctx.beginPath();
                ctx.arc(x + 27, y - halfH * 0.2, 3, 0, Math.PI * 2);
                ctx.fill();
                // 炮口 (黑色圆点)
                ctx.fillStyle = '#2b2b2b';
                ctx.shadowBlur = 6;
                ctx.beginPath();
                ctx.arc(x - halfW * 0.7, y + halfH * 0.3, 4, 0, Math.PI * 2);
                ctx.fill();
                // 红色警示 (海盗眼睛/船灯)
                ctx.fillStyle = '#d42';
                ctx.shadowBlur = 18;
                ctx.beginPath();
                ctx.arc(x - halfW * 0.7, y + halfH * 0.3, 3, 0, Math.PI * 2);
                ctx.fill();
                // 重置阴影
                ctx.shadowBlur = 0;
                ctx.shadowColor = 'transparent';
            }
        }
        // ---------- 爆炸/水花粒子 ----------
        class Particle {
            constructor(x, y, color, speedScale = 1) {
                this.x = x;
                this.y = y;
                const angle = Math.random() * Math.PI * 2;
                const spd = 1.5 + Math.random() * 4 * speedScale;
                this.vx = Math.cos(angle) * spd;
                this.vy = Math.sin(angle) * spd;
                this.size = 3 + Math.random() * 6;
                this.life = 1.0;
                this.decay = 0.015 + Math.random() * 0.02;
                this.color = color;
            }
            update() {
                this.x += this.vx;
                this.y += this.vy;
                this.vx *= 0.98;
                this.vy *= 0.98;
                this.life -= this.decay;
                return this.life > 0;
            }
            draw() {
                ctx.globalAlpha = this.life;
                ctx.fillStyle = this.color;
                ctx.shadowBlur = 14;
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size * this.life, 0, Math.PI * 2);
                ctx.fill();
                ctx.globalAlpha = 1.0;
                ctx.shadowBlur = 0;
            }
        }
        // ---------- 生成粒子效果 ----------
        function spawnExplosion(x, y, baseColor = '#ffaa33', count = 16) {
            for (let i = 0; i < count; i++) {
                const color = i % 3 === 0 ? '#ffdd44' : (i % 3 === 1 ? baseColor : '#ff6600');
                particles.push(new Particle(x, y, color, 1.3));
            }
            // 额外一些烟雾/木屑
            for (let i = 0; i < 6; i++) {
                particles.push(new Particle(x, y, '#b87c4b', 0.8));
            }
        }
        // ---------- 玩家受伤 ----------
        function damagePlayer() {
            if (gameOver) return;
            health--;
            // 受伤闪烁: 简化,添加一些红色粒子在玩家船位置
            spawnExplosion(playerShip.x, playerShip.y - 10, '#ff3333', 20);
            // 更新生命显示
            updateHealthDisplay();
            if (health <= 0) {
                health = 0;
                gameOver = true;
                // 清除所有敌人(停止进攻)
                enemies = [];
            }
        }
        // 更新血量UI
        function updateHealthDisplay() {
            const hearts = healthDisplay.querySelectorAll('.heart');
            hearts.forEach((heart, index) => {
                if (index < health) {
                    heart.style.opacity = '1';
                    heart.style.filter = 'drop-shadow(0 0 6px #ff0000aa)';
                } else {
                    heart.style.opacity = '0.25';
                    heart.style.filter = 'grayscale(1)';
                }
            });
        }
        // ---------- 重置游戏 ----------
        function resetGame() {
            score = 0;
            health = 3;
            gameOver = false;
            gameWin = false;
            enemies = [];
            particles = [];
            frameCounter = 0;
            scoreSpan.textContent = '0';
            updateHealthDisplay();
            // 玩家船位置重置(不必须但求整洁)
            playerShip.x = CW / 2;
            playerShip.y = CH - 40;
        }
        // ---------- 处理点击(开火)----------
        function handleCanvasClick(e) {
            if (gameOver) return;  // 游戏结束不能开火
            const rect = canvas.getBoundingClientRect();
            const scaleX = canvas.width / rect.width;
            const scaleY = canvas.height / rect.height;
            const mouseX = (e.clientX - rect.left) * scaleX;
            const mouseY = (e.clientY - rect.top) * scaleY;
            // 从后往前遍历(便于移除)
            let hitCount = 0;
            for (let i = enemies.length - 1; i >= 0; i--) {
                const enemy = enemies[i];
                if (enemy.isHit(mouseX, mouseY)) {
                    // 击中!
                    spawnExplosion(enemy.x, enemy.y, '#ffaa00', 18);
                    // 移除敌人
                    enemies.splice(i, 1);
                    score += 10;
                    scoreSpan.textContent = score;
                    hitCount++;
                    // 一枪只打一个敌人,但如果你愿意可以穿透,这里只打第一个碰到的 (为了手感)
                    break;  // 一颗子弹只消灭一个敌人
                }
            }
            // 如果没有打中任何敌人,也产生一点小水花或者空枪效果
            if (hitCount === 0) {
                // 开枪火花在鼠标位置
                for (let i = 0; i < 5; i++) {
                    particles.push(new Particle(mouseX, mouseY, '#aaddff', 0.7));
                }
            }
        }
        // ---------- 更新所有游戏逻辑 ----------
        function update() {
            if (gameOver) {
                // 游戏结束,仍然更新粒子淡出
                particles = particles.filter(p => p.update());
                return;
            }
            // 1. 生成敌人
            if (enemies.length < MAX_ENEMIES && Math.random() < ENEMY_SPAWN_RATE) {
                enemies.push(new Enemy());
            }
            // 2. 更新敌人位置,并检测是否攻击到玩家区域(底部/玩家船)
            const newEnemies = [];
            for (let enemy of enemies) {
                const stillInside = enemy.update();  // 更新位置并判断边界
                if (!stillInside) {
                    // 超出边界,移除(不加扣血)
                    continue;
                }
                // 敌人是否靠近玩家船区域 (底部玩家船位置)
                // 玩家船区域约: x±40, y-20到y+15 (y=CH-40)
                const dx = enemy.x - playerShip.x;
                const dy = enemy.y - playerShip.y + 10; // 偏移调整
                // 简易碰撞:如果敌人中心和玩家船距离小于60,并且敌人y大于CH-130(快接近底部)
                const distToPlayer = Math.sqrt(dx * dx + dy * dy);
                if (distToPlayer < 62 && enemy.y > CH - 155) {
                    // 海盗撞到玩家!扣血+爆炸+移除敌船
                    damagePlayer();
                    spawnExplosion(enemy.x, enemy.y, '#ff5555', 15);
                    // 敌人被撞毁, 不加入新数组
                    continue;
                }
                // 检查敌人是否已经到达画面底部太深 (避免挂边不消失)
                if (enemy.y > CH + 60) {
                    continue;
                }
                newEnemies.push(enemy);
            }
            enemies = newEnemies;
            // 3. 更新粒子
            particles = particles.filter(p => p.update());
            // 4. 如果健康归零,确保游戏结束标记 (damagePlayer里已经设置)
            if (health <= 0 && !gameOver) {
                gameOver = true;
                enemies = [];
            }
        }
        // ---------- 绘制一切 ----------
        function draw() {
            ctx.clearRect(0, 0, CW, CH);
            // 海洋背景 (深色波浪)
            const grad = ctx.createLinearGradient(0, 0, 0, CH);
            grad.addColorStop(0, '#0e2a39');
            grad.addColorStop(0.7, '#194452');
            grad.addColorStop(1, '#1d5668');
            ctx.fillStyle = grad;
            ctx.fillRect(0, 0, CW, CH);
            // 绘制波浪线条
            ctx.strokeStyle = '#5fa5b0';
            ctx.lineWidth = 2;
            ctx.globalAlpha = 0.35;
            for (let i = 0; i < 6; i++) {
                const y = 80 + i * 70 + Math.sin(frameCounter * 0.02 + i) * 10;
                ctx.beginPath();
                ctx.moveTo(0, y);
                for (let x = 0; x < CW; x += 40) {
                    const waveY = y + Math.sin(x * 0.02 + frameCounter * 0.05 + i) * 11;
                    ctx.lineTo(x, waveY);
                }
                ctx.strokeStyle = '#8ad0e0';
                ctx.stroke();
            }
            ctx.globalAlpha = 1.0;
            // ----- 绘制玩家船 (简单但醒目) -----
            ctx.shadowColor = '#000000aa';
            ctx.shadowBlur = 12;
            // 船体
            ctx.fillStyle = '#3b6b8b';
            ctx.beginPath();
            ctx.ellipse(playerShip.x, playerShip.y, 44, 20, 0, 0, Math.PI * 2);
            ctx.fill();
            ctx.fillStyle = '#1f4b6e';
            ctx.beginPath();
            ctx.ellipse(playerShip.x - 6, playerShip.y - 5, 30, 14, 0, 0, Math.PI * 2);
            ctx.fill();
            // 主炮
            ctx.fillStyle = '#b0b0b0';
            ctx.shadowBlur = 8;
            ctx.beginPath();
            ctx.ellipse(playerShip.x, playerShip.y - 22, 10, 8, 0, 0, Math.PI * 2);
            ctx.fill();
            ctx.fillStyle = '#d4af37';
            ctx.beginPath();
            ctx.arc(playerShip.x, playerShip.y - 26, 7, 0, Math.PI * 2);
            ctx.fill();
            // 炮口火焰效果 (动态)
            ctx.fillStyle = '#f90';
            ctx.shadowBlur = 25;
            ctx.beginPath();
            ctx.arc(playerShip.x, playerShip.y - 33, 6 + Math.sin(frameCounter * 0.3) * 3, 0, Math.PI * 2);
            ctx.fill();
            ctx.shadowBlur = 0;
            // ----- 绘制所有敌人 -----
            for (let enemy of enemies) {
                enemy.draw();
            }
            // ----- 绘制粒子 (最上层) -----
            for (let p of particles) {
                p.draw();
            }
            // ----- 绘制游戏结束 / 胜利信息 -----
            if (gameOver) {
                ctx.shadowBlur = 20;
                ctx.shadowColor = '#000000';
                ctx.font = 'bold 64px "Segoe UI", "Arial Black", sans-serif';
                ctx.fillStyle = '#d43b2b';
                ctx.strokeStyle = '#1a1a1a';
                ctx.lineWidth = 8;
                ctx.textAlign = 'center';
                ctx.textBaseline = 'middle';
                ctx.strokeText('💀 游戏结束 💀', CW / 2, CH / 2 - 30);
                ctx.fillText('💀 游戏结束 💀', CW / 2, CH / 2 - 30);
                ctx.font = 'bold 32px "Segoe UI"';
                ctx.fillStyle = '#ffd966';
                ctx.strokeStyle = '#1a1a1a';
                ctx.lineWidth = 4;
                ctx.strokeText('点击 "重新开战" 再来一局', CW / 2, CH / 2 + 60);
                ctx.fillText('点击 "重新开战" 再来一局', CW / 2, CH / 2 + 60);
                ctx.shadowBlur = 0;
            }
            // 显示分数在画面中间(小提示)也可以
        }
        // ---------- 动画循环 ----------
        function gameLoop() {
            frameCounter++;
            // 每帧更新逻辑
            update();
            // 绘制
            draw();
            requestAnimationFrame(gameLoop);
        }
        // ---------- 事件绑定 ----------
        canvas.addEventListener('click', handleCanvasClick);
        restartBtn.addEventListener('click', () => {
            resetGame();
        });
        // 移动端触摸支持 (避免双击缩放)
        canvas.addEventListener('touchstart', (e) => {
            e.preventDefault();
            const rect = canvas.getBoundingClientRect();
            const touch = e.touches[0];
            const scaleX = canvas.width / rect.width;
            const scaleY = canvas.height / rect.height;
            const mouseX = (touch.clientX - rect.left) * scaleX;
            const mouseY = (touch.clientY - rect.top) * scaleY;
            if (gameOver) return;
            // 模拟点击逻辑
            let hit = false;
            for (let i = enemies.length - 1; i >= 0; i--) {
                const enemy = enemies[i];
                if (enemy.isHit(mouseX, mouseY)) {
                    spawnExplosion(enemy.x, enemy.y, '#ffaa00', 18);
                    enemies.splice(i, 1);
                    score += 10;
                    scoreSpan.textContent = score;
                    hit = true;
                    break;
                }
            }
            if (!hit) {
                for (let i = 0; i < 5; i++) {
                    particles.push(new Particle(mouseX, mouseY, '#aaddff', 0.7));
                }
            }
        }, { passive: false });
        // 初始化显示
        updateHealthDisplay();
        // 启动游戏循环
        gameLoop();
        // 避免右键菜单弹出
        canvas.addEventListener('contextmenu', (e) => e.preventDefault());
        // 初始敌人快速出现
        for (let i = 0; i < 3; i++) {
            const e = new Enemy();
            // 让敌人稍微靠近画面一点
            e.x = 200 + i * 200;
            e.y = 120 + i * 60;
            enemies.push(e);
        }
    })();
</script>
</body>
</html>

游戏核心特性解析

游戏的核心是考验您的反应速度,通过点击来击退不断靠近的海盗船,您可以快速上手,体验紧张刺激的防御战。

  • 核心玩法:游戏画面中会不断生成海盗船,它们会从画面边缘向您的船只(位于底部中央)移动,您需要使用鼠标(或触摸)点击海盗船来开火将其击沉,每击沉一艘可获得10分。
  • 生命与失败条件:屏幕顶部的爱心代表您的生命值(共3点),如果海盗船靠近并撞击到您的船只,生命值会减少,同时游戏画面会给出提示,当生命值归零时,游戏结束。
  • 交互与反馈:点击海盗船时,该位置会触发爆炸粒子效果,给玩家清晰的击中反馈,您可以通过顶部的“重新开战”按钮随时重置游戏。

打海盗小游戏

发表评论