科学教育

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

111111111111111111111111111111111111111111111111111111111111111

[复制链接]

7

主题

25

帖子

83

积分

注册会员

Rank: 2

积分
83
跳转到指定楼层
楼主
发表于 2026-1-6 09:55:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  • <!DOCTYPE html>
  • <html lang="zh">
  • <head>
  • <meta charset="UTF-8" />
  • <title>打鬼子小游戏</title>
  • <style>
  •   body {
  •     background-color: #f0f0f0;
  •     text-align: center;
  •     font-family: "微软雅黑", sans-serif;
  •   }
  •   #gameArea {
  •     position: relative;
  •     width: 600px;
  •     height: 400px;
  •     background: #a3d9ff;
  •     margin: 20px auto;
  •     border: 3px solid #333;
  •     overflow: hidden;
  •   }
  •   .ghost {
  •     position: absolute;
  •     bottom: 10px;
  •     width: 60px;
  •     height: 60px;
  •     background-color: red;
  •     border-radius: 15px;
  •     line-height: 60px;
  •     color: white;
  •     font-weight: bold;
  •     user-select: none;
  •     cursor: default;
  •   }
  •   #scoreBoard {
  •     font-size: 20px;
  •     margin-top: 10px;
  •   }
  •   #life {
  •     font-size: 18px;
  •     margin-top: 5px;
  •   }
  •   #message {
  •     color: green;
  •     font-weight: bold;
  •     margin-top: 10px;
  •     height: 24px;
  •   }
  • </style>
  • </head>
  • <body>
  • <h1>打鬼子小游戏</h1>
  • <div id="gameArea"></div>
  • <div id="scoreBoard">积分:0</div>
  • <div id="life">生命:3</div>
  • <div id="message"></div>
  • <script>
  •   const gameArea = document.getElementById('gameArea');
  •   const scoreBoard = document.getElementById('scoreBoard');
  •   const lifeDisplay = document.getElementById('life');
  •   const message = document.getElementById('message');
  •   let score = 0;
  •   let life = 3;
  •   // 鬼子类型,不同颜色代表难度(简单,中等,困难)
  •   const ghostTypes = [
  •     {name: '简单鬼子', color: '#44aa44', difficulty: 1, basePoint: 10},
  •     {name: '中等鬼子', color: '#ffaa00', difficulty: 2, basePoint: 20},
  •     {name: '困难鬼子', color: '#cc0000', difficulty: 3, basePoint: 40}
  •   ];
  •   let ghosts = []; // 存放当前出现的鬼子对象
  •   // 随机生成鬼子
  •   function createGhost() {
  •     if (life <= 0) return;
  •     // 随机选择鬼子类型(难度越大概率越小)
  •     const probability = Math.random();
  •     let typeIndex;
  •     if (probability < 0.6) typeIndex = 0;      // 60%简单
  •     else if (probability < 0.9) typeIndex = 1; // 30%中等
  •     else typeIndex = 2;                       // 10%困难
  •     const type = ghostTypes[typeIndex];
  •     // 创建鬼子div
  •     const ghostDiv = document.createElement('div');
  •     ghostDiv.classList.add('ghost');
  •     ghostDiv.style.backgroundColor = type.color;
  •     ghostDiv.textContent = type.name[0]; // 显示第一个字方便识别
  •     // 随机水平位置在游戏区宽度内
  •     const maxX = gameArea.clientWidth - 60;
  •     const xPos = Math.floor(Math.random() * maxX);
  •     ghostDiv.style.left = xPos + 'px';
  •     ghostDiv.style.bottom = '10px';
  •     gameArea.appendChild(ghostDiv);
  •     // 鬼子属性
  •     const ghost = {
  •       element: ghostDiv,
  •       type: type,
  •       positionX: xPos,
  •       bottom: 10,
  •       alive: true,
  •       lifePoints: type.difficulty, // 需要几次攻击才能打死
  •       // 死得“惨”的程度,用不同的死法加分
  •       deathStyle: null
  •     };
  •     ghosts.push(ghost);
  •   }
  •   // 鬼子移动,向上漂浮并慢慢消失
  •   function moveGhosts() {
  •     ghosts.forEach((ghost, index) => {
  •       if (!ghost.alive) return;
  •       ghost.bottom += 1; // 每次上升1px
  •       if (ghost.bottom > gameArea.clientHeight) {
  •         // 鬼子逃走了,少一条命
  •         removeGhost(index);
  •         loseLife();
  •         message.textContent = "鬼子跑掉了,扣一条命!";
  •       } else {
  •         ghost.element.style.bottom = ghost.bottom + 'px';
  •       }
  •     });
  •   }
  •   // 移除鬼子
  •   function removeGhost(index) {
  •     if (ghosts[index]) {
  •       gameArea.removeChild(ghosts[index].element);
  •       ghosts.splice(index, 1);
  •     }
  •   }
  •   // 失去生命
  •   function loseLife() {
  •     life--;
  •     updateLife();
  •     if (life <= 0) {
  •       message.textContent = "游戏结束!你的积分:" + score;
  •       clearInterval(gameLoop);
  •       clearInterval(ghostCreator);
  •     }
  •   }
  •   // 更新生命显示
  •   function updateLife() {
  •     lifeDisplay.textContent = "生命:" + life;
  •   }
  •   // 更新积分显示
  •   function updateScore() {
  •     scoreBoard.textContent = "积分:" + score;
  •   }
  •   // 计算打到鬼子的积分
  •   function calculatePoints(ghost, attackType) {
  •     // attackType是踢、踩、打
  •     // 打得越惨积分越多,踢最高30%,踩次之,打基础
  •     let multiplier = 1;
  •     if (attackType === 'kick') multiplier = 1.3;
  •     else if (attackType === 'step') multiplier = 1.2;
  •     else multiplier = 1;
  •     return Math.floor(ghost.type.basePoint * multiplier);
  •   }
  •   // 玩家攻击操作,参数是按什么键触发
  •   function playerAttack(key) {
  •     if (life <= 0) return; // 游戏结束不处理
  •     if (ghosts.length === 0) {
  •       message.textContent = "没有鬼子可以攻击!";
  •       return;
  •     }
  •     let attackType = null;
  •     if (key === 'ArrowUp') attackType = 'kick';   // 上箭头 踢鬼子
  •     else if (key === 'ArrowDown') attackType = 'step'; // 下箭头 踩鬼子
  •     else if (key === 'ArrowLeft' || key === 'ArrowRight') attackType = 'hit'; // 左右箭头 打鬼子
  •     else return;
  •     // 攻击第一个鬼子(最先出现的)
  •     const ghost = ghosts[0];
  •     ghost.lifePoints--;
  •     if (ghost.lifePoints <= 0) {
  •       // 鬼子死了,获得积分
  •       const gained = calculatePoints(ghost, attackType);
  •       score += gained;
  •       updateScore();
  •       // 死得惨的提示
  •       if (attackType === 'kick') {
  •         message.textContent = "你踢死了" + ghost.type.name + "!得+" + gained + "分";
  •       } else if (attackType === 'step') {
  •         message.textContent = "你踩死了" + ghost.type.name + "!得+" + gained + "分";
  •       } else {
  •         message.textContent = "你打死了" + ghost.type.name + "!得+" + gained + "分";
  •       }
  •       // 移除鬼子
  •       const index = ghosts.indexOf(ghost);
  •       if (index >= 0) removeGhost(index);
  •     } else {
  •       message.textContent = "你" + (attackType === 'kick' ? '踢' : attackType === 'step' ? '踩' : '打') + "中了" + ghost.type.name + ",还需要" + ghost.lifePoints + "下";
  •     }
  •   }
  •   // 游戏循环,每50毫秒移动鬼子
  •   const gameLoop = setInterval(() => {
  •     moveGhosts();
  •   }, 50);
  •   // 每2秒产生新鬼子
  •   const ghostCreator = setInterval(() => {
  •     createGhost();
  •   }, 2000);
  •   // 监听键盘事件
  •   window.addEventListener('keydown', e => {
  •     playerAttack(e.key);
  •   });
  •   updateLife();
  •   updateScore();
  • </script>
  • </body>
  • </html>


把人改成人的形状,鬼子是鬼子的形状,点空格键是重来。

[color=rgb(51, 102, 153) !important]


回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2026-1-6 09:56:05 | 只看该作者
你好!你想让“打鬼子小游戏”里的人物变成人的形状,鬼子变成鬼子的形状,还能用空格键重新开始游戏,是吧?我来帮你一步步讲怎么做,保证简单又好懂!

---

### 1. 把人改成人的形状

原代码里鬼子是用一个红色的方块表示的,我们可以用图片或者用简单的图形代替。

**方法一:用图片显示人**

- 准备一张人的图片(比如`human.png`),然后在人物出现的位置放这张图片。

**方法二:用CSS画一个简单的人形**

比如做一个圆头加身体的小人:

  1. .person {
  2.   position: absolute;
  3.   bottom: 10px;
  4.   width: 40px;
  5.   height: 80px;
  6.   cursor: default;
  7. }
  8. .person .head {
  9.   width: 40px;
  10.   height: 40px;
  11.   background-color: #ffcc99;
  12.   border-radius: 50%; /* 圆头 */
  13. }
  14. .person .body {
  15.   width: 30px;
  16.   height: 40px;
  17.   background-color: #3366cc;
  18.   margin: 0 auto;
  19.   border-radius: 10px;
  20. }
复制代码


**用法**:

  1. <div class="person">
  2.   <div class="head"></div>
  3.   <div class="body"></div>
  4. </div>
复制代码


这样就做出了一个简单的人。

---

### 2. 让鬼子变成鬼子的形状

同样,我们也可以用图片或CSS来设计鬼子的样子。

**用图片:**

比如准备一张“鬼子”的图片叫`ghost.png`,在创建鬼子时候替换颜色背景为图片:

  1. .ghost {
  2.   width: 60px;
  3.   height: 60px;
  4.   background-image: url('ghost.png');
  5.   background-size: cover;
  6.   border-radius: 15px;
  7.   position: absolute;
  8.   bottom: 10px;
  9.   cursor: pointer;
  10. }
复制代码


这样鬼子就显示成鬼子的样子了。

---

### 3. 空格键重来游戏

我们给页面添加键盘监听,按空格键时,重置分数和生命,清除场上所有鬼子,重新开始。

**示例代码:**

  1. window.addEventListener('keydown', e => {
  2.   if (e.code === 'Space') {  // 按空格键
  3.     resetGame();
  4.   } else {
  5.     playerAttack(e.key);
  6.   }
  7. });

  8. function resetGame() {
  9.   // 清除所有鬼子
  10.   ghosts.forEach(g => {
  11.     gameArea.removeChild(g.element);
  12.   });
  13.   ghosts = [];

  14.   score = 0;
  15.   life = 3;
  16.   updateScore();
  17.   updateLife();
  18.   message.textContent = "游戏已重置,开始吧!";

  19.   // 开始新游戏循环
  20.   clearInterval(gameInterval);
  21.   gameInterval = setInterval(createGhost, 2000);
  22. }
复制代码


> 注意:`gameInterval`是你用来不停生成鬼子的计时器,代码里要先把它定义为变量,比如`let gameInterval = setInterval(...);`

---

### 4. 小总结

- 用图片或者CSS画简单的“人”和“鬼子”
- 为空格键添加事件,可以重新开始游戏
- 注意动态创建和删除DOM元素,保持游戏流畅

---

如果你愿意,可以把你的完整代码发给我,我帮你写完具体代码示范哦!希望你玩得开心,网页编程其实很有趣,继续加油!????
回复 支持 反对

使用道具 举报

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

本版积分规则

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