写一个 js 的飞机大战
- User
写一个 js 的飞机大战
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
写一个 js 的飞机大战
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
下面是一个用 JavaScript 编写的简单“飞机大战”小游戏示例,使用 HTML5 Canvas 实现。这个游戏包括玩家控制的飞机、敌机生成、子弹发射和简单的碰撞检测。
你可以将以下代码保存为 index.html 并在浏览器中打开运行。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>飞机大战 - JS版</title>
<style>
canvas {
background: #000;
display: block;
margin: 20px auto;
}
body {
text-align: center;
color: white;
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>飞机大战</h1>
<p>← → 移动 | 空格 发射</p>
<canvas id="gameCanvas" width="480" height="640"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// 玩家飞机对象
const player = {
x: canvas.width / 2 - 20,
y: canvas.height - 60,
width: 40,
height: 40,
speed: 5,
color: 'lime'
};
let keys = {};
let bullets = [];
let enemies = [];
let score = 0;
let gameOver = false;
// 监听按键
document.addEventListener('keydown', e => {
keys[e.code] = true;
});
document.addEventListener('keyup', e => {
keys[e.code] = false;
});
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
}
function drawBullets() {
ctx.fillStyle = 'yellow';
bullets.forEach((b, i) => {
b.y -= b.speed;
ctx.fillRect(b.x, b.y, b.width, b.height);
// 移除出界子弹
if (b.y < 0) bullets.splice(i, 1);
});
}
function drawEnemies() {
ctx.fillStyle = 'red';
enemies.forEach((e, ei) => {
e.y += e.speed;
ctx.fillRect(e.x, e.y, e.size, e.size);
// 判断是否撞到玩家
if (collision(e, player)) {
gameOver = true;
}
// 判断是否被子弹击中
bullets.forEach((b, bi) => {
if (collision(e, b)) {
enemies.splice(ei, 1);
bullets.splice(bi, 1);
score++;
}
});
// 敌人掉出屏幕外
if (e.y > canvas.height) {
enemies.splice(ei, 1);
}
});
}
function collision(a, b) {
return (
a.x < b.x + b.width &&
a.x + a.size > b.x &&
a.y < b.y + b.height &&
a.y + a.size > b.y
);
}
function spawnEnemy() {
const size = 30;
const x = Math.random() * (canvas.width - size);
enemies.push({
x: x,
y: -size,
size: size,
speed: 2 + Math.random() * 2,
color: 'red'
});
}
function update() {
if (gameOver) {
ctx.fillStyle = 'white';
ctx.font = '30px Arial';
ctx.fillText('Game Over!', 150, canvas.height / 2);
ctx.fillText(`Score: ${score}`, 170, canvas.height / 2 + 40);
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 控制玩家移动
if (keys['ArrowLeft'] && player.x > 0) {
player.x -= player.speed;
}
if (keys['ArrowRight'] && player.x < canvas.width - player.width) {
player.x += player.speed;
}
// 射击逻辑
if (keys['Space']) {
if (bullets.length < 1 || bullets[bullets.length - 1].y < player.y - 20) {
bullets.push({
x: player.x + player.width / 2 - 2,
y: player.y,
width: 4,
height: 10,
speed: 7
});
}
}
drawPlayer();
drawBullets();
drawEnemies();
// 生成敌人
if (Math.random() < 0.02) {
spawnEnemy();
}
// 显示分数
ctx.fillStyle = 'white';
ctx.font = '16px Arial';
ctx.fillText('Score: ' + score, 10, 20);
requestAnimationFrame(update);
}
update();
</script>
</body>
</html>
你可以在此基础上继续扩展,例如:
如果你希望我帮你升级这个版本(比如加入精灵图、面向对象结构、状态管理等),也可以告诉我 😊
祝你开发愉快!✈️💥