科学教育

搜索
查看: 2165|回复: 3
打印 上一主题 下一主题

陈丁赫

[复制链接]

1

主题

4

帖子

31

积分

新手上路

Rank: 1

积分
31
跳转到指定楼层
楼主
发表于 2024-7-14 10:05:39 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
你好,你是一名专业的代码设计师,请你用html、css、js生成一个贪吃蛇游戏。
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
地板
发表于 2024-7-14 10:21:51 | 只看该作者
陈丁赫 发表于 2024-07-14 10:21
那你可以把html、css、js分三部分,再生成一个贪吃蛇 ...


当然可以!以下是将HTML、CSS和JavaScript代码分离后的贪吃蛇游戏。

### HTML (`index.html`)
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>贪吃蛇游戏</title>
  7.     <link rel="stylesheet" href="styles.css">
  8. </head>
  9. <body>
  10.     <canvas id="gameCanvas" width="400" height="400"></canvas>
  11.     <script src="script.js"></script>
  12. </body>
  13. </html>
复制代码


### CSS (`styles.css`)
  1. body {
  2.     display: flex;
  3.     justify-content: center;
  4.     align-items: center;
  5.     height: 100vh;
  6.     margin: 0;
  7.     background-color: #f0f0f0;
  8. }

  9. canvas {
  10.     border: 1px solid black;
  11. }
复制代码


### JavaScript (`script.js`)
  1. const canvas = document.getElementById("gameCanvas");
  2. const ctx = canvas.getContext("2d");

  3. const gridSize = 20;
  4. const tileCount = canvas.width / gridSize;

  5. let snake = [{ x: 10, y: 10 }];
  6. let direction = { x: 1, y: 0 };
  7. let food = { x: 5, y: 5 };
  8. let gameOver = false;

  9. function drawTile(x, y, color) {
  10.     ctx.fillStyle = color;
  11.     ctx.fillRect(x * gridSize, y * gridSize, gridSize, gridSize);
  12. }

  13. function update() {
  14.     if (gameOver) return;

  15.     const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };

  16.     // 检查是否撞墙或撞自己
  17.     if (
  18.         head.x < 0 ||
  19.         head.x >= tileCount ||
  20.         head.y < 0 ||
  21.         head.y >= tileCount ||
  22.         snake.some(segment => segment.x === head.x && segment.y === head.y)
  23.     ) {
  24.         gameOver = true;
  25.         alert("游戏结束!");
  26.         return;
  27.     }

  28.     snake.unshift(head);

  29.     // 检查是否吃到食物
  30.     if (head.x === food.x && head.y === food.y) {
  31.         placeFood();
  32.     } else {
  33.         snake.pop();
  34.     }
  35. }

  36. function draw() {
  37.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  38.     snake.forEach(segment => drawTile(segment.x, segment.y, "green"));
  39.     drawTile(food.x, food.y, "red");
  40. }

  41. function placeFood() {
  42.     let newFoodPosition;
  43.     while (true) {
  44.         newFoodPosition = {
  45.             x: Math.floor(Math.random() * tileCount),
  46.             y: Math.floor(Math.random() * tileCount)
  47.         };
  48.         if (!snake.some(segment => segment.x === newFoodPosition.x && segment.y === newFoodPosition.y)) {
  49.             break;
  50.         }
  51.     }
  52.     food = newFoodPosition;
  53. }

  54. function changeDirection(event) {
  55.     switch (event.key) {
  56.         case "ArrowUp":
  57.             if (direction.y === 0) direction = { x: 0, y: -1 };
  58.             break;
  59.         case "ArrowDown":
  60.             if (direction.y === 0) direction = { x: 0, y: 1 };
  61.             break;
  62.         case "ArrowLeft":
  63.             if (direction.x === 0) direction = { x: -1, y: 0 };
  64.             break;
  65.         case "ArrowRight":
  66.             if (direction.x === 0) direction = { x: 1, y: 0 };
  67.             break;
  68.     }
  69. }

  70. document.addEventListener("keydown", changeDirection);

  71. function gameLoop() {
  72.     update();
  73.     draw();
  74.     setTimeout(gameLoop, 100);
  75. }

  76. placeFood();
  77. gameLoop();
复制代码


将这些代码分别保存到不同的文件中(`index.html`, `styles.css`, `script.js`),然后在浏览器中打开`index.html`即可运行贪吃蛇游戏。如果有需要进一步改进或增加功能,请告诉我!
回复 支持 反对

使用道具 举报

1

主题

4

帖子

31

积分

新手上路

Rank: 1

积分
31
板凳
 楼主| 发表于 2024-7-14 10:21:37 | 只看该作者
那你可以把html、css、js分三部分,再生成一个贪吃蛇游戏吗?
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2024-7-14 10:05:56 | 只看该作者
当然可以!以下是一个简单的贪吃蛇游戏的实现,使用了HTML、CSS和JavaScript。这个示例包含一个HTML文件,其中嵌入了CSS和JavaScript代码。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>贪吃蛇游戏</title>
  7.     <style>
  8.         body {
  9.             display: flex;
  10.             justify-content: center;
  11.             align-items: center;
  12.             height: 100vh;
  13.             margin: 0;
  14.             background-color: #f0f0f0;
  15.         }

  16.         canvas {
  17.             border: 1px solid black;
  18.         }
  19.     </style>
  20. </head>
  21. <body>
  22.     <canvas id="gameCanvas" width="400" height="400"></canvas>
  23.     <script>
  24.         const canvas = document.getElementById("gameCanvas");
  25.         const ctx = canvas.getContext("2d");

  26.         const gridSize = 20;
  27.         const tileCount = canvas.width / gridSize;

  28.         let snake = [{ x: 10, y: 10 }];
  29.         let direction = { x: 1, y: 0 };
  30.         let food = { x: 5, y: 5 };
  31.         let gameOver = false;

  32.         function drawTile(x, y, color) {
  33.             ctx.fillStyle = color;
  34.             ctx.fillRect(x * gridSize, y * gridSize, gridSize, gridSize);
  35.         }

  36.         function update() {
  37.             if (gameOver) return;

  38.             const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };

  39.             // 检查是否撞墙或撞自己
  40.             if (
  41.                 head.x < 0 ||
  42.                 head.x >= tileCount ||
  43.                 head.y < 0 ||
  44.                 head.y >= tileCount ||
  45.                 snake.some(segment => segment.x === head.x && segment.y === head.y)
  46.             ) {
  47.                 gameOver = true;
  48.                 alert("游戏结束!");
  49.                 return;
  50.             }

  51.             snake.unshift(head);

  52.             // 检查是否吃到食物
  53.             if (head.x === food.x && head.y === food.y) {
  54.                 placeFood();
  55.             } else {
  56.                 snake.pop();
  57.             }
  58.         }

  59.         function draw() {
  60.             ctx.clearRect(0, 0, canvas.width, canvas.height);
  61.             snake.forEach(segment => drawTile(segment.x, segment.y, "green"));
  62.             drawTile(food.x, food.y, "red");
  63.         }

  64.         function placeFood() {
  65.             let newFoodPosition;
  66.             while (true) {
  67.                 newFoodPosition = {
  68.                     x: Math.floor(Math.random() * tileCount),
  69.                     y: Math.floor(Math.random() * tileCount)
  70.                 };
  71.                 if (!snake.some(segment => segment.x === newFoodPosition.x && segment.y === newFoodPosition.y)) {
  72.                     break;
  73.                 }
  74.             }
  75.             food = newFoodPosition;
  76.         }

  77.         function changeDirection(event) {
  78.             switch (event.key) {
  79.                 case "ArrowUp":
  80.                     if (direction.y === 0) direction = { x: 0, y: -1 };
  81.                     break;
  82.                 case "ArrowDown":
  83.                     if (direction.y === 0) direction = { x: 0, y: 1 };
  84.                     break;
  85.                 case "ArrowLeft":
  86.                     if (direction.x === 0) direction = { x: -1, y: 0 };
  87.                     break;
  88.                 case "ArrowRight":
  89.                     if (direction.x === 0) direction = { x: 1, y: 0 };
  90.                     break;
  91.             }
  92.         }

  93.         document.addEventListener("keydown", changeDirection);

  94.         function gameLoop() {
  95.             update();
  96.             draw();
  97.             setTimeout(gameLoop, 100);
  98.         }

  99.         placeFood();
  100.         gameLoop();
  101.     </script>
  102. </body>
  103. </html>
复制代码


这个简化版的贪吃蛇游戏包含了基本的移动、吃食物以及碰撞检测功能。你可以在浏览器中直接运行这个HTML文件来玩贪吃蛇游戏。如果你有任何其他要求或需要进一步改进,可以告诉我!
回复 支持 反对

使用道具 举报

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

本版积分规则

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