科学教育
标题:
zhr飞坤
[打印本页]
作者:
金外章宏睿
时间:
2025-12-2 15:14
标题:
zhr飞坤
帮我生成一个网页游戏飞机大战
作者:
kaia
时间:
2025-12-2 15:15
好的!我来帮你做一个简单的“飞机大战”小游戏网页版本,好玩又容易理解。
### 游戏规则简介:
- 你控制一架飞机,可以用键盘左右移动。
- 上方会有敌机往下飞,你要躲避它们。
- 你可以发射子弹击中敌机,敌机被打中就会消失。
### 代码部分
你只需要新建一个HTML文件,比如叫 `feiji.html`,把下面代码复制进去,然后用浏览器打开就能玩啦!
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>飞机大战小游戏</title>
<style>
body {
text-align: center;
background-color: #000;
color: white;
font-family: "微软雅黑", sans-serif;
margin: 0;
}
canvas {
background: #111;
display: block;
margin: 20px auto;
border: 2px solid #555;
}
</style>
</head>
<body>
<h1>飞机大战小游戏</h1>
<p>左右方向键控制飞机移动,空格键发射子弹</p>
<canvas id="game" width="400" height="600"></canvas>
<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
// 飞机对象
const plane = {
x: canvas.width / 2 - 25,
y: canvas.height - 80,
width: 50,
height: 60,
speed: 5,
movingLeft: false,
movingRight: false,
};
// 子弹数组
let bullets = [];
// 敌机数组
let enemies = [];
// 分数
let score = 0;
// 创建敌机函数
function createEnemy() {
const enemyWidth = 40;
const enemyHeight = 40;
const x = Math.random() * (canvas.width - enemyWidth);
enemies.push({
x: x,
y: -enemyHeight,
width: enemyWidth,
height: enemyHeight,
speed: 2 + Math.random() * 2,
});
}
// 键盘事件监听
window.addEventListener('keydown', function(e) {
if (e.code === 'ArrowLeft') plane.movingLeft = true;
if (e.code === 'ArrowRight') plane.movingRight = true;
if (e.code === 'Space') shootBullet();
});
window.addEventListener('keyup', function(e) {
if (e.code === 'ArrowLeft') plane.movingLeft = false;
if (e.code === 'ArrowRight') plane.movingRight = false;
});
// 发射子弹函数
function shootBullet() {
bullets.push({
x: plane.x + plane.width / 2 - 5,
y: plane.y,
width: 10,
height: 20,
speed: 7,
});
}
// 检测碰撞函数(矩形碰撞)
function isColliding(obj1, obj2) {
return !(
obj1.x + obj1.width < obj2.x ||
obj1.x > obj2.x + obj2.width ||
obj1.y + obj1.height < obj2.y ||
obj1.y > obj2.y + obj2.height
);
}
// 游戏主循环
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 移动飞机
if (plane.movingLeft && plane.x > 0) plane.x -= plane.speed;
if (plane.movingRight && plane.x + plane.width < canvas.width) plane.x += plane.speed;
// 画飞机(用三角形表示)
ctx.fillStyle = 'cyan';
ctx.beginPath();
ctx.moveTo(plane.x + plane.width/2, plane.y); // 顶点
ctx.lineTo(plane.x, plane.y + plane.height);
ctx.lineTo(plane.x + plane.width, plane.y + plane.height);
ctx.closePath();
ctx.fill();
// 更新和画子弹
ctx.fillStyle = 'yellow';
for (let i = bullets.length -1; i >= 0; i--) {
bullets[i].y -= bullets[i].speed;
if (bullets[i].y + bullets[i].height < 0) bullets.splice(i, 1);
else {
ctx.fillRect(bullets[i].x, bullets[i].y, bullets[i].width, bullets[i].height);
}
}
// 每隔一段时间生成敌机
if (Math.random() < 0.02) {
createEnemy();
}
// 更新和画敌机
ctx.fillStyle = 'red';
for (let i = enemies.length -1; i >= 0; i--) {
enemies[i].y += enemies[i].speed;
if (enemies[i].y > canvas.height) {
enemies.splice(i, 1);
// 这里可以扣分或者游戏结束,不过为了简单我们忽略
}
else {
ctx.fillRect(enemies[i].x, enemies[i].y, enemies[i].width, enemies[i].height);
}
}
// 检测子弹和敌机的碰撞
for (let b = bullets.length -1; b >= 0; b--) {
for (let e = enemies.length -1; e >= 0; e--) {
if (isColliding(bullets[b], enemies[e])) {
bullets.splice(b, 1);
enemies.splice(e, 1);
score += 10;
break;
}
}
}
// 画分数
ctx.fillStyle = 'white';
ctx.font = '20px 微软雅黑';
ctx.fillText('分数: ' + score, 10, 30);
requestAnimationFrame(gameLoop);
}
// 开始游戏
gameLoop();
</script>
</body>
</html>
复制代码
---
### 怎么开始玩?
1. 新建一个文本文件,取名叫 `feiji.html`。
2. 把上面代码全部复制粘贴进去,保存。
3. 用浏览器打开这个 `feiji.html` 文件。
4. 按左右方向键控制飞机移动,按空格键发射子弹。
5. 击中红色敌机得分,试试看你能得多少分吧!
---
如果你想要,我可以告诉你怎么一步步改进游戏,添加更多什么功能,比如生命值、敌人不同速度、背景音乐等等。你有兴趣吗?????
欢迎光临 科学教育 (http://www.sci-edu.net/)
Powered by Discuz! X3.2