我怎样才能让一个随机的 space 入侵者射击?

How can I make only one random space invader shoot?

我一直在努力制作我自己的 space 入侵者版本,除了允许我的外星人射击外,我确实拥有一切正常,问题是现在我有一个 2D 外星人阵列(15 x 3)我想发射一枚导弹,每次发射我都希望它来自一个随机的外星人,而不是一次全部发射。

目前我的导弹是随机发射的,但所有的外星人同时发射,几乎无法玩游戏。

这是我绘制外星人和射击的代码片段:

for (int j = 0; j < 3; j++) { //Adds aliens on the screen (15 by 3)
  for (int i = 0; i < 15; i++) {
    if (alien[i][j] != null) {
      alien[i][j].updateAlien();
      if (alien[i][j].edgeDetection() == true) { //If the edge is hit 
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < 15; l++) {
            if (alien[l][k] != null) {
              alien[l][k].deltaX = - alien[l][k].deltaX; //the aliens move the opposite direction
              alien[l][k].y = alien[l][k].y + 25; //the aliens move down a little
            }
          }
        }
      }
      for (int k = 0; k < 3; k++) {
        for (int l = 0; l < 15; l++) {
          if (alien[l][k] != null) {
            if (alienMissileCounter % 75 == 0) { //If the random int is divisible by 75 then the Aliens shoot a missile
              missileAlien.add(new MissileAlien(alien[l][k].x, alien[l][k].y, 10));
            }
          }
        }
      }
    }
  }
}

我在我的导弹上添加了 [l][k] 位,以为只有一个会射击,但结果恰恰相反。

这里也是用于添加导弹数量的for循环:

    for (int m = missileAlien.size()-1; m >= 0; m--) { //This adds one missile each time
  MissileAlien missilesalien = missileAlien.get(m); //This gets the number of missile i.e. 1
  missilesalien.updateMissile(); //This draws and moves the missile

  if (missilesalien.hitDefender(player) == true) { //If the defender is hit it loses a life
    missileAlien.remove(m);
    lives -= 1;
  }
}

如果您需要我粘贴您认为可能有帮助的任何其他代码,我会非常乐意这样做,这没什么大不了的,因为我已经完成了游戏的其余部分,所以如果它不是无法解决那么不用担心。

更新:在这里发表评论后,我的代码现在是这样的:

    alienMissileCounter = int(random(0, 100));


//Adds Aliens
for (int j = 0; j < 3; j++) { //Adds aliens on the screen (15 by 3)
  for (int i = 0; i < 15; i++) {
    if (alien[i][j] != null) {
      alien[i][j].updateAlien();
      if (alien[i][j].edgeDetection() == true) { //If the edge is hit 
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < 15; l++) {
            if (alien[l][k] != null) {
              alien[l][k].deltaX = - alien[l][k].deltaX; //the aliens move the opposite direction
              alien[l][k].y = alien[l][k].y + 25; //the aliens move down a little
            }
          }
        }
      }
      for (int k = 0; k < 3; k++) {
        for (int l = 0; l < 15; l++) {
          if (alien[l][k] != null) {
            if (alienMissileCounter == 75) { //If the random int is divisible by 75 then the Aliens shoot a missile
              alienMissileCounter = 0;
              missileAlien.add(new MissileAlien(alien[l][k].x, alien[l][k].y, 10));
            }
          }
        }
      }
    }
  }
}

为什么不使用内置的 java 随机数生成器来激活具有随机索引值的入侵者。 如果您喜欢编码,请尝试使用任何随机数生成算法生成伪随机数,例如 线性结果法 中间平方法 。这些是基于简短数学公式的非常简单的算法。

您可以生成两个随机数:

int row = (int) random(numRows);
int col = (int) random(numCols);

但随后您需要检查该点是否为 null,然后重试 - 重复直到 rowcol 不是 null。但是,如果二维数组中没有外星人,则有进入无限循环的危险——或者需要 100 次尝试才能找到非 null 外星人(例如,45 个点中只剩下两个)。

如果您还没有完全适应二维数组数据结构,我建议您使用一种 Java 集合类型——也许是 ArrayList。最大的卖点是它们是动态数组。

你只是 .add() 所有外星人在前面,当他们被击中时 .remove() 他们(而不是将那个位置设置为 null)。您的 ArrayList 将准确包含屏幕上可见的外星人数量。然后随机挑选一个非常容易:

// Assuming:
//  your alien class is Alien
//  your list is created like:
ArrayList<Alien> aliens = new ArrayList<Alien>();
//  aliens are added at some point, like:
aliens.add( new Alien() );

// in your loop that wants to drop a missile:
if ( aliens.size() > 0 ) {
  int randomIndex = (int) random( aliens.size() );
  Alien theAlien = aliens.get( randomIndex );
  // drop a missile from `alien`
}

顺便说一句,你的绘制循环也会简单很多:

boolean hitEdge = false;
for ( Alien alien : aliens ) {
  alien.updateAlien();
  hitEdge = hitEdge || alien.edgeDetection();
}
if ( hitEdge ) {
  for ( Alien alien : aliens ) {
    // do your bounced-off-the-edge stuff here
  }
}
for (int k = 0; k < 3; k++) {
    for (int l = 0; l < 15; l++) {
        if (alien[l][k] != null) {
            if (alienMissileCounter == 75) { //If the random int is divisible by 75 then the Aliens shoot a missile
            //This area of code will only be reached if the initial value of 
            //alienMissileCounter is equal to 75 initially. If it is not, then no alien 
            //will have any missile. Also once this area is reached for the first time, 
            //only the first alien will shoot a missile and no subsequent alien can 
            //shoot a missile
                alienMissileCounter = 0;
                missileAlien.add(new MissileAlien(alien[l][k].x, alien[l][k].y, 10));
            }
        }
    }
}

在上面的代码中,您将遍历整个区域 (15*3) 并根据那里是否存在外星人来更新导弹。因此,每次您为该地区出现的每个外星人更新游戏时。所以你需要在循环内生成随机数,而不是像你所做的那样。我还在代码本身添加了一些注释

你应该在你的代码中使用这样的东西:

for (int k = 0; k < 3; k++) {
    for (int l = 0; l < 15; l++) {
        if (alien[l][k] != null) {
            alienMissileCounter = int(random(0, 100)); 
            //Every time an alien is detected, a new random number is 
            //generated. Now if it's equal to 75 as you want, this alien 
            //will shoot a missile otherwise not
            if (alienMissileCounter == 75) { //If the random int is divisible by 75 then the Aliens shoot a missile
                alienMissileCounter = 0;
                missileAlien.add(new MissileAlien(alien[l][k].x, alien[l][k].y, 10));
            }
        }
    }
}