科学教育

搜索
楼主: 解轩瑜
打印 上一主题 下一主题

解轩瑜

[复制链接]

3

主题

45

帖子

117

积分

注册会员

Rank: 2

积分
117
31#
 楼主| 发表于 2024-7-29 10:13:17 | 只看该作者
完整点,把上次成功的合起来
回复 支持 反对

使用道具 举报

0

主题

1147

帖子

1718

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1718
32#
发表于 2024-7-29 10:13:52 | 只看该作者
解轩瑜 发表于 2024-07-29 10:13
完整点,把上次成功的合起来


好的,我会将之前成功的多玩家贪吃蛇游戏和新增的暂停功能进行整合。以下是完整的代码,包括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="styles.css">
  8. </head>
  9. <body>
  10.     <h1>多人贪吃蛇</h1>
  11.     <div id="score">得分: </div>
  12.     <canvas id="gameCanvas" width="800" height="600"></canvas>
  13.     <button id="restartButton">重新开始游戏</button>
  14.     <button id="pauseButton">暂停游戏</button>
  15.     <script src="script.js"></script>
  16. </body>
  17. </html>
复制代码


### CSS文件(styles.css)

  1. body {
  2.     display: flex;
  3.     flex-direction: column;
  4.     align-items: center;
  5.     justify-content: center;
  6.     margin: 0;
  7.     background-color: #f0f0f0;
  8.     font-family: Arial, sans-serif;
  9. }

  10. h1 {
  11.     color: #333;
  12. }

  13. canvas {
  14.     border: 1px solid #333;
  15. }
复制代码


### JavaScript文件(script.js)

  1. const canvas = document.getElementById("gameCanvas");
  2. const ctx = canvas.getContext("2d");
  3. const restartButton = document.getElementById("restartButton");
  4. const pauseButton = document.getElementById("pauseButton");

  5. const box = 20;
  6. const rows = canvas.height / box;
  7. const columns = canvas.width / box;

  8. let snakes = []; // 蛇的数组
  9. let foods = []; // 食物的数组
  10. let directions = []; // 每个蛇的方向
  11. let scores = []; // 每个玩家的得分
  12. let gameInterval;
  13. let isPaused = false;

  14. function createSnake(x, y) {
  15.     return [{ x: x * box, y: y * box }];
  16. }

  17. function addFood() {
  18.     while (foods.length < 10) {
  19.         let foodX = Math.floor(Math.random() * columns) * box;
  20.         let foodY = Math.floor(Math.random() * rows) * box;
  21.         if (!isFoodOnSnake(foodX, foodY)) {
  22.             foods.push({ x: foodX, y: foodY });
  23.         }
  24.     }
  25. }

  26. function isFoodOnSnake(foodX, foodY) {
  27.     return snakes.some(snake => snake.some(segment => segment.x === foodX && segment.y === foodY));
  28. }

  29. function initGame() {
  30.     clearInterval(gameInterval);
  31.     directions = [{ x: 1, y: 0 }, { x: -1, y: 0 }]; // 初始方向
  32.     snakes = [
  33.         createSnake(2, 1), // 玩家1的初始位置在画布左边
  34.         createSnake(columns - 3, rows - 2) // 玩家2的初始位置在画布右边
  35.     ];
  36.     scores = [0, 0]; // 初始得分
  37.     foods = []; // 清空食物数组
  38.     addFood(); // 增加食物
  39.     gameInterval = setInterval(updateGame, 100);
  40. }

  41. function updateGame() {
  42.     if (!isPaused) {
  43.         ctx.clearRect(0, 0, canvas.width, canvas.height);

  44.         // 绘制食物
  45.         for (let f = 0; f < foods.length; f++) {
  46.             ctx.fillStyle = "red";
  47.             ctx.fillRect(foods[f].x, foods[f].y, box, box);
  48.         }

  49.         for (let n = 0; n < snakes.length; n++) {
  50.             let snake = snakes[n];
  51.             let newHead = { x: snake[0].x + directions[n].x * box, y: snake[0].y + directions[n].y * box };

  52.             // 检测边界和碰撞
  53.             if (newHead.x < 0 || newHead.y < 0 || newHead.x >= canvas.width || newHead.y >= canvas.height || isCollide(snake, newHead)) {
  54.                 clearInterval(gameInterval);
  55.                 alert(`游戏结束,玩家${n + 1}输了`);
  56.                 return;
  57.             }

  58.             snake.unshift(newHead); // 添加新头
  59.             let ateFood = false;

  60.             for (let f = 0; f < foods.length; f++) {
  61.                 if (foods[f].x === newHead.x && foods[f].y === newHead.y) {
  62.                     foods.splice(f, 1);
  63.                     ateFood = true;
  64.                     scores[n] += 10;
  65.                     break;
  66.                 }
  67.             }

  68.             if (!ateFood) {
  69.                 snake.pop(); // 移除尾部
  70.             }

  71.             // 绘制蛇
  72.             for (let s = 0; s < snake.length; s++) {
  73.                 ctx.fillStyle = n === 0 ? 'green' : 'blue'; // 玩家1为绿色,玩家2为蓝色
  74.                 ctx.fillRect(snake[s].x, snake[s].y, box, box);
  75.             }
  76.         }

  77.         addFood(); // 如果食物被吃掉,增加新食物

  78.         // 更新得分显示
  79.         document.getElementById('score').textContent = `得分: 玩家1 - ${scores[0]}, 玩家2 - ${scores[1]}`;
  80.     }
  81. }

  82. function isCollide(snake, head) {
  83.     return snake.some(segment => segment.x === head.x && segment.y === head.y);
  84. }

  85. document.addEventListener('keydown', function(e) {
  86.     // 控制玩家1
  87.     if (e.key === 'w' && directions[0].y === 0) {
  88.         directions[0] = { x: 0, y: -1 };
  89.     } else if (e.key === 's' && directions[0].y === 0) {
  90.         directions[0] = { x: 0, y: 1 };
  91.     } else if (e.key === 'a' && directions[0].x === 0) {
  92.         directions[0] = { x: -1, y: 0 };
  93.     } else if (e.key === 'd' && directions[0].x === 0) {
  94.         directions[0] = { x: 1, y: 0 };
  95.     }

  96.     // 控制玩家2
  97.     if (e.key === 'ArrowUp' && directions[1].y === 0) {
  98.         directions[1] = { x: 0, y: -1 };
  99.     } else if (e.key === 'ArrowDown' && directions[1].y === 0) {
  100.         directions[1] = { x: 0, y: 1 };
  101.     } else if (e.key === 'ArrowLeft' && directions[1].x === 0) {
  102.         directions[1] = { x: -1, y: 0 };
  103.     } else if (e.key === 'ArrowRight' && directions[1].x === 0) {
  104.         directions[1] = { x: 1, y: 0 };
  105.     }
  106. });

  107. pauseButton.addEventListener("click", function() {
  108.     isPaused = !isPaused;
  109.     pauseButton.textContent = isPaused ? "继续游戏" : "暂停游戏";
  110. });

  111. restartButton.addEventListener("click", initGame);

  112. initGame(); // 初始化游戏
复制代码


### 功能说明:

1. **玩家控制**:
   - 玩家1使用WASD键来控制。
   - 玩家2使用箭头键来控制。

2. **食物**:
   - 食物会随机生成,玩家每吃一个食物得10分。

3. **边界和碰撞检测**:
   - 如果蛇碰到边界或自身,游戏结束。

4. **得分显示**:
   - 游戏页面的上方会显示每个玩家的得分。

5. **暂停功能**:
   - 添加了一个 `pauseButton`,点击该按钮可以暂停或继续游戏。

将这些代码分别保存到 `index.html`、 `styles.css` 和 `script.js` 文件中,并确保它们在同一目录下。然后打开 `index.html` 文件,即可在浏览器里运行带有暂停功能的多人贪吃蛇游戏。

如果你遇到任何问题或需要进一步的帮助,请随时告诉我!
回复 支持 反对

使用道具 举报

3

主题

45

帖子

117

积分

注册会员

Rank: 2

积分
117
33#
 楼主| 发表于 2024-7-29 10:29:51 | 只看该作者
植物大战僵尸,不能开始,干脆把真的植物大战僵尸1的三件套代码给我
回复 支持 反对

使用道具 举报

3

主题

45

帖子

117

积分

注册会员

Rank: 2

积分
117
34#
 楼主| 发表于 2024-7-29 10:38:47 | 只看该作者
好了没有?
回复

使用道具 举报

3

主题

45

帖子

117

积分

注册会员

Rank: 2

积分
117
35#
 楼主| 发表于 2024-7-29 12:06:17 | 只看该作者
植物大战僵尸1的三件套代码给我
回复 支持 反对

使用道具 举报

3

主题

45

帖子

117

积分

注册会员

Rank: 2

积分
117
36#
 楼主| 发表于 2024-7-29 12:14:56 | 只看该作者
植物大战僵尸用三件套合成代码
回复 支持 反对

使用道具 举报

3

主题

45

帖子

117

积分

注册会员

Rank: 2

积分
117
37#
 楼主| 发表于 2024-7-29 12:18:49 | 只看该作者
AI,植物大战僵尸用三件套合成代码,快点!
回复 支持 反对

使用道具 举报

3

主题

45

帖子

117

积分

注册会员

Rank: 2

积分
117
38#
 楼主| 发表于 2024-7-29 12:29:59 | 只看该作者
用三件套合成火柴人对战的代码
回复 支持 反对

使用道具 举报

3

主题

45

帖子

117

积分

注册会员

Rank: 2

积分
117
39#
 楼主| 发表于 2024-7-31 10:43:58 | 只看该作者
用三件套合成火柴人对战的代码
回复 支持 反对

使用道具 举报

3

主题

45

帖子

117

积分

注册会员

Rank: 2

积分
117
40#
 楼主| 发表于 2024-7-31 10:51:23 | 只看该作者

用三件套合成飞机大战的代码


回复 支持 反对

使用道具 举报

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

本版积分规则

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