⑤游戏进行阶段
2.绘制敌方飞机
分别创建小飞机,中飞机,大飞机,并给出其基本信息
// 3.3 敌方飞机
// 3.3.1 小号飞机
var enemy1Arr = [
"img/enemy1.png",
"img/enemy1_down1.png",
"img/enemy1_down2.png",
"img/enemy1_down3.png",
"img/enemy1_down4.png",
];
var enemyPlane = [];
for (var i = 0; i < enemy1Arr.length; i++) {
enemyPlane[i] = new Image();
enemyPlane[i].src = enemy1Arr[i];
}
// 小号飞机信息
var enemy1 = {
enemyPlane: enemyPlane,
width: 57,
height: 51,
life: 3,
score: 1,
};
// 3.3.2 中号飞机
var enemy2Arr = [
"img/enemy2.png",
"img/enemy2_down1.png",
"img/enemy2_down2.png",
"img/enemy2_down3.png",
"img/enemy2_down4.png",
];
var enemyPlane = [];
for (var i = 0; i < enemy1Arr.length; i++) {
enemyPlane[i] = new Image();
enemyPlane[i].src = enemy1Arr[i];
}
// 中号飞机信息
var enemy2 = {
enemyPlane: enemyPlane,
width: 69,
height: 95,
life: 3,
score: 3,
};
// 3.3.2 大号飞机
var enemy1Arr = [
"img/enemy3_n1.png",
"img/enemy3_n2.png",
"img/enemy3_hit.png",
"img/enemy3_down1.png",
"img/enemy3_down2.png",
"img/enemy3_down3.png",
"img/enemy3_down4.png",
"img/enemy3_down5.png",
"img/enemy3_down6.png",
];
var enemyPlane = [];
for (var i = 0; i < enemy1Arr.length; i++) {
enemyPlane[i] = new Image();
enemyPlane[i].src = enemy1Arr[i];
}
// 大号飞机信息
var enemy3 = {
enemyPlane: enemyPlane,
width: 166,
height: 261,
life: 3,
score: 5,
};
判断敌机与子弹是否碰撞,是否死亡
// 敌机的构造函数
function Enemy(param) {
this.enemyPlane = param.enemyPlane;
this.width = param.width;
this.height = param.height;
this.life = param.life;
this.score = param.score;
this.x = Math.random() * (width - this.width);
this.y = -this.height;
this.index = 0;
// 判断敌机是否发生碰撞
this.down = false;
// 是否爆炸完成
this.bang = false;
this.paint = function () {
ctx.drawImage(
this.enemyPlane[this.index],
this.x,
this.y,
this.width,
this.height
);
};
this.sport = function () {
if (!this.down) {
// 当前敌机未被碰撞时
this.y += 10;
} else {
this.index++;
if (this.index === this.enemyPlane.length) {
this.index = this.enemyPlane.length - 1;
this.bang = true;
}
}
};
// 判断是否被碰撞
this.hit = function (obj) {
return (
obj.x + obj.width >= this.x &&
obj.x <= this.x + this.width &&
obj.y <= this.y + this.height &&
obj.y + obj.height >= this.y
);
};
}
//
var enemies = [];
function pushEnemy() {
var random = Math.random();
if (random < 0.5) {
enemies.push(new Enemy(enemy1));
} else if (random < 0.8) {
// 中号飞机
enemies.push(new Enemy(enemy2));
} else {
// 大号飞机
enemies.push(new Enemy(enemy3));
}
}
// 绘制、运动飞机对象
function enemyPaint() {
for (var i = 0; i < enemies.length; i++) {
enemies[i].paint();
enemies[i].sport();
}
}
function enemyStop() {
for (var i = 0; i < enemies.length; i++) {
enemies[i].paint();
}
}
function enemyDelete() {
for (var i = 0; i < enemies.length; i++) {
if (enemies[i].bang) {
score += +enemies[i].score;
}
if (enemies[i].y >= height || enemies[i].bang) {
enemies.splice(i, 1);
}
}
}
// 如何检测每一个敌机是否被(每一个子弹 hero)碰撞
function checkHit() {
for (var i = 0; i < enemies.length; i++) {
// 子弹和敌机撞击
for (var j = 0; j < bullets.length; j++) {
if (enemies[i].hit(bullets[j])) {
enemies[i].down = true;
bullets[j].down = true;
}
}
// 敌机和hero
if (enemies[i].hit(heroObj)) {
enemies[i].down = true;
heroObj.down = true;
}
}
}
对分数和生命值进行实时更新
// 2. 对分数更新
function score1() {
ctx.textBaseline = "top";
ctx.font = "50px 微软雅黑 bold ";
ctx.textAlign = "left";
ctx.fillText("score:" + score, 12, 12);
}
// 3. 生命值更新
function life1() {
ctx.textAlign = "right";
ctx.fillText("life: " + life, 400, 12);
}