飞机大战-3

⑤游戏进行阶段
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);
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值