科学教育

搜索
查看: 620|回复: 1
打印 上一主题 下一主题

124

[复制链接]

9

主题

15

帖子

61

积分

注册会员

Rank: 2

积分
61
跳转到指定楼层
楼主
发表于 2026-1-6 09:39:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
修改代码让他不是乱码<!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>
        body {
            margin: 0;
            padding: 0;
            background-color: #f7f7f7;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            font-family: Arial, sans-serif;
        }

        #gameCanvas {
            border: 2px solid #333;
            background-color: #ffffff;
        }

        .game-info {
            position: absolute;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            font-size: 24px;
            font-weight: bold;
            color: #333;
        }

        .game-over {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 36px;
            font-weight: bold;
            color: #ff0000;
            display: none;
        }

        .restart-tip {
            position: absolute;
            top: 55%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 18px;
            color: #666;
            display: none;
        }
    </style>
</head>
<body>
    <div class="game-info">分数: <span id="score">0</span></div>
    <div class="game-over" id="gameOver">游戏结束!</div>
    <div class="restart-tip" id="restartTip">按空格键重新开始</div>
    <canvas id="gameCanvas" width="800" height="200"></canvas>

    <script>
        // 获取画布和上下文
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        const scoreElement = document.getElementById('score');
        const gameOverElement = document.getElementById('gameOver');
        const restartTipElement = document.getElementById('restartTip');

        // 游戏参数
        let score = 0;
        let gameRunning = true;

        // 恐龙对象
        const dino = {
            x: 50,
            y: 150,
            width: 40,
            height: 40,
            velocityY: 0,
            jumpPower: -15,
            gravity: 0.8,
            onGround: true
        };

        // 障碍物数组
        const obstacles = [];
        const obstacleWidth = 20;
        const obstacleHeight = 40;
        let obstacleSpawnTimer = 0;
        const obstacleSpawnRate = 120; // 生成障碍物的帧率间隔

        // 地面参数
        const groundY = 190;
        let groundX = 0;
        const groundSpeed = 5;

        // 初始化
        function init() {
            score = 0;
            gameRunning = true;
            obstacles.length = 0;
            dino.y = 150;
            dino.velocityY = 0;
            dino.onGround = true;
            gameOverElement.style.display = 'none';
            restartTipElement.style.display = 'none';
            scoreElement.textContent = score;
        }

        // 绘制恐龙
        function drawDino() {
            ctx.fillStyle = '#333';
            ctx.fillRect(dino.x, dino.y, dino.width, dino.height);
            // 简化的恐龙头部
            ctx.beginPath();
            ctx.arc(dino.x + 35, dino.y + 15, 10, 0, Math.PI * 2);
            ctx.fill();
        }

        // 绘制障碍物
        function drawObstacles() {
            ctx.fillStyle = '#2ecc71';
            obstacles.forEach(obstacle => {
                ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
            });
        }

        // 绘制地面
        function drawGround() {
            ctx.fillStyle = '#95a5a6';
            ctx.fillRect(groundX, groundY, canvas.width * 2, 10);
        }

        // 更新恐龙状态(跳跃/重力)
        function updateDino() {
            if (!dino.onGround) {
                dino.velocityY += dino.gravity;
                dino.y += dino.velocityY;

                // 落地检测
                if (dino.y >= 150) {
                    dino.y = 150;
                    dino.velocityY = 0;
                    dino.onGround = true;
                }
            }
        }

        // 更新障碍物
        function updateObstacles() {
            if (!gameRunning) return;

            // 生成新障碍物
            obstacleSpawnTimer++;
            if (obstacleSpawnTimer >= obstacleSpawnRate) {
                obstacles.push({
                    x: canvas.width,
                    y: groundY - obstacleHeight,
                    width: obstacleWidth,
                    height: obstacleHeight
                });
                obstacleSpawnTimer = 0;
                // 随着分数增加,提高生成频率
                if (obstacleSpawnRate > 60) {
                    obstacleSpawnRate -= 0.5;
                }
            }

            // 移动障碍物
            obstacles.forEach((obstacle, index) => {
                obstacle.x -= groundSpeed;
                // 移除超出画布的障碍物
                if (obstacle.x + obstacle.width < 0) {
                    obstacles.splice(index, 1);
                    // 计分
                    score++;
                    scoreElement.textContent = score;
                }

                // 碰撞检测
                if (
                    dino.x < obstacle.x + obstacle.width &&
                    dino.x + dino.width > obstacle.x &&
                    dino.y < obstacle.y + obstacle.height &&
                    dino.y + dino.height > obstacle.y
                ) {
                    gameRunning = false;
                    gameOverElement.style.display = 'block';
                    restartTipElement.style.display = 'block';
                }
            });
        }

        // 更新地面
        function updateGround() {
            groundX -= groundSpeed;
            if (groundX <= -canvas.width) {
                groundX = 0;
            }
        }

        // 游戏主循环
        function gameLoop() {
            // 清空画布
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            if (gameRunning) {
                updateDino();
                updateObstacles();
                updateGround();
            }

            drawGround();
            drawDino();
            drawObstacles();

            requestAnimationFrame(gameLoop);
        }

        // 监听空格键跳跃/重启
        document.addEventListener('keydown', (e) => {
            if (e.code === 'Space') {
                if (gameRunning && dino.onGround) {
                    // 跳跃
                    dino.velocityY = dino.jumpPower;
                    dino.onGround = false;
                } else if (!gameRunning) {
                    // 重启游戏
                    init();
                }
            }
        });

        // 初始化并启动游戏
        init();
        gameLoop();
    </script>
</body>
</html>

回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2026-1-6 09:39:27 | 只看该作者
你好!你这段代码看起来是一个简单的“小恐龙跑酷游戏”,但是乱码主要是因为代码没有写完整,还有一些语法错误和缺少必要的HTML结构。

我帮你整理了一份完整且能正常运行的代码,你可以复制下面的内容保存成 `.html` 文件,然后用浏览器打开看看效果哦!

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.     <meta charset="UTF-8" />
  5.     <title>小恐龙跑酷游戏</title>
  6.     <style>
  7.         body {
  8.             margin: 0;
  9.             padding: 0;
  10.             background-color: #f7f7f7;
  11.             display: flex;
  12.             justify-content: center;
  13.             align-items: center;
  14.             min-height: 100vh;
  15.             font-family: Arial, sans-serif;
  16.             position: relative;
  17.         }

  18.         #gameCanvas {
  19.             border: 2px solid #333;
  20.             background-color: #ffffff;
  21.         }

  22.         .game-info {
  23.             position: absolute;
  24.             top: 20px;
  25.             left: 50%;
  26.             transform: translateX(-50%);
  27.             font-size: 24px;
  28.             font-weight: bold;
  29.             color: #333;
  30.         }

  31.         .game-over {
  32.             position: absolute;
  33.             top: 50%;
  34.             left: 50%;
  35.             transform: translate(-50%, -50%);
  36.             font-size: 36px;
  37.             font-weight: bold;
  38.             color: #ff0000;
  39.             display: none;
  40.         }

  41.         .restart-tip {
  42.             position: absolute;
  43.             top: 55%;
  44.             left: 50%;
  45.             transform: translate(-50%, -50%);
  46.             font-size: 18px;
  47.             color: #666;
  48.             display: none;
  49.         }
  50.     </style>
  51. </head>
  52. <body>

  53. <div class="game-info">分数: <span id="score">0</span></div>
  54. <div class="game-over" id="gameOver">游戏结束!</div>
  55. <div class="restart-tip" id="restartTip">按空格键重新开始</div>
  56. <canvas id="gameCanvas" width="600" height="220"></canvas>

  57. <script>
  58.     // 获取画布和上下文
  59.     const canvas = document.getElementById('gameCanvas');
  60.     const ctx = canvas.getContext('2d');
  61.     const scoreElement = document.getElementById('score');
  62.     const gameOverElement = document.getElementById('gameOver');
  63.     const restartTipElement = document.getElementById('restartTip');

  64.     // 游戏参数
  65.     let score = 0;
  66.     let gameRunning = true;

  67.     // 恐龙对象
  68.     const dino = {
  69.         x: 50,
  70.         y: 150,
  71.         width: 40,
  72.         height: 40,
  73.         velocityY: 0,
  74.         jumpPower: -15,
  75.         gravity: 0.8,
  76.         onGround: true
  77.     };

  78.     // 障碍物数组
  79.     const obstacles = [];
  80.     const obstacleWidth = 20;
  81.     const obstacleHeight = 40;
  82.     let obstacleSpawnTimer = 0;
  83.     let obstacleSpawnRate = 120; // 生成障碍物的帧率间隔

  84.     // 地面参数
  85.     const groundY = 190;
  86.     let groundX = 0;
  87.     const groundSpeed = 5;

  88.     // 初始化游戏数据
  89.     function init() {
  90.         score = 0;
  91.         gameRunning = true;
  92.         obstacles.length = 0;
  93.         dino.y = 150;
  94.         dino.velocityY = 0;
  95.         dino.onGround = true;
  96.         gameOverElement.style.display = 'none';
  97.         restartTipElement.style.display = 'none';
  98.         scoreElement.textContent = score;
  99.         obstacleSpawnRate = 120;
  100.         groundX = 0;
  101.     }

  102.     // 绘制恐龙
  103.     function drawDino() {
  104.         ctx.fillStyle = '#333';
  105.         ctx.fillRect(dino.x, dino.y, dino.width, dino.height);
  106.         // 简单画头部圆形
  107.         ctx.beginPath();
  108.         ctx.arc(dino.x + 35, dino.y + 15, 10, 0, Math.PI * 2);
  109.         ctx.fill();
  110.     }

  111.     // 绘制障碍物
  112.     function drawObstacles() {
  113.         ctx.fillStyle = '#2ecc71';
  114.         obstacles.forEach(obstacle => {
  115.             ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
  116.         });
  117.     }

  118.     // 绘制地面
  119.     function drawGround() {
  120.         ctx.fillStyle = '#95a5a6';
  121.         ctx.fillRect(groundX, groundY, canvas.width * 2, 10);
  122.     }

  123.     // 更新恐龙状态(跳跃和重力)
  124.     function updateDino() {
  125.         if (!dino.onGround) {
  126.             dino.velocityY += dino.gravity;
  127.             dino.y += dino.velocityY;

  128.             // 落地检测
  129.             if (dino.y >= 150) {
  130.                 dino.y = 150;
  131.                 dino.velocityY = 0;
  132.                 dino.onGround = true;
  133.             }
  134.         }
  135.     }

  136.     // 更新障碍物位置和生成新的障碍物
  137.     function updateObstacles() {
  138.         if (!gameRunning) return;

  139.         obstacleSpawnTimer++;
  140.         if (obstacleSpawnTimer >= obstacleSpawnRate) {
  141.             obstacles.push({
  142.                 x: canvas.width,
  143.                 y: groundY - obstacleHeight,
  144.                 width: obstacleWidth,
  145.                 height: obstacleHeight
  146.             });
  147.             obstacleSpawnTimer = 0;

  148.             // 随着分数增加,障碍物生成速度提升,最少不低于60帧
  149.             if (obstacleSpawnRate > 60) {
  150.                 obstacleSpawnRate -= 0.5;
  151.             }
  152.         }

  153.         // 移动障碍物及碰撞检测
  154.         obstacles.forEach((obstacle, index) => {
  155.             obstacle.x -= groundSpeed;

  156.             // 移除消失的障碍物,计分
  157.             if (obstacle.x + obstacle.width < 0) {
  158.                 obstacles.splice(index, 1);
  159.                 if(gameRunning) { // 确保游戏没结束时才加分
  160.                     score++;
  161.                     scoreElement.textContent = score;
  162.                 }
  163.             }

  164.             // 碰撞检测
  165.             if (
  166.                 dino.x < obstacle.x + obstacle.width &&
  167.                 dino.x + dino.width > obstacle.x &&
  168.                 dino.y < obstacle.y + obstacle.height &&
  169.                 dino.y + dino.height > obstacle.y
  170.             ) {
  171.                 gameRunning = false;
  172.                 gameOverElement.style.display = 'block';
  173.                 restartTipElement.style.display = 'block';
  174.             }
  175.         });
  176.     }

  177.     // 更新地面滚动
  178.     function updateGround() {
  179.         groundX -= groundSpeed;
  180.         if (groundX <= -canvas.width) {
  181.             groundX = 0;
  182.         }
  183.     }

  184.     // 清空画布并重绘所有元素
  185.     function draw() {
  186.         ctx.clearRect(0, 0, canvas.width, canvas.height);
  187.         drawGround();
  188.         drawDino();
  189.         drawObstacles();
  190.     }

  191.     // 游戏主循环
  192.     function gameLoop() {
  193.         if (gameRunning) {
  194.             updateDino();
  195.             updateObstacles();
  196.             updateGround();
  197.         }
  198.         draw();
  199.         requestAnimationFrame(gameLoop);
  200.     }

  201.     // 监听键盘事件(空格跳跃或重新开始)
  202.     window.addEventListener('keydown', e => {
  203.         if (e.code === 'Space') {
  204.             if (gameRunning && dino.onGround) {
  205.                 // 恐龙跳跃
  206.                 dino.velocityY = dino.jumpPower;
  207.                 dino.onGround = false;
  208.             } else if (!gameRunning) {
  209.                 // 重新开始游戏
  210.                 init();
  211.             }
  212.         }
  213.     });

  214.     // 启动游戏
  215.     init();
  216.     gameLoop();
  217. </script>

  218. </body>
  219. </html>
复制代码


---

### 这个代码有什么作用?

- **HTML部分**:画了一个画布(`<canvas>`)和显示分数、游戏结束信息的区域。
- **CSS部分**:让页面变得美观、居中,并给画布加边框和颜色。
- **JavaScript部分**:
  - 定义恐龙和障碍物的属性。
  - 通过动画循环不停地更新恐龙位置和障碍物移动。
  - 监听空格键控制恐龙跳跃。
  - 碰到障碍物后显示游戏结束,并提示按空格重新开始。

---

### 你可以试试做什么:

- 在浏览器里打开这个网页,用空格键让小恐龙跳跃。
- 尝试修改 `jumpPower` 或 `gravity`,看小恐龙跳的高度和速度变化。
- 改变障碍物的颜色或大小,自己做更多有趣的小改动。

如果你还是不太明白,可以随时告诉我,我来帮你一步步解释哦!????
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表