科学教育

搜索
查看: 1410|回复: 1
打印 上一主题 下一主题

daima

[复制链接]

1

主题

4

帖子

19

积分

新手上路

Rank: 1

积分
19
跳转到指定楼层
楼主
发表于 2024-7-12 19:10:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
你好,你是一名专业的程序员,请你帮我生成一个贪吃蛇网页代码,用himl、css、js文件完成
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2024-7-12 19:10:47 | 只看该作者
当然可以!以下是一个简单的贪吃蛇网页游戏的代码示例,包含HTML、CSS和JavaScript文件。

1. 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.     <div class="game-container">
  11.         <canvas id="gameCanvas"></canvas>
  12.     </div>
  13.     <script src="script.js"></script>
  14. </body>
  15. </html>
复制代码


2. 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: #000;
  8. }

  9. .game-container {
  10.     position: relative;
  11.     width: 500px;
  12.     height: 500px;
  13. }

  14. canvas {
  15.     display: block;
  16.     border: 1px solid #fff;
  17.     background-color: #111;
  18. }
复制代码


3. JavaScript 文件 (`script.js`):
  1. const canvas = document.getElementById('gameCanvas');
  2. const ctx = canvas.getContext('2d');

  3. const tileSize = 20;
  4. const tileCount = canvas.width / tileSize;
  5. let snake = [{ x: tileCount / 2, y: tileCount / 2 }];
  6. let direction = { x: 0, y: 0 };
  7. let apple = { x: Math.floor(Math.random() * tileCount), y: Math.floor(Math.random() * tileCount) };
  8. let score = 0;

  9. document.addEventListener('keydown', (event) => {
  10.     switch (event.key) {
  11.         case 'ArrowUp':
  12.             if (direction.y === 0) direction = { x: 0, y: -1 };
  13.             break;
  14.         case 'ArrowDown':
  15.             if (direction.y === 0) direction = { x: 0, y: 1 };
  16.             break;
  17.         case 'ArrowLeft':
  18.             if (direction.x === 0) direction = { x: -1, y: 0 };
  19.             break;
  20.         case 'ArrowRight':
  21.             if (direction.x === 0) direction = { x: 1, y: 0 };
  22.             break;
  23.     }
  24. });

  25. function gameLoop() {
  26.     const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };

  27.     // Check collision with walls
  28.     if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
  29.         alert('Game Over!');
  30.         document.location.reload();
  31.     }

  32.     // Check collision with itself
  33.     for (let segment of snake) {
  34.         if (segment.x === head.x && segment.y === head.y) {
  35.             alert('Game Over!');
  36.             document.location.reload();
  37.         }
  38.     }

  39.     snake.unshift(head);

  40.     // Check if snake has eaten the apple
  41.     if (head.x === apple.x && head.y === apple.y) {
  42.         score++;
  43.         apple = { x: Math.floor(Math.random() * tileCount), y: Math.floor(Math.random() * tileCount) };
  44.     } else {
  45.         snake.pop();
  46.     }

  47.     draw();
  48.     setTimeout(gameLoop, 100);
  49. }

  50. function draw() {
  51.     ctx.clearRect(0, 0, canvas.width, canvas.height);

  52.     // Draw snake
  53.     for (let segment of snake) {
  54.         ctx.fillStyle = 'lime';
  55.         ctx.fillRect(segment.x * tileSize, segment.y * tileSize, tileSize, tileSize);
  56.     }

  57.     // Draw apple
  58.     ctx.fillStyle = 'red';
  59.     ctx.fillRect(apple.x * tileSize, apple.y * tileSize, tileSize, tileSize);

  60.     // Draw score
  61.     ctx.fillStyle = 'white';
  62.     ctx.font = '20px Arial';
  63.     ctx.fillText(`Score: ${score}`, 10, 20);
  64. }

  65. window.onload = () => {
  66.     canvas.width = tileSize * tileCount;
  67.     canvas.height = tileSize * tileCount;
  68.     gameLoop();
  69. };
复制代码


将以上内容分别保存为 `index.html`、`styles.css` 和 `script.js` 文件,并将它们放在同一目录下。然后打开 `index.html` 即可在浏览器中运行这个简单的贪吃蛇游戏。希望你喜欢!
回复 支持 反对

使用道具 举报

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

本版积分规则

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