科学教育

搜索
查看: 2604|回复: 7
打印 上一主题 下一主题

林奕锦-

[复制链接]

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
跳转到指定楼层
楼主
发表于 2024-7-12 10:46:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
请你帮我设计一个网页贪吃蛇游戏,有html、css、js文件,并且有丰富的背景图片和食物,还有开始按钮和结束提示,以及生动的蛇头和蛇身
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2024-7-12 10:47:17 | 只看该作者
当然可以!以下是一个网页贪吃蛇游戏的基本结构实现,包括HTML、CSS和JS文件。这个实现包含丰富的背景图片和食物,开始按钮和结束提示,以及生动的蛇头和蛇身。

### 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="style.css">
  8. </head>
  9. <body>
  10.     <div id="game-container">
  11.         <canvas id="game-board"></canvas>
  12.         <div id="controls">
  13.             <button id="start-button">开始游戏</button>
  14.             <div id="game-over" class="hidden">游戏结束</div>
  15.         </div>
  16.     </div>
  17.     <script src="script.js"></script>
  18. </body>
  19. </html>
复制代码


### CSS (`style.css`)

  1. body {
  2.     display: flex;
  3.     justify-content: center;
  4.     align-items: center;
  5.     height: 100vh;
  6.     margin: 0;
  7.     background-image: url('background.jpg');
  8.     background-size: cover;
  9. }

  10. #game-container {
  11.     display: flex;
  12.     flex-direction: column;
  13.     align-items: center;
  14. }

  15. #game-board {
  16.     border: 1px solid #000;
  17.     background-color: rgba(255, 255, 255, 0.6);
  18. }

  19. #controls {
  20.     margin-top: 20px;
  21.     text-align: center;
  22. }

  23. button {
  24.     padding: 10px 20px;
  25.     font-size: 16px;
  26.     cursor: pointer;
  27. }

  28. .hidden {
  29.     display: none;
  30. }

  31. #game-over {
  32.     font-size: 24px;
  33.     color: red;
  34.     margin-top: 20px;
  35. }
复制代码


### JavaScript (`script.js`)

  1. const canvas = document.getElementById('game-board');
  2. const context = canvas.getContext('2d');
  3. const startButton = document.getElementById('start-button');
  4. const gameOverText = document.getElementById('game-over');

  5. canvas.width = 400;
  6. canvas.height = 400;

  7. const cellSize = 20;
  8. let snake = [{ x: 200, y: 200 }];
  9. let direction = { x: 0, y: 0 };
  10. let food = {};
  11. let gameInterval;

  12. function drawBackground() {
  13.     context.fillStyle = 'rgba(0, 128, 128, 0.5)';
  14.     context.fillRect(0, 0, canvas.width, canvas.height);
  15. }

  16. function drawSnake() {
  17.     context.fillStyle = 'green';
  18.     context.fillRect(snake[0].x, snake[0].y, cellSize, cellSize);
  19.     context.fillStyle = 'lime';
  20.     for (let i = 1; i < snake.length; i++) {
  21.         context.fillRect(snake[i].x, snake[i].y, cellSize, cellSize);
  22.     }
  23. }

  24. function spawnFood() {
  25.     food.x = Math.floor(Math.random() * canvas.width / cellSize) * cellSize;
  26.     food.y = Math.floor(Math.random() * canvas.height / cellSize) * cellSize;
  27. }

  28. function drawFood() {
  29.     context.fillStyle = 'red';
  30.     context.fillRect(food.x, food.y, cellSize, cellSize);
  31. }

  32. function updateSnake() {
  33.     const head = { x: snake[0].x + direction.x * cellSize, y: snake[0].y + direction.y * cellSize };
  34.     snake.unshift(head);

  35.     if (head.x === food.x && head.y === food.y) {
  36.         spawnFood();
  37.     } else {
  38.         snake.pop();
  39.     }

  40.     if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height || snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y)) {
  41.         gameOver();
  42.     }
  43. }

  44. function gameOver() {
  45.     clearInterval(gameInterval);
  46.     gameOverText.classList.remove('hidden');
  47. }

  48. function gameLoop() {
  49.     drawBackground();
  50.     drawSnake();
  51.     drawFood();
  52.     updateSnake();
  53. }

  54. document.addEventListener('keydown', event => {
  55.     if (event.key === 'ArrowUp' && direction.y === 0) {
  56.         direction = { x: 0, y: -1 };
  57.     } else if (event.key === 'ArrowDown' && direction.y === 0) {
  58.         direction = { x: 0, y: 1 };
  59.     } else if (event.key === 'ArrowLeft' && direction.x === 0) {
  60.         direction = { x: -1, y: 0 };
  61.     } else if (event.key === 'ArrowRight' && direction.x === 0) {
  62.         direction = { x: 1, y: 0 };
  63.     }
  64. });

  65. startButton.addEventListener('click', () => {
  66.     snake = [{ x: 200, y: 200 }];
  67.     direction = { x: 0, y: 0 };
  68.     spawnFood();
  69.     gameOverText.classList.add('hidden');
  70.     gameInterval = setInterval(gameLoop, 100);
  71. });
复制代码


### 补充资源

1. **背景图片:** `background.jpg`
2. **蛇头和身体图片:** 可以用 CSS 渲染或者图片替代,目前使用颜色填充。

将上述代码整合到对应的文件中,并确保 `background.jpg` 在同一目录下。打开 `index.html` 就可以看到贪吃蛇游戏了。享受游戏吧!如果你有任何进一步的需求或问题,请随时告诉我!
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
板凳
 楼主| 发表于 2024-7-12 10:49:29 | 只看该作者
kaia 发表于 2024-7-12 10:47
当然可以!以下是一个网页贪吃蛇游戏的基本结构实现,包括HTML、CSS和JS文件。这个实现包含丰富的背景图片 ...

能不能再加上生动的蛇头和蛇身
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
地板
发表于 2024-7-12 10:50:04 | 只看该作者
林奕锦 发表于 2024-07-12 10:49
能不能再加上生动的蛇头和蛇身


当然可以!我们可以用图片来代替蛇头和蛇身的简单颜色填充,以实现更生动的效果。下面是修改后的实现:

### 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="style.css">
  8. </head>
  9. <body>
  10.     <div id="game-container">
  11.         <canvas id="game-board"></canvas>
  12.         <div id="controls">
  13.             <button id="start-button">开始游戏</button>
  14.             <div id="game-over" class="hidden">游戏结束</div>
  15.         </div>
  16.     </div>
  17.     <script src="script.js"></script>
  18. </body>
  19. </html>
复制代码


### CSS (`style.css`)

  1. body {
  2.     display: flex;
  3.     justify-content: center;
  4.     align-items: center;
  5.     height: 100vh;
  6.     margin: 0;
  7.     background-image: url('background.jpg');
  8.     background-size: cover;
  9. }

  10. #game-container {
  11.     display: flex;
  12.     flex-direction: column;
  13.     align-items: center;
  14. }

  15. #game-board {
  16.     border: 1px solid #000;
  17.     background-color: rgba(255, 255, 255, 0.6);
  18. }

  19. #controls {
  20.     margin-top: 20px;
  21.     text-align: center;
  22. }

  23. button {
  24.     padding: 10px 20px;
  25.     font-size: 16px;
  26.     cursor: pointer;
  27. }

  28. .hidden {
  29.     display: none;
  30. }

  31. #game-over {
  32.     font-size: 24px;
  33.     color: red;
  34.     margin-top: 20px;
  35. }
复制代码


### JavaScript (`script.js`)

  1. const canvas = document.getElementById('game-board');
  2. const context = canvas.getContext('2d');
  3. const startButton = document.getElementById('start-button');
  4. const gameOverText = document.getElementById('game-over');

  5. canvas.width = 400;
  6. canvas.height = 400;

  7. const cellSize = 20;
  8. let snake = [];
  9. let direction = { x: 0, y: 0 };
  10. let food = {};
  11. let gameInterval;

  12. const snakeHeadImg = new Image();
  13. snakeHeadImg.src = 'snake-head.png';

  14. const snakeBodyImg = new Image();
  15. snakeBodyImg.src = 'snake-body.png';

  16. const foodImg = new Image();
  17. foodImg.src = 'food.png';

  18. function drawBackground() {
  19.     context.fillStyle = 'rgba(0, 128, 128, 0.5)';
  20.     context.fillRect(0, 0, canvas.width, canvas.height);
  21. }

  22. function drawSnake() {
  23.     for (let i = 0; i < snake.length; i++) {
  24.         const part = snake[i];
  25.         if (i === 0) { // 画蛇头
  26.             context.drawImage(snakeHeadImg, part.x, part.y, cellSize, cellSize);
  27.         } else { // 画蛇身
  28.             context.drawImage(snakeBodyImg, part.x, part.y, cellSize, cellSize);
  29.         }
  30.     }
  31. }

  32. function spawnFood() {
  33.     food.x = Math.floor(Math.random() * canvas.width / cellSize) * cellSize;
  34.     food.y = Math.floor(Math.random() * canvas.height / cellSize) * cellSize;
  35. }

  36. function drawFood() {
  37.     context.drawImage(foodImg, food.x, food.y, cellSize, cellSize);
  38. }

  39. function updateSnake() {
  40.     const head = { x: snake[0].x + direction.x * cellSize, y: snake[0].y + direction.y * cellSize };
  41.     snake.unshift(head);

  42.     if (head.x === food.x && head.y === food.y) {
  43.         spawnFood();
  44.     } else {
  45.         snake.pop();
  46.     }

  47.     if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height || snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y)) {
  48.         gameOver();
  49.     }
  50. }

  51. function gameOver() {
  52.     clearInterval(gameInterval);
  53.     gameOverText.classList.remove('hidden');
  54. }

  55. function gameLoop() {
  56.     drawBackground();
  57.     drawSnake();
  58.     drawFood();
  59.     updateSnake();
  60. }

  61. document.addEventListener('keydown', event => {
  62.     if (event.key === 'ArrowUp' && direction.y === 0) {
  63.         direction = { x: 0, y: -1 };
  64.     } else if (event.key === 'ArrowDown' && direction.y === 0) {
  65.         direction = { x: 0, y: 1 };
  66.     } else if (event.key === 'ArrowLeft' && direction.x === 0) {
  67.         direction = { x: -1, y: 0 };
  68.     } else if (event.key === 'ArrowRight' && direction.x === 0) {
  69.         direction = { x: 1, y: 0 };
  70.     }
  71. });

  72. startButton.addEventListener('click', () => {
  73.     snake = [{ x: cellSize * 5, y: cellSize * 5 }];
  74.     direction = { x: 0, y: 0 };
  75.     spawnFood();
  76.     gameOverText.classList.add('hidden');
  77.     gameInterval = setInterval(gameLoop, 100);
  78. });
复制代码


### 补充资源

1. **背景图片:** `background.jpg`
2. **蛇头图片:** `snake-head.png`
3. **蛇身图片:** `snake-body.png`
4. **食物图片:** `food.png`

确保将这些图片文件存放在与HTML、CSS和JavaScript文件相同的目录下。打开 `index.html` 运行游戏时,你会看到具有生动蛇头、蛇身和食物的贪吃蛇游戏了。享受游戏吧!如果你有进一步的需求或问题,请随时告诉我!
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
5#
 楼主| 发表于 2024-7-12 11:05:39 | 只看该作者
情在代码中告诉我添加图片的位置
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
6#
发表于 2024-7-12 11:06:26 | 只看该作者
林奕锦 发表于 2024-07-12 11:05
情在代码中告诉我添加图片的位置


好的,我会在代码中添加具体的注释,指导你如何添加图片。确保将图片文件放在与HTML、CSS和JavaScript文件相同的目录下。

### HTML (`index.html`)

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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="style.css">
  8. </head>
  9. <body>
  10.     <div id="game-container">
  11.         <canvas id="game-board"></canvas>
  12.         <div id="controls">
  13.             <button id="start-button">开始游戏</button>
  14.             <div id="game-over" class="hidden">游戏结束</div>
  15.         </div>
  16.     </div>
  17.     <script src="script.js"></script>
  18. </body>
  19. </html>
复制代码


### CSS (`style.css`)

  1. body {
  2.     display: flex;
  3.     justify-content: center;
  4.     align-items: center;
  5.     height: 100vh;
  6.     margin: 0;
  7.     background-image: url('background.jpg'); /* 背景图片 */
  8.     background-size: cover;
  9. }

  10. #game-container {
  11.     display: flex;
  12.     flex-direction: column;
  13.     align-items: center;
  14. }

  15. #game-board {
  16.     border: 1px solid #000;
  17.     background-color: rgba(255, 255, 255, 0.6);
  18. }

  19. #controls {
  20.     margin-top: 20px;
  21.     text-align: center;
  22. }

  23. button {
  24.     padding: 10px 20px;
  25.     font-size: 16px;
  26.     cursor: pointer;
  27. }

  28. .hidden {
  29.     display: none;
  30. }

  31. #game-over {
  32.     font-size: 24px;
  33.     color: red;
  34.     margin-top: 20px;
  35. }
复制代码


### JavaScript (`script.js`)

  1. const canvas = document.getElementById('game-board');
  2. const context = canvas.getContext('2d');
  3. const startButton = document.getElementById('start-button');
  4. const gameOverText = document.getElementById('game-over');

  5. canvas.width = 400;
  6. canvas.height = 400;

  7. const cellSize = 20;
  8. let snake = [{ x: 2 * cellSize, y: 2 * cellSize }]; // 初始位置
  9. let direction = { x: 0, y: 0 };
  10. let food = {};
  11. let gameInterval;

  12. // 加载蛇头和蛇身图像
  13. const snakeHeadImg = new Image();
  14. snakeHeadImg.src = 'snake-head.png'; // 蛇头图片位置

  15. const snakeBodyImg = new Image();
  16. snakeBodyImg.src = 'snake-body.png'; // 蛇身图片位置

  17. // 加载食物图像
  18. const foodImg = new Image();
  19. foodImg.src = 'food.png'; // 食物图片位置

  20. function drawBackground() {
  21.     context.fillStyle = 'rgba(0, 128, 128, 0.5)';
  22.     context.fillRect(0, 0, canvas.width, canvas.height);
  23. }

  24. function drawSnake() {
  25.     for (let i = 0; i < snake.length; i++) {
  26.         const part = snake[i];
  27.         if (i === 0) { // 画蛇头
  28.             context.drawImage(snakeHeadImg, part.x, part.y, cellSize, cellSize);
  29.         } else { // 画蛇身
  30.             context.drawImage(snakeBodyImg, part.x, part.y, cellSize, cellSize);
  31.         }
  32.     }
  33. }

  34. function spawnFood() {
  35.     food.x = Math.floor(Math.random() * canvas.width / cellSize) * cellSize;
  36.     food.y = Math.floor(Math.random() * canvas.height / cellSize) * cellSize;
  37. }

  38. function drawFood() {
  39.     context.drawImage(foodImg, food.x, food.y, cellSize, cellSize);
  40. }

  41. function updateSnake() {
  42.     const head = { x: snake[0].x + direction.x * cellSize, y: snake[0].y + direction.y * cellSize };
  43.     snake.unshift(head);

  44.     if (head.x === food.x && head.y === food.y) {
  45.         spawnFood();
  46.     } else {
  47.         snake.pop();
  48.     }

  49.     if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height || snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y)) {
  50.         gameOver();
  51.     }
  52. }

  53. function gameOver() {
  54.     clearInterval(gameInterval);
  55.     gameOverText.classList.remove('hidden');
  56. }

  57. function gameLoop() {
  58.     drawBackground();
  59.     drawSnake();
  60.     drawFood();
  61.     updateSnake();
  62. }

  63. document.addEventListener('keydown', event => {
  64.     if (event.key === 'ArrowUp' && direction.y === 0) {
  65.         direction = { x: 0, y: -1 };
  66.     } else if (event.key === 'ArrowDown' && direction.y === 0) {
  67.         direction = { x: 0, y: 1 };
  68.     } else if (event.key === 'ArrowLeft' && direction.x === 0) {
  69.         direction = { x: -1, y: 0 };
  70.     } else if (event.key === 'ArrowRight' && direction.x === 0) {
  71.         direction = { x: 1, y: 0 };
  72.     }
  73. });

  74. startButton.addEventListener('click', () => {
  75.     snake = [{ x: 2 * cellSize, y: 2 * cellSize }];
  76.     direction = { x: 0, y: 0 };
  77.     spawnFood();
  78.     gameOverText.classList.add('hidden');
  79.     gameInterval = setInterval(gameLoop, 100);
  80. });
复制代码


### 补充资源

请确保以下图片文件(背景、蛇头、蛇身、食物)与你的 HTML、CSS 和 JavaScript 文件位于同一目录下:

1. **背景图片:** `background.jpg`
2. **蛇头图片:** `snake-head.png`
3. **蛇身图片:** `snake-body.png`
4. **食物图片:** `food.png`

以上是完整的代码,确保所有图片都放置在正确的位置后,打开 `index.html` 文件即可运行具有生动蛇头和蛇身的贪吃蛇游戏。希望你喜欢这个游戏!如果有任何问题或者进一步的需求,请随时告诉我。
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
7#
 楼主| 发表于 2024-7-12 11:06:32 | 只看该作者
kaia 发表于 2024-7-12 10:50
当然可以!我们可以用图片来代替蛇头和蛇身的简单颜色填充,以实现更生动的效果。下面是修改后的实现:

# ...


请在代码中告诉我添加图片的位置
回复 支持 反对

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
8#
发表于 2024-7-12 11:07:03 | 只看该作者
林奕锦 发表于 2024-07-12 11:06
请在代码中告诉我添加图片的位置


明白了,我会在代码中添加详细的注释,指导你如何添加图片的位置。请确保将图片文件放在与HTML、CSS和JavaScript文件相同的目录下。

### HTML (`index.html`)

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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="style.css">
  8. </head>
  9. <body>
  10.     <div id="game-container">
  11.         <canvas id="game-board"></canvas>
  12.         <div id="controls">
  13.             <button id="start-button">开始游戏</button>
  14.             <div id="game-over" class="hidden">游戏结束</div>
  15.         </div>
  16.     </div>
  17.     <script src="script.js"></script>
  18. </body>
  19. </html>
复制代码


### CSS (`style.css`)

  1. body {
  2.     display: flex;
  3.     justify-content: center;
  4.     align-items: center;
  5.     height: 100vh;
  6.     margin: 0;
  7.     background-image: url('background.jpg'); /* 背景图片 */
  8.     background-size: cover;
  9. }

  10. #game-container {
  11.     display: flex;
  12.     flex-direction: column;
  13.     align-items: center;
  14. }

  15. #game-board {
  16.     border: 1px solid #000;
  17.     background-color: rgba(255, 255, 255, 0.6);
  18. }

  19. #controls {
  20.     margin-top: 20px;
  21.     text-align: center;
  22. }

  23. button {
  24.     padding: 10px 20px;
  25.     font-size: 16px;
  26.     cursor: pointer;
  27. }

  28. .hidden {
  29.     display: none;
  30. }

  31. #game-over {
  32.     font-size: 24px;
  33.     color: red;
  34.     margin-top: 20px;
  35. }
复制代码


### JavaScript (`script.js`)

  1. const canvas = document.getElementById('game-board');
  2. const context = canvas.getContext('2d');
  3. const startButton = document.getElementById('start-button');
  4. const gameOverText = document.getElementById('game-over');

  5. canvas.width = 400;
  6. canvas.height = 400;

  7. const cellSize = 20;
  8. let snake = [{ x: 200, y: 200 }];
  9. let direction = { x: 0, y: 0 };
  10. let food = {};
  11. let gameInterval;

  12. // 加载蛇头和蛇身图像
  13. const snakeHeadImg = new Image();
  14. snakeHeadImg.src = 'snake-head.png'; // 蛇头图片位置

  15. const snakeBodyImg = new Image();
  16. snakeBodyImg.src = 'snake-body.png'; // 蛇身图片位置

  17. // 加载食物图像
  18. const foodImg = new Image();
  19. foodImg.src = 'food.png'; // 食物图片位置

  20. function drawBackground() {
  21.     context.fillStyle = 'rgba(0, 128, 128, 0.5)';
  22.     context.fillRect(0, 0, canvas.width, canvas.height);
  23. }

  24. function drawSnake() {
  25.     for (let i = 0; i < snake.length; i++) {
  26.         const part = snake[i];
  27.         if (i === 0) { // 画蛇头
  28.             context.drawImage(snakeHeadImg, part.x, part.y, cellSize, cellSize);
  29.         } else { // 画蛇身
  30.             context.drawImage(snakeBodyImg, part.x, part.y, cellSize, cellSize);
  31.         }
  32.     }
  33. }

  34. function spawnFood() {
  35.     food.x = Math.floor(Math.random() * canvas.width / cellSize) * cellSize;
  36.     food.y = Math.floor(Math.random() * canvas.height / cellSize) * cellSize;
  37. }

  38. function drawFood() {
  39.     context.drawImage(foodImg, food.x, food.y, cellSize, cellSize);
  40. }

  41. function updateSnake() {
  42.     const head = { x: snake[0].x + direction.x * cellSize, y: snake[0].y + direction.y * cellSize };
  43.     snake.unshift(head);

  44.     if (head.x === food.x && head.y === food.y) {
  45.         spawnFood();
  46.     } else {
  47.         snake.pop();
  48.     }

  49.     if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height || snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y)) {
  50.         gameOver();
  51.     }
  52. }

  53. function gameOver() {
  54.     clearInterval(gameInterval);
  55.     gameOverText.classList.remove('hidden');
  56. }

  57. function gameLoop() {
  58.     drawBackground();
  59.     drawSnake();
  60.     drawFood();
  61.     updateSnake();
  62. }

  63. document.addEventListener('keydown', event => {
  64.     if (event.key === 'ArrowUp' && direction.y === 0) {
  65.         direction = { x: 0, y: -1 };
  66.     } else if (event.key === 'ArrowDown' && direction.y === 0) {
  67.         direction = { x: 0, y: 1 };
  68.     } else if (event.key === 'ArrowLeft' && direction.x === 0) {
  69.         direction = { x: -1, y: 0 };
  70.     } else if (event.key === 'ArrowRight' && direction.x === 0) {
  71.         direction = { x: 1, y: 0 };
  72.     }
  73. });

  74. startButton.addEventListener('click', () => {
  75.     snake = [{ x: 200, y: 200 }];
  76.     direction = { x: 0, y: 0 };
  77.     spawnFood();
  78.     gameOverText.classList.add('hidden');
  79.     gameInterval = setInterval(gameLoop, 100);
  80. });
复制代码


### 补充资源

将以下图片文件放在与HTML、CSS和JavaScript文件相同的目录下:

1. **背景图片:** `background.jpg`
2. **蛇头图片:** `snake-head.png`
3. **蛇身图片:** `snake-body.png`
4. **食物图片:** `food.png`

将这些文件添加到同一个目录下后,打开 `index.html` 文件就可以运行具有生动蛇头和蛇身的贪吃蛇游戏了。如果有任何问题或者进一步的需求,请随时告诉我。
回复 支持 反对

使用道具 举报

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

本版积分规则

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