用js写一个飞机大战游戏
时间: 2023-12-22 07:05:52 浏览: 79
好的,下面是一个简单的飞机大战游戏的代码示例:
HTML
```html
<!DOCTYPE html>
<html>
<head>
<title>飞机大战</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
background-color: #000;
}
canvas {
display: block;
margin: 0 auto;
background-color: #222;
}
</style>
</head>
<body>
<canvas id="canvas" width="600" height="600"></canvas>
<script src="game.js"></script>
</body>
</html>
```
JavaScript
```javascript
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
// 背景图片
var bgImage = new Image();
bgImage.src = "images/bg.png";
// 玩家飞机图片
var playerImage = new Image();
playerImage.src = "images/player.png";
// 敌机图片
var enemyImage = new Image();
enemyImage.src = "images/enemy.png";
// 子弹图片
var bulletImage = new Image();
bulletImage.src = "images/bullet.png";
// 音效
var shootSound = new Audio("sounds/shoot.mp3");
var explodeSound = new Audio("sounds/explode.mp3");
// 玩家飞机
var player = {
x: 250,
y: 500,
speed: 5,
image: playerImage,
width: 50,
height: 50,
bullets: []
};
// 敌机
var enemies = [];
function createEnemy() {
var enemy = {
x: Math.random() * (canvas.width - 50),
y: 0,
speed: Math.random() * 3 + 1,
image: enemyImage,
width: 50,
height: 50
};
enemies.push(enemy);
}
// 子弹
function createBullet() {
var bullet = {
x: player.x + player.width / 2 - 5,
y: player.y - 10,
speed: 10,
image: bulletImage,
width: 10,
height: 20
};
player.bullets.push(bullet);
shootSound.currentTime = 0;
shootSound.play();
}
// 更新游戏状态
function update() {
// 移动玩家飞机
if (keys[37] && player.x > 0) { // 左箭头
player.x -= player.speed;
}
if (keys[39] && player.x < canvas.width - player.width) { // 右箭头
player.x += player.speed;
}
if (keys[32]) { // 空格键
createBullet();
}
// 移动敌机
for (var i = 0; i < enemies.length; i++) {
enemies[i].y += enemies[i].speed;
// 判断敌机是否被击中
for (var j = 0; j < player.bullets.length; j++) {
if (collides(player.bullets[j], enemies[i])) {
explodeSound.currentTime = 0;
explodeSound.play();
enemies.splice(i, 1);
player.bullets.splice(j, 1);
break;
}
}
// 判断敌机是否撞到玩家飞机
if (collides(enemies[i], player)) {
alert("游戏结束!");
location.reload();
break;
}
}
// 移动子弹
for (var i = 0; i < player.bullets.length; i++) {
player.bullets[i].y -= player.bullets[i].speed;
// 删除超出屏幕的子弹
if (player.bullets[i].y < 0) {
player.bullets.splice(i, 1);
i--;
}
}
// 创建新敌机
if (Math.random() < 0.02) {
createEnemy();
}
}
// 绘制游戏画面
function draw() {
// 绘制背景图片
ctx.drawImage(bgImage, 0, 0);
// 绘制玩家飞机
ctx.drawImage(player.image, player.x, player.y, player.width, player.height);
// 绘制敌机
for (var i = 0; i < enemies.length; i++) {
ctx.drawImage(enemies[i].image, enemies[i].x, enemies[i].y, enemies[i].width, enemies[i].height);
}
// 绘制子弹
for (var i = 0; i < player.bullets.length; i++) {
ctx.drawImage(player.bullets[i].image, player.bullets[i].x, player.bullets[i].y, player.bullets[i].width, player.bullets[i].height);
}
}
// 碰撞检测
function collides(a, b) {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}
// 键盘控制
var keys = {};
window.addEventListener("keydown", function(event) {
keys[event.keyCode] = true;
});
window.addEventListener("keyup", function(event) {
delete keys[event.keyCode];
});
// 游戏循环
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
gameLoop();
```
这个游戏是基于HTML5 Canvas和JavaScript实现的,通过键盘控制玩家飞机移动和发射子弹,击落敌机得分,同时要避免敌机的攻击和碰撞。可以根据自己的需求进行修改和扩展。
阅读全文