0

我的程序是一个僵尸生存游戏,设置在二维数组中。我使用一个数组列表——我的第一次尝试——来存储僵尸,并且每个人都“检查”它的上方、下方和周围是否有一个块,以检测它的运动。

我这里贴出相关代码,单独上传sketch文件夹。

ArrayList zombies;

void setup() {
  zombies = new ArrayList();
}

void draw() {
      for (int i = 0; i < zombies.size(); i++) {
    Zombie zombie = (Zombie) zombies.get(i);
      zombie.draw();
}
}

void keyPressed() {
  if (key == 'z') {
    zombies.add(new Zombie());
}
}

class Zombie
{
  int posX = 20;
  int posY = 10;
  boolean fall;
  boolean playerOnBlock;

  Zombie() {
    posX = 10;
    posY = 590;
    fall = false;
  }

  void draw() {
    grid.blockCheck(posX, posY, 2);
    fill(0, 255, 0);
    rect(posX, posY, 10, 10);
  }

  void fall() {
    posY += 5;
    println("zombiefall"+posY);
  }

  void move(boolean left, boolean right, boolean above) {
    if (left == true && player.playerX < posX) {
      posX -= 1;
    }
    if (right == true && player.playerX > posX) {
      posX += 1;
    }
  }
}

class Grid {
  void blockCheck(int x, int y, int e) {
    for (int i = 0; i < l; i++)
      for (int j = 0; j < h; j++)
      {
        grid[i][j].aroundMe (x, y, i, j, e);
      }
  }
}

class Block {
  void aroundMe(int _x, int _y, int _i, int _j, int entity) {
    int pGX = _x/10;
    int pGY = _y/10;

    if (entity == 1) {
      if (pGY+1 == _j && pGX == _i && state == 4) {
        player.fall();
      }

      if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
        leftOfMe = true;
      }
      else
      {
        leftOfMe = false;
      }

      if (pGX+1 == _i && _y == posY && state == 4) {
        rightOfMe = true;
      }
      else
      {
        rightOfMe = false;
      }
      if (pGY-1 == _j && _x == posX && state ==4) {
        aboveOfMe = true;
      }
      else
      {
        aboveOfMe = false;
      }

      player.controls(leftOfMe, rightOfMe, aboveOfMe);
    }

    if (entity == 2) {

      if (pGY+1 == _j && pGX == _i && state == 4) {
              for (int i = 0; i < zombies.size(); i++) {
    Zombie zombie = (Zombie) zombies.get(i);
        zombie.fall();
      }
      }

      if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
        ZleftOfMe = true;
      }
      else
      {
        ZleftOfMe = false;
      }

      if (pGX+1 == _i && _y == posY && state == 4) {
        ZrightOfMe = true;
      }
      else
      {
        ZrightOfMe = false;
      }

      if (pGY-1 == _j && _x == posX && state ==4) {
        ZaboveOfMe = true;
      }
      else
      {
        ZaboveOfMe = false;
      }
      for (int i = 0; i < zombies.size(); i++) {
    Zombie zombie = (Zombie) zombies.get(i);
      zombie.move(ZleftOfMe, ZrightOfMe, ZaboveOfMe);
    }
  }

草图在这里:http ://www.mediafire.com/?u5v3117baym846v

我认为问题在于指定我所指的数组列表的哪个元素,因为我可以观察到问题是:

当一个人检测到它应该倒下时,所有“僵尸”都会倒下。

僵尸的速度随着每增加一个僵尸而增加 - 以某种方式将所有僵尸元素视为一个僵尸对象?

这可能是一个类似的问题: An ArrayList 的所有元素都在添加新元素时发生变化?

但我已经摆弄了我的项目,我似乎无法让它继续工作。

请不要犹豫,询问有关我的项目的更多信息。我将整晚都在我的电脑前,所以应该能够快速回复。提前致谢。

谢谢你的帮助。我这样使用它:

ArrayList <Zombie> zombies =  new ArrayList <Zombie>();
-------------------------------------------
    void setup(){
  zombies = new ArrayList();
-------------------------------------------

void draw(){
  for (Zombie z:zombies) {
    z.draw();
  }
}

-------------------------------------------

void keyPressed() {
  if (key == 'z') {
    for (int i = 0; i< 1; i++) {
      zombies.add(new Zombie(i));
    }
  }

-------------------------------------------

class Zombie
{
  int posX = 20;
  int posY = 10;
  boolean fall;
  boolean playerOnBlock;
  int z;

  Zombie(int _z) {
    posX = 10;
    posY = 590;
    fall = false;
    z = _z;
  }

  void draw() {
    grid.blockCheck(posX, posY, 2);
    fill(0, 255, 0);
    rect(posX, posY, 10, 10);
  }

  void fall() {
    posY += 5;
    println("zombiefall"+posY);
  }

  void move(boolean left, boolean right, boolean above) {
    if (left == true && player.playerX < posX) {
      posX -= 1;
    }
    if (right == true && player.playerX > posX) {
      posX += 1;
    }
  }
}
4

1 回答 1

0

这是想法:)

//use generics to keep it simple. Only Zoombies in this list
ArrayList <Zoombie> samp =  new ArrayList <Zoombie>();

void setup() {
  //a regular for loop to use the i as id
  for (int i = 0; i< 100; i++) {
    samp.add(new Zoombie(i));

  }
  //run just once
  noLoop();
}


void draw() {

  //a enhanced for, no need for use  get()
  // z will represent each element in the collection
  for (Zoombie z:samp) {
    z.d();   
  }

  println("done");
}

// a dummy class ;)
class Zoombie {
  int id;
  Zoombie (int _id)
  {
    id =_id;
  }

  void d()
  {
    println(id);
  }
}
于 2013-03-14T12:41:23.243 回答