生活在JavaScript游戏中

时间:2018-12-12 21:16:34

标签: javascript html

我正在用html制作游戏,以获得一些练习编码。一架飞机沿其x和y轴移动,而三个敌人则上下移动。您可以射击并杀死它们,如果与它们碰撞3次,则会崩溃。我实施了一个生活系统,如果您一次又一次地碰撞,就会伤心,而如果第三次碰撞则碰撞。这是我想要的,但是只有一个敌人,其余的代码几乎相同。与另外两个一起,您将失去全部生命,并在第一次碰撞时坠毁。

function fnCrash() {
  var rect5 = {
    //plane
    x: 1 * imgPic[1].dataset.x,
    y: 1 * imgPic[1].dataset.y,
    width: imgPic[1].width,
    height: imgPic[1].width
  };

  var rect2 = {
    //purple enemy
    x: 1 * imgPic[3].dataset.x,
    y: 1 * imgPic[3].dataset.y,
    width: imgPic[3].width,
    height: imgPic[3].width
  };

  var rect3 = {
    //green enemy
    x: 1 * imgPic[2].dataset.x,
    y: 1 * imgPic[2].dataset.y,
    width: imgPic[2].width,
    height: imgPic[2].width
  };

  var rect4 = {
    //red enemy
    x: 1 * imgPic[4].dataset.x,
    y: 1 * imgPic[4].dataset.y,
    width: imgPic[4].width,
    height: imgPic[4].width
  };

  if (
    rect5.x < rect2.x + rect2.width - 30 &&
    rect5.x + rect5.width - 20 > rect2.x &&
    rect5.y < rect2.y + rect2.height - 10 &&
    rect5.height - 100 + rect5.y > rect2.y
  ) {
    fnDebugMessage("plane crashed");

    bPurpleEnemy = false;

    iLives = iLives - 1;

    imgPic[9].dataset.x = rect2.x;
    imgPic[9].dataset.y = rect2.y;

    bExplode2 = true;

    setTimeout(function() {
      bExplode2 = false;
    }, 100);

    if (iLives == 2) {
      bHeartC = false;
    }
    if (iLives == 1) {
      bHeartB = false;
      bHeartC = false;
    }

    if (iLives == 0) {
      imgPic[9].dataset.x = rect5.x;
      imgPic[9].dataset.y = rect5.y;

      bHeartA = false;
      bHeartB = false;
      bHeartC = false;

      bExplode2 = true;
      bCrash = true;

      setTimeout(function() {
        bExplode2 = false;
      }, 300);
    }
  }

  if (
    rect5.x < rect3.x + rect3.width - 30 &&
    rect5.x + rect5.width - 20 > rect3.x &&
    rect5.y < rect3.y + rect3.height - 10 &&
    rect5.height - 110 + rect5.y > rect3.y
  ) {
    fnDebugMessage("plane crashed");

    bGreenEnemy = false;

    iLives = iLives - 1;

    imgPic[9].dataset.x = rect3.x;
    imgPic[9].dataset.y = rect3.y;

    bExplode2 = true;

    setTimeout(function() {
      bExplode2 = false;
    }, 100);

    if (iLives == 2) {
      bHeartC = false;
    }
    if (iLives == 1) {
      bHeartB = false;
      bHeartC = false;
    }

    if (iLives == 0) {
      imgPic[9].dataset.x = rect5.x;
      imgPic[9].dataset.y = rect5.y;

      bHeartA = false;
      bHeartB = false;
      bHeartC = false;

      bExplode2 = true;
      bCrash = true;

      setTimeout(function() {
        bExplode2 = false;
      }, 300);
    }
  }

  if (
    rect5.x < rect4.x + rect4.width - 60 &&
    rect5.x + rect5.width - 40 > rect4.x &&
    rect5.y < rect4.y + rect4.height - 115 &&
    rect5.height - 80 + rect5.y > rect4.y
  ) {
    fnDebugMessage("plane crashed");

    bRedEnemy = false;

    iLives = iLives - 1;

    imgPic[9].dataset.x = rect4.x;
    imgPic[9].dataset.y = rect4.y;

    bExplode2 = true;

    setTimeout(function() {
      bExplode2 = false;
    }, 100);

    if (iLives == 2) {
      bHeartC = false;
    }
    if (iLives == 1) {
      bHeartB = false;
      bHeartC = false;
    }

    if (iLives == 0) {
      imgPic[9].dataset.x = rect5.x;
      imgPic[9].dataset.y = rect5.y;

      bHeartA = false;
      bHeartB = false;
      bHeartC = false;

      bExplode2 = true;
      bCrash = true;

      setTimeout(function() {
        bExplode2 = false;
      }, 300);
    }
  }
}

只有最后一个作品,红色敌人(Rect4)

谢谢

0 个答案:

没有答案