科学教育

搜索
楼主: 林奕锦
打印 上一主题 下一主题

林奕锦

[复制链接]

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
11#
 楼主| 发表于 2024-7-12 09:59:15 | 只看该作者
kaia 发表于 2024-7-12 09:44
当然可以!以下是一个简化版的贪吃蛇游戏的网页代码,包含HTML、CSS和JavaScript:

**HTML(index.html ...

你好,请问可不可以在这个代码的基础上增加更丰富的背景,更生动的角色,有开始按钮和结束提示
回复 支持 反对

使用道具 举报

0

主题

1147

帖子

1718

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1718
12#
发表于 2024-7-12 09:59:58 | 只看该作者
林奕锦 发表于 2024-07-12 09:59
你好,请问可不可以在这个代码的基础上增加更丰富的背 ...


当然可以!我们可以对现有代码进行一些改进,增加一个开始按钮、结束提示,以及更丰富的背景和角色。以下是更新后的代码:

### 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 class="game-container">
  11.         <canvas id="gameCanvas" width="400" height="400"></canvas>
  12.         <button id="startButton">开始游戏</button>
  13.         <div id="gameOverMessage"></div>
  14.     </div>
  15.     <script src="script.js"></script>
  16. </body>
  17. </html>
复制代码


### CSS(style.css)

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

  9. .game-container {
  10.     text-align: center;
  11. }

  12. canvas {
  13.     border: 1px solid #000;
  14.     background-image: url('background.png'); /* 更丰富的背景图 */
  15.     background-size: cover;
  16. }

  17. #startButton {
  18.     margin-top: 20px;
  19.     padding: 10px 20px;
  20.     font-size: 16px;
  21.     cursor: pointer;
  22. }

  23. #gameOverMessage {
  24.     margin-top: 20px;
  25.     font-size: 18px;
  26.     color: red;
  27. }
复制代码


### JavaScript(script.js)

  1. const canvas = document.getElementById('gameCanvas');
  2. const ctx = canvas.getContext('2d');

  3. const box = 20;
  4. let snake;
  5. let food;
  6. let score;
  7. let direction;
  8. let gameInterval;
  9. const startButton = document.getElementById('startButton');
  10. const gameOverMessage = document.getElementById('gameOverMessage');

  11. startButton.addEventListener('click', startGame);
  12. document.addEventListener('keydown', setDirection);

  13. function startGame() {
  14.     // 初始化游戏参数
  15.     snake = [{ x: 9 * box, y: 10 * box }];
  16.     food = {
  17.         x: Math.floor(Math.random() * 19 + 1) * box,
  18.         y: Math.floor(Math.random() * 19 + 1) * box
  19.     };
  20.     score = 0;
  21.     direction = null;
  22.     gameOverMessage.innerHTML = '';
  23.    
  24.     if (gameInterval) {
  25.         clearInterval(gameInterval);
  26.     }
  27.     gameInterval = setInterval(draw, 100);
  28. }

  29. function setDirection(event) {
  30.     const key = event.keyCode;
  31.     if (key == 37 && direction != 'RIGHT') {
  32.         direction = 'LEFT';
  33.     } else if (key == 38 && direction != 'DOWN') {
  34.         direction = 'UP';
  35.     } else if (key == 39 && direction != 'LEFT') {
  36.         direction = 'RIGHT';
  37.     } else if (key == 40 && direction != 'UP') {
  38.         direction = 'DOWN';
  39.     }
  40. }

  41. function draw() {
  42.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  43.    
  44.     for (let i = 0; i < snake.length; i++) {
  45.         const snakePart = new Image();
  46.         snakePart.src = i === 0 ? 'head.png' : 'body.png'; // 更生动的蛇头和蛇身
  47.         ctx.drawImage(snakePart, snake[i].x, snake[i].y, box, box);
  48.     }

  49.     const foodImg = new Image();
  50.     foodImg.src = 'food.png'; // 更生动的食物
  51.     ctx.drawImage(foodImg, food.x, food.y, box, box);
  52.    
  53.     let snakeX = snake[0].x;
  54.     let snakeY = snake[0].y;

  55.     if (direction == 'LEFT') snakeX -= box;
  56.     if (direction == 'UP') snakeY -= box;
  57.     if (direction == 'RIGHT') snakeX += box;
  58.     if (direction == 'DOWN') snakeY += box;

  59.     if (snakeX == food.x && snakeY == food.y) {
  60.         score++;
  61.         food = {
  62.             x: Math.floor(Math.random() * 19 + 1) * box,
  63.             y: Math.floor(Math.random() * 19 + 1) * box
  64.         };
  65.     } else {
  66.         snake.pop();
  67.     }

  68.     let newHead = { x: snakeX, y: snakeY };

  69.     if (snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake)) {
  70.         clearInterval(gameInterval);
  71.         gameOverMessage.innerHTML = `游戏结束. 得分: ${score}`;
  72.     }

  73.     snake.unshift(newHead);

  74.     ctx.fillStyle = 'black';
  75.     ctx.font = '45px Changa one';
  76.     ctx.fillText(score, box, 1.5 * box);
  77. }

  78. function collision(head, array) {
  79.     for (let i = 0; i < array.length; i++) {
  80.         if (head.x == array[i].x && head.y == array[i].y) {
  81.             return true;
  82.         }
  83.     }
  84.     return false;
  85. }
复制代码


### 补充图片资源
为了这个例子更生动,你可以使用以下图片资源:
- `background.png`:游戏背景图
- `head.png`:蛇头图
- `body.png`:蛇身图
- `food.png`:食物图

将这些图片放在与HTML文件相同的目录下,确保它们的文件名和代码中的一致。

以上就是完整的贪吃蛇游戏代码,包含更丰富的背景、更生动的角色、开始按钮和结束提示。你可以将这些代码复制到相应的文件中然后通过浏览器打开`index.html`来运行这个游戏。祝你游戏愉快!
回复 支持 反对

使用道具 举报

8

主题

62

帖子

184

积分

注册会员

Rank: 2

积分
184
13#
 楼主| 发表于 2024-7-12 10:06:26 | 只看该作者


你好,请帮我生成一个贪吃蛇游戏的网页代码,用html、css、js来完成
回复 支持 反对

使用道具 举报

0

主题

1147

帖子

1718

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1718
14#
发表于 2024-7-12 10:07:12 | 只看该作者
林奕锦 发表于 2024-07-12 10:06
你好,请帮我生成一个贪吃蛇游戏的网页代码,用html、 ...


好的,这里是一个完整的贪吃蛇游戏示例,包括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="style.css">
  8. </head>
  9. <body>
  10.     <div class="game-container">
  11.         <canvas id="gameCanvas" width="400" height="400"></canvas>
  12.         <button id="startButton">开始游戏</button>
  13.         <div id="gameOverMessage"></div>
  14.     </div>
  15.     <script src="script.js"></script>
  16. </body>
  17. </html>
复制代码


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

  9. .game-container {
  10.     text-align: center;
  11. }

  12. canvas {
  13.     border: 1px solid #000;
  14.     background-color: #eee; /* 可以替换为背景图片 */
  15. }

  16. #startButton {
  17.     margin-top: 20px;
  18.     padding: 10px 20px;
  19.     font-size: 16px;
  20.     cursor: pointer;
  21. }

  22. #gameOverMessage {
  23.     margin-top: 20px;
  24.     font-size: 18px;
  25.     color: red;
  26. }
复制代码


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

  3. const box = 20;
  4. let snake;
  5. let food;
  6. let score;
  7. let direction;
  8. let gameInterval;
  9. const startButton = document.getElementById('startButton');
  10. const gameOverMessage = document.getElementById('gameOverMessage');

  11. startButton.addEventListener('click', startGame);
  12. document.addEventListener('keydown', setDirection);

  13. function startGame() {
  14.     // 初始化游戏参数
  15.     snake = [{ x: 9 * box, y: 9 * box }];
  16.     food = {
  17.         x: Math.floor(Math.random() * 19 + 1) * box,
  18.         y: Math.floor(Math.random() * 19 + 1) * box
  19.     };
  20.     score = 0;
  21.     direction = null;
  22.     gameOverMessage.innerHTML = '';

  23.     if (gameInterval) {
  24.         clearInterval(gameInterval);
  25.     }
  26.     gameInterval = setInterval(draw, 100);
  27. }

  28. function setDirection(event) {
  29.     const key = event.keyCode;
  30.     if (key == 37 && direction != 'RIGHT') {
  31.         direction = 'LEFT';
  32.     } else if (key == 38 && direction != 'DOWN') {
  33.         direction = 'UP';
  34.     } else if (key == 39 && direction != 'LEFT') {
  35.         direction = 'RIGHT';
  36.     } else if (key == 40 && direction != 'UP') {
  37.         direction = 'DOWN';
  38.     }
  39. }

  40. function draw() {
  41.     ctx.clearRect(0, 0, canvas.width, canvas.height);

  42.     for (let i = 0; i < snake.length; i++) {
  43.         ctx.fillStyle = i === 0 ? 'green' : 'white';
  44.         ctx.fillRect(snake[i].x, snake[i].y, box, box);

  45.         ctx.strokeStyle = 'red';
  46.         ctx.strokeRect(snake[i].x, snake[i].y, box, box);
  47.     }

  48.     ctx.fillStyle = 'red';
  49.     ctx.fillRect(food.x, food.y, box, box);

  50.     let snakeX = snake[0].x;
  51.     let snakeY = snake[0].y;

  52.     if (direction == 'LEFT') snakeX -= box;
  53.     if (direction == 'UP') snakeY -= box;
  54.     if (direction == 'RIGHT') snakeX += box;
  55.     if (direction == 'DOWN') snakeY += box;

  56.     if (snakeX == food.x && snakeY == food.y) {
  57.         score++;
  58.         food = {
  59.             x: Math.floor(Math.random() * 19 + 1) * box,
  60.             y: Math.floor(Math.random() * 19 + 1) * box
  61.         };
  62.     } else {
  63.         snake.pop();
  64.     }

  65.     let newHead = { x: snakeX, y: snakeY };

  66.     if (snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake)) {
  67.         clearInterval(gameInterval);
  68.         gameOverMessage.innerHTML = `游戏结束. 得分: ${score}`;
  69.     }

  70.     snake.unshift(newHead);

  71.     ctx.fillStyle = 'black';
  72.     ctx.font = '45px Changa one';
  73.     ctx.fillText(score, box, 1.5 * box);
  74. }

  75. function collision(head, array) {
  76.     for (let i = 0; i < array.length; i++) {
  77.         if (head.x == array[i].x && head.y == array[i].y) {
  78.             return true;
  79.         }
  80.     }
  81.     return false;
  82. }

  83. startGame();
复制代码


上面的代码可以放在三个独立的文件中。你可以用更生动的图像来替换背景、蛇头、蛇身和食物。如果你没有这些图像,也可以选择颜色填充的方式展示蛇和食物。你只需要将这些文件保存在同一个目录下,然后在浏览器中打开`index.html`来启动游戏。

游戏开始时,点击“开始游戏”按钮进行游戏,每次游戏结束后会显示得分。这样,你就具备了一个完整的、有开始按钮和结束提示的贪吃蛇游戏。希望你喜欢这个小游戏!
回复 支持 反对

使用道具 举报

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

本版积分规则

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