简体   繁体   English

为太空入侵者游戏移动 2D 阵列

[英]Moving 2D Array For Space Invaders Game

So the title pretty much explains it all, recently I have learned about 2D arrays but I am a little confused on how to make the 2D array move correctly in this Space Invaders game I am creating.所以标题几乎解释了这一切,最近我了解了 2D 阵列,但我对如何在我正在创建的这个 Space Invaders 游戏中正确移动 2D 阵列感到有些困惑。

Right now I have Aliens moving left to right (and vice versa), however, they don't all move down at the same time, they move down column by column.现在我让外星人从左向右移动(反之亦然),但是,它们不会同时向下移动,而是逐列向下移动。 Anyone know where to edit the code?有谁知道在哪里编辑代码?

Here is my code for the Aliens:这是我的外星人代码:

class Aliens {

  int x = 100, y = 75, deltaX = 1;

  Aliens (int x, int y) {
    this.x = x;
    this.y = y;
  }

  void drawAlien() {
    fill(255);
    rect(x, y, 25, 25);
  }

  void moveAlien() {
    x = x + deltaX;
    if (x >= width) {
      y = y + 20;
      deltaX = - deltaX;
    } else if (x <=0) {
      y = y + 20;
      deltaX = - deltaX;
    }
  }

  void updateAlien() {
    drawAlien();
    moveAlien();
  }
}

and my main class:和我的主要课程:

import ddf.minim.*;

//Global Variables
PImage splash;
PFont roboto;
Defender player;
Aliens[][] alienArray = new Aliens[15][3];
Missile missile;
int gameMode = 0;
int score = 0;

void setup() {
  size(1000, 750);
  rectMode(CENTER);
  textAlign(CENTER, CENTER);
  splash = loadImage("Splash.png");
  player = new Defender();
  for (int row = 0; row < 15; row++) {
    for (int column = 0; column < 3; column++) {
      alienArray[row][column] = new Aliens((row + 1) * 50, (column + 1) * 50);
    }
  }
  roboto = createFont("Roboto-Regular.ttf", 32);
  textFont(roboto);
}

void draw() {
  if (gameMode == 0) {
    background(0);
    textSize(75);
    text("Space Invaders", width/2, height/8);
    textSize(25);
    text("Created by Ryan Simms", width/2, height/4);
    textSize(45);
    text("Press SPACE to Begin", width/2, height - 100);
    image(splash, width/2-125, height/4 + 75);
  } else if (gameMode == 1) {
    background(0);
    score();
    player.updateDefender();
    for (int row = 0; row < 10; row ++) {
      for (int column = 0; column < 3; column++) {
        alienArray[row][column].updateAlien();
      }
    }
    if (missile != null) {
      missile.updateMissile();
    }
    if (keyPressed) {
      if (key == ' ') {
        if (missile == null) {
          missile = new Missile(player.x);
        }
      }
    }
    if (missile != null) {
      if (missile.y <= 0) {
        missile = null;
      }
    }
  }
}

void score() {
  textSize(20);
  text("Score: " + score, 40, 15);
}

void keyPressed() {
  if (key == ' ') {
    gameMode = 1;
  }
}

The logic flaw is in the moveAlien() method.逻辑缺陷在于moveAlien()方法。 You move the x position of an alien by a certain delta.您将外星人的x位置移动一定的增量。 If it is detected that the alien has passed some screen boundary (the x >= width and x <=0 checks), you invert the delta to reverse the movement of the alien, and also update the y position to have it move down.如果检测到外星人已经通过了某个屏幕边界( x >= widthx <=0检查),则反转增量以反转外星人的移动,并更新y位置以使其向下移动。

Since the aliens are oriented in vertical columns, one column will always reach such a boundary while the others haven't yet.由于外星人以垂直列定向,因此一个列总是会到达这样的边界,而其他列尚未到达。 So that column will go down, and also begin reverting its movement.因此,该列将下降,并开始恢复其运动。 The other columns will then "catch up" later.其他列稍后将“赶上”。 So not only do they move down per column, the columns will also end up moving through one another.因此,它们不仅每列向下移动,而且列最终也会相互移动。

You'll have to implement some logic to have your aliens move as a block, by detecting when the rightmost remaining alien has reached the right boundary, or the leftmost remaining alien has reached the left boundary, and then doing an update on all aliens to move down.你必须实现一些逻辑来让你的外星人作为一个块移动,通过检测最右边的外星人何时到达右边界,或者最左边的外星人何时到达左边界,然后对所有外星人进行更新向下移动。

The idea of having each alien have its own state, putting the logic for controlling it in the alien class and calling that is actually good object-oriented design.让每个外星人都有自己的状态的想法,将控制它的逻辑放在外星人类中并调用它实际上是很好的面向对象设计。 It just so happens that for Space Invaders, the aliens all affect one another because they move in a block.碰巧的是,对于太空侵略者来说,外星人都互相影响,因为他们在一个街区内移动。 As a result, your design results in them having too much individuality.结果,您的设计导致它们具有太多的个性。 Perhaps you can add some class that maintains state for the invader array as a whole (such as the leftmost and rightmost column still having an alien, direction of the entire block), control that in your movement loop and have it update the alien's location in turn.也许你可以添加一些类来维护整个入侵者数组的状态(例如最左边和最右边的列仍然有一个外星人,整个块的方向),在你的移动循环中控制它并让它更新外星人的位置转动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM