马年跳跳乐:HTML小游戏

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>马年跳跳乐 - 新春小游戏</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background: linear-gradient(to bottom, #0a1f3d, #1a0b2e);
            color: #fff;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
            overflow-x: hidden;
        }
        
        .header {
            text-align: center;
            margin-bottom: 20px;
            width: 100%;
        }
        
        h1 {
            color: #ffcc00;
            font-size: 2.5rem;
            text-shadow: 0 0 10px rgba(255, 204, 0, 0.7);
            margin-bottom: 10px;
            background: linear-gradient(to right, #ff9900, #ffcc00);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
        
        .subtitle {
            color: #ff9966;
            font-size: 1.2rem;
            margin-bottom: 15px;
        }
        
        .game-info {
            display: flex;
            justify-content: space-between;
            width: 800px;
            max-width: 90%;
            margin-bottom: 15px;
            background-color: rgba(0, 0, 0, 0.3);
            padding: 15px;
            border-radius: 10px;
            border: 2px solid #ffcc00;
        }
        
        .info-box {
            text-align: center;
        }
        
        .info-label {
            font-size: 0.9rem;
            color: #aaa;
        }
        
        .info-value {
            font-size: 1.8rem;
            font-weight: bold;
            color: #ffcc00;
        }
        
        .game-container {
            position: relative;
            width: 800px;
            max-width: 90%;
            height: 400px;
            background-color: #111;
            border: 4px solid #ffcc00;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 0 30px rgba(255, 204, 0, 0.5);
            margin-bottom: 20px;
        }
        
        #game-canvas {
            width: 100%;
            height: 100%;
            background: linear-gradient(to bottom, #0c2d5e, #0a1f3d);
        }
        
        .controls {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 15px;
            margin-bottom: 20px;
            width: 800px;
            max-width: 90%;
        }
        
        button {
            background: linear-gradient(to bottom, #ff9900, #cc6600);
            color: white;
            border: none;
            padding: 12px 25px;
            font-size: 1.1rem;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s;
            font-weight: bold;
            box-shadow: 0 5px 15px rgba(255, 153, 0, 0.4);
        }
        
        button:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(255, 153, 0, 0.6);
        }
        
        button:active {
            transform: translateY(1px);
        }
        
        .instructions {
            background-color: rgba(0, 0, 0, 0.3);
            padding: 20px;
            border-radius: 10px;
            width: 800px;
            max-width: 90%;
            margin-bottom: 20px;
            border-left: 4px solid #ff9966;
        }
        
        .instructions h3 {
            color: #ffcc00;
            margin-bottom: 10px;
        }
        
        .instructions ul {
            padding-left: 20px;
        }
        
        .instructions li {
            margin-bottom: 8px;
            line-height: 1.5;
        }
        
        .highlight {
            color: #ffcc00;
            font-weight: bold;
        }
        
        .new-year-message {
            color: #ff9966;
            font-size: 1.1rem;
            text-align: center;
            margin-top: 10px;
            font-style: italic;
        }
        
        .footer {
            margin-top: 20px;
            text-align: center;
            color: #888;
            font-size: 0.9rem;
            width: 100%;
        }
        
        .game-over {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.85);
            display: none;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            z-index: 10;
            border-radius: 10px;
        }
        
        .game-over h2 {
            color: #ff3333;
            font-size: 3rem;
            margin-bottom: 20px;
            text-shadow: 0 0 10px rgba(255, 51, 51, 0.7);
        }
        
        .game-over p {
            font-size: 1.5rem;
            margin-bottom: 30px;
            color: #ffcc00;
        }
        
        @media (max-width: 768px) {
            h1 {
                font-size: 1.8rem;
            }
            
            .game-info, .controls, .instructions {
                width: 95%;
            }
            
            .game-container {
                height: 300px;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>🐎 马年跳跳乐 🐎</h1>
        <div class="subtitle">农历丙午马年新春特别版</div>
    </div>
    
    <div class="game-info">
        <div class="info-box">
            <div class="info-label">得分</div>
            <div id="score" class="info-value">0</div>
        </div>
        <div class="info-box">
            <div class="info-label">最高分</div>
            <div id="high-score" class="info-value">0</div>
        </div>
        <div class="info-box">
            <div class="info-label">速度等级</div>
            <div id="speed-level" class="info-value">1</div>
        </div>
        <div class="info-box">
            <div class="info-label">状态</div>
            <div id="status" class="info-value">准备开始</div>
        </div>
    </div>
    
    <div class="game-container">
        <canvas id="game-canvas" width="800" height="400"></canvas>
        <div class="game-over" id="game-over-screen">
            <h2>游戏结束</h2>
            <p>本次得分: <span id="final-score">0</span></p>
            <button id="restart-btn">再来一次</button>
        </div>
    </div>
    
    <div class="controls">
        <button id="start-btn">开始游戏</button>
        <button id="pause-btn">暂停游戏</button>
        <button id="jump-btn">立即跳跃</button>
        <button id="reset-btn">重置游戏</button>
    </div>
    
    <div class="instructions">
        <h3>游戏说明</h3>
        <ul>
            <li>控制小马跳过<span class="highlight">鞭炮</span><span class="highlight">灯笼</span>,每跳过一个得1分</li>
            <li><span class="highlight">空格键</span><span class="highlight">点击跳跃按钮</span>控制小马跳跃</li>
            <li>每得10分,游戏速度提升一个等级</li>
            <li>撞到障碍物或地面则游戏结束</li>
            <li>游戏最高分会保存在本地,努力刷新记录吧!</li>
        </ul>
        <div class="new-year-message">新春快乐,马到成功!丙午马年大吉大利!</div>
    </div>
    
    <div class="footer">
        <p>马年跳跳乐 | 农历丙午马年(2026)新春特别游戏 | 使用HTML5 Canvas制作</p>
    </div>

    <script>
        // 获取Canvas元素和上下文
        const canvas = document.getElementById('game-canvas');
        const ctx = canvas.getContext('2d');
        
        // 获取DOM元素
        const scoreElement = document.getElementById('score');
        const highScoreElement = document.getElementById('high-score');
        const speedLevelElement = document.getElementById('speed-level');
        const statusElement = document.getElementById('status');
        const finalScoreElement = document.getElementById('final-score');
        const gameOverScreen = document.getElementById('game-over-screen');
        
        // 游戏变量
        let gameRunning = false;
        let gamePaused = false;
        let score = 0;
        let highScore = localStorage.getItem('horseJumpHighScore') || 0;
        let speedLevel = 1;
        let animationId;
        
        // 马对象
        const horse = {
            x: 100,
            y: canvas.height - 100,
            width: 60,
            height: 50,
            velocityY: 0,
            gravity: 0.5,
            jumpStrength: -12,
            isJumping: false,
            color: '#8B4513', // 马的颜色
            
            // 绘制马
            draw() {
                // 马的身体
                ctx.fillStyle = this.color;
                ctx.fillRect(this.x, this.y, this.width, this.height);
                
                // 马头
                ctx.fillStyle = '#A0522D';
                ctx.fillRect(this.x + this.width - 10, this.y - 20, 25, 30);
                
                // 马腿
                ctx.fillStyle = '#8B4513';
                ctx.fillRect(this.x + 10, this.y + this.height, 10, 25);
                ctx.fillRect(this.x + 40, this.y + this.height, 10, 25);
                
                // 马尾
                ctx.fillStyle = '#4A2C2A';
                ctx.fillRect(this.x - 10, this.y + 10, 15, 5);
                
                // 马眼睛
                ctx.fillStyle = 'white';
                ctx.fillRect(this.x + this.width + 5, this.y - 10, 8, 8);
                ctx.fillStyle = 'black';
                ctx.fillRect(this.x + this.width + 8, this.y - 7, 4, 4);
                
                // 马耳朵
                ctx.fillStyle = '#A0522D';
                ctx.beginPath();
                ctx.moveTo(this.x + this.width + 15, this.y - 20);
                ctx.lineTo(this.x + this.width + 25, this.y - 30);
                ctx.lineTo(this.x + this.width + 15, this.y - 25);
                ctx.fill();
            },
            
            // 跳跃
            jump() {
                if (!this.isJumping && gameRunning && !gamePaused) {
                    this.velocityY = this.jumpStrength;
                    this.isJumping = true;
                }
            },
            
            // 更新位置
            update() {
                // 应用重力
                this.velocityY += this.gravity;
                this.y += this.velocityY;
                
                // 确保马不会掉出地面
                if (this.y > canvas.height - 100) {
                    this.y = canvas.height - 100;
                    this.velocityY = 0;
                    this.isJumping = false;
                }
                
                // 确保马不会飞出顶部
                if (this.y < 0) {
                    this.y = 0;
                    this.velocityY = 0;
                }
            }
        };
        
        // 障碍物数组
        let obstacles = [];
        
        // 障碍物类
        class Obstacle {
            constructor(type) {
                this.type = type; // 'firecracker' 或 'lantern'
                this.x = canvas.width;
                this.width = type === 'firecracker' ? 30 : 40;
                this.height = type === 'firecracker' ? 50 : 60;
                this.y = canvas.height - 100 - this.height + (type === 'firecracker' ? 10 : 0);
                this.speed = 5 + (speedLevel * 0.5);
                this.color = type === 'firecracker' ? '#FF3333' : '#FFCC00';
            }
            
            // 绘制障碍物
            draw() {
                ctx.fillStyle = this.color;
                
                if (this.type === 'firecracker') {
                    // 绘制鞭炮
                    ctx.fillRect(this.x, this.y, this.width, this.height);
                    
                    // 鞭炮引线
                    ctx.fillStyle = '#888888';
                    ctx.fillRect(this.x + this.width/2 - 2, this.y - 10, 4, 10);
                    
                    // 鞭炮装饰
                    ctx.fillStyle = '#FF9900';
                    ctx.fillRect(this.x, this.y + 10, this.width, 5);
                    ctx.fillRect(this.x, this.y + 30, this.width, 5);
                } else {
                    // 绘制灯笼
                    ctx.beginPath();
                    ctx.ellipse(this.x + this.width/2, this.y + this.height/2, this.width/2, this.height/2, 0, 0, Math.PI * 2);
                    ctx.fill();
                    
                    // 灯笼顶部和底部
                    ctx.fillStyle = '#CC9900';
                    ctx.fillRect(this.x + 5, this.y - 5, this.width - 10, 5);
                    ctx.fillRect(this.x + 5, this.y + this.height, this.width - 10, 5);
                    
                    // 灯笼穗
                    ctx.fillStyle = '#FF9900';
                    for (let i = 0; i < 5; i++) {
                        ctx.fillRect(this.x + this.width/2 - 1, this.y + this.height + 5 + i*3, 2, 3);
                    }
                    
                    // 灯笼上的"福"字
                    ctx.fillStyle = 'red';
                    ctx.font = 'bold 20px Arial';
                    ctx.textAlign = 'center';
                    ctx.fillText('福', this.x + this.width/2, this.y + this.height/2 + 8);
                }
            }
            
            // 更新位置
            update() {
                this.x -= this.speed;
            }
            
            // 检查是否与马碰撞
            checkCollision() {
                return (
                    horse.x < this.x + this.width &&
                    horse.x + horse.width > this.x &&
                    horse.y < this.y + this.height &&
                    horse.y + horse.height > this.y
                );
            }
            
            // 检查是否已经通过
            isPassed() {
                return this.x + this.width < horse.x && !this.counted;
            }
        }
        
        // 初始化游戏
        function initGame() {
            score = 0;
            speedLevel = 1;
            obstacles = [];
            horse.x = 100;
            horse.y = canvas.height - 100;
            horse.velocityY = 0;
            horse.isJumping = false;
            
            updateScore();
            updateSpeedLevel();
            statusElement.textContent = '准备开始';
            gameOverScreen.style.display = 'none';
        }
        
        // 更新分数显示
        function updateScore() {
            scoreElement.textContent = score;
            
            if (score > highScore) {
                highScore = score;
                localStorage.setItem('horseJumpHighScore', highScore);
                highScoreElement.textContent = highScore;
            }
        }
        
        // 更新速度等级显示
        function updateSpeedLevel() {
            speedLevelElement.textContent = speedLevel;
        }
        
        // 生成障碍物
        function generateObstacle() {
            // 随机选择障碍物类型
            const types = ['firecracker', 'lantern'];
            const type = types[Math.floor(Math.random() * types.length)];
            
            // 控制障碍物之间的最小距离
            const minGap = 300;
            const lastObstacle = obstacles[obstacles.length - 1];
            
            if (!lastObstacle || lastObstacle.x < canvas.width - minGap) {
                obstacles.push(new Obstacle(type));
            }
        }
        
        // 绘制背景
        function drawBackground() {
            // 天空渐变
            const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
            gradient.addColorStop(0, '#0c2d5e');
            gradient.addColorStop(1, '#0a1f3d');
            ctx.fillStyle = gradient;
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            
            // 绘制星星
            ctx.fillStyle = 'white';
            for (let i = 0; i < 50; i++) {
                const x = Math.random() * canvas.width;
                const y = Math.random() * canvas.height * 0.7;
                const size = Math.random() * 2;
                ctx.fillRect(x, y, size, size);
            }
            
            // 绘制地面
            ctx.fillStyle = '#2d5016';
            ctx.fillRect(0, canvas.height - 100, canvas.width, 100);
            
            // 绘制草地纹理
            ctx.fillStyle = '#3a6b1f';
            for (let i = 0; i < canvas.width; i += 20) {
                ctx.fillRect(i, canvas.height - 100, 10, 10);
            }
            
            // 绘制新年横幅
            ctx.fillStyle = '#FF3333';
            ctx.fillRect(0, 20, canvas.width, 40);
            
            ctx.fillStyle = '#FFCC00';
            ctx.font = 'bold 28px Arial';
            ctx.textAlign = 'center';
            ctx.fillText('农历丙午马年 新春快乐', canvas.width / 2, 50);
        }
        
        // 游戏主循环
        function gameLoop() {
            // 清空画布
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            // 绘制背景
            drawBackground();
            
            // 更新和绘制马
            horse.update();
            horse.draw();
            
            // 生成障碍物
            if (Math.random() < 0.02 && gameRunning && !gamePaused) {
                generateObstacle();
            }
            
            // 更新和绘制障碍物
            for (let i = obstacles.length - 1; i >= 0; i--) {
                const obstacle = obstacles[i];
                
                if (gameRunning && !gamePaused) {
                    obstacle.update();
                }
                
                obstacle.draw();
                
                // 检查碰撞
                if (obstacle.checkCollision()) {
                    gameOver();
                    return;
                }
                
                // 检查是否通过障碍物
                if (obstacle.isPassed() && !obstacle.counted) {
                    score++;
                    obstacle.counted = true;
                    
                    // 每得10分提高速度等级
                    if (score % 10 === 0) {
                        speedLevel++;
                        updateSpeedLevel();
                    }
                    
                    updateScore();
                }
                
                // 移除屏幕外的障碍物
                if (obstacle.x + obstacle.width < 0) {
                    obstacles.splice(i, 1);
                }
            }
            
            // 如果游戏运行中,继续循环
            if (gameRunning && !gamePaused) {
                animationId = requestAnimationFrame(gameLoop);
            }
        }
        
        // 开始游戏
        function startGame() {
            if (!gameRunning) {
                gameRunning = true;
                gamePaused = false;
                statusElement.textContent = '游戏中';
                gameLoop();
            } else if (gamePaused) {
                gamePaused = false;
                statusElement.textContent = '游戏中';
                gameLoop();
            }
        }
        
        // 暂停游戏
        function pauseGame() {
            if (gameRunning && !gamePaused) {
                gamePaused = true;
                statusElement.textContent = '已暂停';
                cancelAnimationFrame(animationId);
            }
        }
        
        // 重置游戏
        function resetGame() {
            cancelAnimationFrame(animationId);
            initGame();
            gameRunning = false;
            gamePaused = false;
            
            // 重绘初始状态
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            drawBackground();
            horse.draw();
            statusElement.textContent = '准备开始';
        }
        
        // 游戏结束
        function gameOver() {
            gameRunning = false;
            statusElement.textContent = '游戏结束';
            finalScoreElement.textContent = score;
            gameOverScreen.style.display = 'flex';
        }
        
        // 事件监听
        document.getElementById('start-btn').addEventListener('click', startGame);
        document.getElementById('pause-btn').addEventListener('click', pauseGame);
        document.getElementById('reset-btn').addEventListener('click', resetGame);
        document.getElementById('restart-btn').addEventListener('click', () => {
            initGame();
            startGame();
        });
        
        document.getElementById('jump-btn').addEventListener('click', () => {
            horse.jump();
        });
        
        // 键盘控制
        document.addEventListener('keydown', (event) => {
            if (event.code === 'Space') {
                event.preventDefault(); // 防止空格键滚动页面
                horse.jump();
            }
            
            // 快捷键:S开始,P暂停,R重置
            if (event.key === 's' || event.key === 'S') {
                startGame();
            } else if (event.key === 'p' || event.key === 'P') {
                pauseGame();
            } else if (event.key === 'r' || event.key === 'R') {
                resetGame();
            }
        });
        
        // 初始化游戏
        window.onload = function() {
            highScoreElement.textContent = highScore;
            initGame();
            
            // 绘制初始画面
            drawBackground();
            horse.draw();
            
            // 绘制初始提示
            ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
            ctx.fillRect(canvas.width/2 - 150, canvas.height/2 - 50, 300, 100);
            
            ctx.fillStyle = '#FFCC00';
            ctx.font = 'bold 22px Arial';
            ctx.textAlign = 'center';
            ctx.fillText('点击"开始游戏"按钮', canvas.width/2, canvas.height/2 - 10);
            ctx.fillText('或按 S 键开始', canvas.width/2, canvas.height/2 + 20);
        };
    </script>
</body>
</html>
posted @ 2026-02-24 21:52  bz02_2023f2  阅读(2)  评论(0)    收藏  举报  来源