科学教育

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

帮我生成一个在果园里种东西的网页游戏

[复制链接]

3

主题

7

帖子

31

积分

新手上路

Rank: 1

积分
31
跳转到指定楼层
楼主
发表于 2025-12-24 15:20:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我生成一个在果园里种东西的网页游戏

回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-12-24 15:20:37 | 只看该作者
好的!我来帮你做一个简单的“在果园里种东西”的网页小游戏。这个游戏会有一个果园的格子,你可以点击格子,然后选择种苹果树、橙子树或者葡萄藤,种下去后它们会慢慢长大。

下面是小游戏的完整代码,你可以直接复制到一个`.html`文件里,用浏览器打开就能玩啦!

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.   <meta charset="UTF-8" />
  5.   <title>果园种植小游戏</title>
  6.   <style>
  7.     body {
  8.       font-family: "微软雅黑", sans-serif;
  9.       background-color: #e0f7fa;
  10.       display: flex;
  11.       flex-direction: column;
  12.       align-items: center;
  13.       margin: 20px;
  14.     }
  15.     h1 {
  16.       color: #00796b;
  17.     }
  18.     #garden {
  19.       display: grid;
  20.       grid-template-columns: repeat(5, 80px);
  21.       grid-gap: 10px;
  22.       margin-top: 20px;
  23.     }
  24.     .plot {
  25.       width: 80px;
  26.       height: 80px;
  27.       background-color: #a5d6a7;
  28.       border: 2px solid #4caf50;
  29.       border-radius: 8px;
  30.       display: flex;
  31.       justify-content: center;
  32.       align-items: center;
  33.       cursor: pointer;
  34.       position: relative;
  35.       font-weight: bold;
  36.       color: #2e7d32;
  37.       user-select: none;
  38.     }
  39.     .tree-icon {
  40.       font-size: 32px;
  41.       pointer-events: none;
  42.       transition: transform 1s linear;
  43.     }
  44.     #plantMenu {
  45.       margin-top: 15px;
  46.       display: flex;
  47.       gap: 15px;
  48.     }
  49.     .plantBtn {
  50.       padding: 8px 15px;
  51.       background-color: #81c784;
  52.       border: none;
  53.       border-radius: 6px;
  54.       color: white;
  55.       font-size: 16px;
  56.       cursor: pointer;
  57.       user-select: none;
  58.       transition: background-color 0.3s;
  59.     }
  60.     .plantBtn:hover {
  61.       background-color: #4caf50;
  62.     }
  63.     #info {
  64.       margin-top: 15px;
  65.       min-height: 24px;
  66.       color: #00796b;
  67.       font-weight: bold;
  68.     }
  69.   </style>
  70. </head>
  71. <body>
  72.   <h1>果园种植小游戏</h1>
  73.   <div id="info">点击格子来选择要种植的位置</div>
  74.   <div id="garden"></div>

  75.   <div id="plantMenu" style="display:none;">
  76.     <button class="plantBtn" data-plant="apple">种苹果树 ????</button>
  77.     <button class="plantBtn" data-plant="orange">种橙子树 ????</button>
  78.     <button class="plantBtn" data-plant="grape">种葡萄藤 ????</button>
  79.     <button class="plantBtn" id="cancelBtn" style="background-color:#e57373;">取消</button>
  80.   </div>

  81.   <script>
  82.     const garden = document.getElementById('garden');
  83.     const plantMenu = document.getElementById('plantMenu');
  84.     const info = document.getElementById('info');
  85.     let selectedPlot = null;

  86.     // 创建5x5共25个格子
  87.     const plotCount = 25;
  88.     const plots = [];

  89.     for (let i = 0; i < plotCount; i++) {
  90.       const plot = document.createElement('div');
  91.       plot.classList.add('plot');
  92.       plot.dataset.index = i;
  93.       plot.textContent = '空地';
  94.       garden.appendChild(plot);
  95.       plots.push({
  96.         element: plot,
  97.         plant: null,
  98.         growthStage: 0,
  99.         growTimer: null,
  100.       });
  101.     }

  102.     function showPlantMenu(plot) {
  103.       selectedPlot = plot;
  104.       plantMenu.style.display = 'flex';
  105.       info.textContent = '请选择要种的植物';
  106.     }

  107.     function hidePlantMenu() {
  108.       plantMenu.style.display = 'none';
  109.       selectedPlot = null;
  110.       info.textContent = '点击格子来选择要种植的位置';
  111.     }

  112.     // 植物的表情符号和名称
  113.     const plantTypes = {
  114.       apple: {name: '苹果树', emoji: '????', maxStage: 3},
  115.       orange: {name: '橙子树', emoji: '????', maxStage: 3},
  116.       grape: {name: '葡萄藤', emoji: '????', maxStage: 3},
  117.     };

  118.     // 点击格子,显示选择植物菜单,但如果已经种了植物,不能再种
  119.     garden.addEventListener('click', (e) => {
  120.       if (!e.target.classList.contains('plot')) return;
  121.       const index = e.target.dataset.index;
  122.       const plotObj = plots[index];
  123.       if (plotObj.plant !== null) {
  124.         info.textContent = `这里已经种了${plantTypes[plotObj.plant].name}哦!`;
  125.         plantMenu.style.display = 'none';
  126.         return;
  127.       }
  128.       showPlantMenu(plotObj);
  129.     });

  130.     // 点击选择植物按钮,开始种植
  131.     plantMenu.addEventListener('click', (e) => {
  132.       if (!e.target.classList.contains('plantBtn')) return;
  133.       const plant = e.target.dataset.plant;
  134.       if (plant === undefined) {
  135.         // 取消按钮
  136.         hidePlantMenu();
  137.         return;
  138.       }
  139.       if (selectedPlot) {
  140.         plantTree(selectedPlot, plant);
  141.         hidePlantMenu();
  142.       }
  143.     });

  144.     // 种植函数,会显示植物,慢慢长大
  145.     function plantTree(plotObj, plant) {
  146.       plotObj.plant = plant;
  147.       plotObj.growthStage = 1;
  148.       updatePlotDisplay(plotObj);

  149.       // 每2秒生长一级,直到最大阶段停止
  150.       plotObj.growTimer = setInterval(() => {
  151.         plotObj.growthStage++;
  152.         if (plotObj.growthStage > plantTypes[plant].maxStage) {
  153.           clearInterval(plotObj.growTimer);
  154.           plotObj.growTimer = null;
  155.           info.textContent = `${plantTypes[plant].name}长成了,快来收获吧!`;
  156.         } else {
  157.           updatePlotDisplay(plotObj);
  158.         }
  159.       }, 2000);

  160.       info.textContent = `种下了${plantTypes[plant].name},正在生长中...`;
  161.     }

  162.     // 根据生长阶段更新格子显示
  163.     function updatePlotDisplay(plotObj) {
  164.       const {element, plant, growthStage} = plotObj;
  165.       if (plant === null) {
  166.         element.textContent = '空地';
  167.         element.style.color = '#2e7d32';
  168.         element.style.backgroundColor = '#a5d6a7';
  169.         element.style.fontSize = '16px';
  170.         element.innerHTML = '空地';
  171.       } else {
  172.         // 显示植物emoji,越大阶段字体越大
  173.         let size = 24 + growthStage * 8;
  174.         element.style.fontSize = size + 'px';
  175.         element.style.color = '#4e342e';
  176.         element.style.backgroundColor = '#c8e6c9';
  177.         element.innerHTML = plantTypes[plant].emoji;

  178.         // 长成后加点动画
  179.         if (growthStage === plantTypes[plant].maxStage) {
  180.           element.style.animation = 'swing 1s infinite alternate';
  181.         } else {
  182.           element.style.animation = '';
  183.         }
  184.       }
  185.     }

  186.     // 大摇摆动画的CSS
  187.     const style = document.createElement('style');
  188.     style.textContent = `
  189.     @keyframes swing {
  190.       0% { transform: rotate(-5deg); }
  191.       100% { transform: rotate(5deg); }
  192.     }
  193.     `;
  194.     document.head.appendChild(style);

  195.     // 初始化格子显示
  196.     plots.forEach(updatePlotDisplay);
  197.   </script>
  198. </body>
  199. </html>
复制代码


### 这个小游戏怎么玩?
1. 打开网页后,会看到25个绿色格子(果园土地)。
2. 点击一个空地格子,下面会出现种植物的菜单。
3. 选择苹果树????、橙子树????或葡萄藤????,就会在格子里出现对应的植物。
4. 植物会慢慢“长大”,从小变大,等长大了会摇摆表示成熟啦。
5. 你可以继续点击其他空地种更多植物。

如果你想增加功能,比如收获水果、浇水、卖水果之类,我们以后也可以一起做哦!

你试试看这个代码,遇到问题告诉我,我可以帮你修改和解释!????
回复 支持 反对

使用道具 举报

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

本版积分规则

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