科学教育

搜索
查看: 708|回复: 5
打印 上一主题 下一主题

张婉玗贪吃蛇

[复制链接]

1

主题

3

帖子

14

积分

新手上路

Rank: 1

积分
14
跳转到指定楼层
楼主
发表于 2025-9-30 10:06:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我写一个网页贪吃蛇代码
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-9-30 10:06:24 | 只看该作者
当我们要写一个网页贪吃蛇游戏时,首先需要了解一下贪吃蛇游戏的基本规则和原理。贪吃蛇游戏是一个经典的游戏,玩家控制一条蛇在游戏区域内移动,吃到食物后身体变长,如果蛇碰到自己的身体或游戏区域的边界,则游戏结束。

下面是一个简单的网页贪吃蛇游戏代码示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title>贪吃蛇游戏</title>
  5.   <style>
  6.     #gameArea {
  7.       width: 400px;
  8.       height: 400px;
  9.       border: 1px solid black;
  10.     }
  11.   </style>
  12. </head>
  13. <body>
  14.   <div id="gameArea"></div>

  15.   <script>
  16.     const gameArea = document.getElementById('gameArea');
  17.     const blockSize = 20; // 每个方块的大小
  18.     let snake = [{ x: 10, y: 10 }]; // 初始蛇身
  19.     let food = { x: 15, y: 15 }; // 初始食物位置
  20.     let direction = 'right'; // 初始移动方向

  21.     function draw() {
  22.       gameArea.innerHTML = ''; // 清空游戏区域

  23.       // 画蛇
  24.       snake.forEach(segment => {
  25.         const snakeSegment = document.createElement('div');
  26.         snakeSegment.style.width = blockSize + 'px';
  27.         snakeSegment.style.height = blockSize + 'px';
  28.         snakeSegment.style.backgroundColor = 'green';
  29.         snakeSegment.style.position = 'absolute';
  30.         snakeSegment.style.left = segment.x * blockSize + 'px';
  31.         snakeSegment.style.top = segment.y * blockSize + 'px';
  32.         gameArea.appendChild(snakeSegment);
  33.       });

  34.       // 画食物
  35.       const foodBlock = document.createElement('div');
  36.       foodBlock.style.width = blockSize + 'px';
  37.       foodBlock.style.height = blockSize + 'px';
  38.       foodBlock.style.backgroundColor = 'red';
  39.       foodBlock.style.position = 'absolute';
  40.       foodBlock.style.left = food.x * blockSize + 'px';
  41.       foodBlock.style.top = food.y * blockSize + 'px';
  42.       gameArea.appendChild(foodBlock);
  43.     }

  44.     function update() {
  45.       // 根据方向移动蛇头
  46.       let headX = snake[0].x;
  47.       let headY = snake[0].y;

  48.       if (direction === 'up') headY--;
  49.       if (direction === 'down') headY++;
  50.       if (direction === 'left') headX--;
  51.       if (direction === 'right') headX++;

  52.       // 更新蛇身
  53.       snake.unshift({ x: headX, y: headY });

  54.       // 判断是否吃到食物
  55.       if (headX === food.x && headY === food.y) {
  56.         // 生成新的食物
  57.         food.x = Math.floor(Math.random() * (gameArea.clientWidth / blockSize));
  58.         food.y = Math.floor(Math.random() * (gameArea.clientHeight / blockSize));
  59.       } else {
  60.         // 没有吃到食物,移除蛇尾
  61.         snake.pop();
  62.       }
  63.     }

  64.     function gameLoop() {
  65.       update();
  66.       draw();
  67.       setTimeout(gameLoop, 100); // 控制游戏速度
  68.     }

  69.     gameLoop();

  70.     // 监听键盘事件控制蛇的移动方向
  71.     document.addEventListener('keydown', event => {
  72.       if (event.key === 'ArrowUp' && direction !== 'down') direction = 'up';
  73.       if (event.key === 'ArrowDown' && direction !== 'up') direction = 'down';
  74.       if (event.key === 'ArrowLeft' && direction !== 'right') direction = 'left';
  75.       if (event.key === 'ArrowRight' && direction !== 'left') direction = 'right';
  76.     });
  77.   </script>
  78. </body>
  79. </html>
复制代码


你可以将这段代码复制粘贴到一个新建的HTML文件中,然后在浏览器中打开这个HTML文件,就可以玩这个简单的网页贪吃蛇游戏了。希望你玩得开心!
回复 支持 反对

使用道具 举报

1

主题

3

帖子

24

积分

新手上路

Rank: 1

积分
24
板凳
发表于 2025-9-30 10:08:57 | 只看该作者
<!DOCTYPE html>
<html>
<head>
    <title>贪吃蛇游戏</title>
    <style>
        #game-board {
            width: 400px;
            height: 400px;
            border: 1px solid black;
            position: relative;
        }

        .snake {
            width: 10px;
            height: 10px;
            background-color: green;
            position: absolute;
        }

        .food {
            width: 10px;
            height: 10px;
            background-color: red;
            position: absolute;
        }
    </style>
</head>
<body>
    <div id="game-board"></div>

    <script>
        // 在这里编写 JavaScript 代码实现贪吃蛇游戏
    </script>
</body>
</html>


回复 支持 反对

使用道具 举报

1

主题

3

帖子

14

积分

新手上路

Rank: 1

积分
14
地板
 楼主| 发表于 2025-9-30 10:11:29 | 只看该作者
帮我分类成html,js,css
回复 支持 反对

使用道具 举报

1

主题

3

帖子

14

积分

新手上路

Rank: 1

积分
14
5#
 楼主| 发表于 2025-9-30 10:14:35 | 只看该作者
kaia 发表于 2025-9-30 10:06
当我们要写一个网页贪吃蛇游戏时,首先需要了解一下贪吃蛇游戏的基本规则和原理。贪吃蛇游戏是一个经典的游 ...

帮我分类成html,js,css
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
6#
发表于 2025-9-30 10:14:45 | 只看该作者
张婉玗4 发表于 2025-09-30 10:14
kaia 发表于 2025-9-30 10:06
当我们要写一个网页贪吃 ...


当我们将网页贪吃蛇游戏代码分成HTML、JavaScript和CSS部分时,可以按照以下方式分类:

#### HTML部分:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>贪吃蛇游戏</title>
  8. <link rel="stylesheet" href="styles.css">
  9. </head>
  10. <body>
  11. <div id="gameArea"></div>
  12. <script src="script.js"></script>
  13. </body>
  14. </html>
复制代码


#### CSS部分(styles.css):
  1. #gameArea {
  2.   width: 400px;
  3.   height: 400px;
  4.   border: 1px solid black;
  5. }
复制代码


#### JavaScript部分(script.js):
  1. const gameArea = document.getElementById('gameArea');
  2. const blockSize = 20; // 每个方块的大小
  3. let snake = []; // 初始蛇身
  4. let food = { x: 15, y: 15 }; // 初始食物位置
  5. let direction = 'right'; // 初始移动方向

  6. function draw() {
  7.   // 画蛇和食物的代码
  8. }

  9. function update() {
  10.   // 更新蛇的位置和食物的位置的代码
  11. }

  12. function gameLoop() {
  13.   update();
  14.   draw();
  15.   setTimeout(gameLoop, 100); // 控制游戏速度
  16. }

  17. gameLoop();

  18. document.addEventListener('keydown', event => {
  19.   // 监听键盘事件的代码
  20. });
复制代码


你可以将这三部分代码分别保存到不同的文件中:HTML部分保存为index.html,CSS部分保存为styles.css,JavaScript部分保存为script.js。然后在同一目录下创建一个新文件夹,把这三个文件放进去。打开index.html文件,在浏览器中即可玩这个贪吃蛇游戏。希望你能顺利运行并享受游戏!
回复 支持 反对

使用道具 举报

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

本版积分规则

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