二维数组导致动画故障(处理中)

2D Array causes animation glitch (Processing)

简介: 对于一项作业,我必须创建一个 Space Invaders 游戏(使用 Processing)并带有我自己的主题。我创造了一艘可以左右移动并发射的宇宙飞船。我还创造了一个一直向下移动的敌人。我现在想使用 2D 阵列(我必须使用 2D 阵列,作业中的规范)来创建敌人舰队。我希望舰队是 3 排,每排有 5 个敌人。

问题: 当我自己实现一个 2D Array 时,舰队的运动会在下降一点后停止。当我不使用 2D 阵列而只有一个敌人时,一切正常。所以我希望 Whosebug 的优秀人员帮助我弄清楚为什么会发生这种情况以及我们如何解决这个问题。如果你能成功地实现一个 2D 阵列,让舰队像对付一个敌人一样倒下,那么你已经找到了我几周来一直在尝试做的事情。

LINK: 我把我目前拥有的游戏的代码贴出来了。请注意,这些代码将显示只有一个敌人的游戏,而不是一个有敌人舰队和故障的游戏。

谢谢你们,希望你们能帮助我学习和提高我的技能。

注意:我的代码很乱,我用的名字也不好我知道,我会在提交之前解决所有这些问题。

player hero1;

Cell fleet;

final int RED=1;

int testColour(color c) {
  if (c==color(255,0,0))
     return RED;
    return OTHER;
}

PFont font1;
PFont font2;

final int MENU=0;
final int START=1;
int gameMode = MENU;

float x = 0;
float y = 0;

float cX;
float cY;

boolean b1 = false;
boolean b2= false;

void setup()  {
    size(900, 600);
    hero1 = new player(x + 450, 550, 80, 40);
    fleet = new Cell(cX, cY, 20, 20); 
    font1 = loadFont("Font1.vlw");
    font2 = loadFont("Font2.vlw");
    bullets = new ArrayList();

    grid = new Cell[cols][rows];
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      // Initialize each object
      grid[i][j] = new Cell(i*(width-800),j*200,20,20); 
      }
      }
 }

void draw()  {

       if(gameMode==MENU) {
         screen1(); 
       }

       if(gameMode==START) {
         screen2(); 
};
  moveAll();//move all the bullets
  displayAll();
}

void keyPressed() {
if(key==' ' && gameMode==MENU) {
         gameMode=START;
       }
    if(keyCode == RIGHT && gameMode==START) {
      b1=true;
      b2=false;
}
if(keyCode == LEFT && gameMode==START) {
      b2=true;
      b1=false;
}
if(keyCode == ' ' && gameMode==START) {
  Bullet temp = new Bullet(hero1.x,hero1.y);
  bullets.add(temp);
}
}

void keyReleased() {
    if(keyCode == RIGHT && gameMode==START) {
      b1=false;
}
if(keyCode == LEFT && gameMode==START) {
      b2=false;
}
}

ArrayList <Bullet> bullets;

class Bullet
{
  float x;
  float y;
  float speed;
  Bullet(float tx, float ty)
  {
    x = tx;
    y = ty;
  }
  void display()
  {
    stroke(255, 0, 0);
    fill(255);
    ellipse(x,y, 10, 10);
  }
  void move()
  {
    y -= 5;
  }
}
void moveAll()
{
  for(Bullet temp : bullets)
  {
    temp.move();
  }
}
void displayAll()
{
  for(Bullet temp : bullets)
  {
    temp.display();
  }
}

Cell[][] grid;

boolean b3=false;
boolean b4=true;
boolean b5=false;

int cols = 5;
int rows = 2;
color pixel;

class Cell {
  float cX,cY;   


  Cell(float tempX, float tempY, float tempW, float tempH) {
    cX = tempX;
    cY = tempY;
  } 

  void display2() {
    stroke(255, 0, 0);
    fill(255, 0, 0);
    rect(cX+30,cY+100,5,5);
    rect(cX+35,cY+95,5,5);
    rect(cX+35,cY+90,5,5);
    rect(cX+40,cY+85,5,5);
    rect(cX+45,cY+80,5,5);
    rect(cX+50,cY+80,5,5);
    rect(cX+55,cY+75,5,5);
    rect(cX+60,cY+75,5,5);
    rect(cX+65,cY+75,5,5);
    rect(cX+70,cY+75,5,5);
    rect(cX+75,cY+80,5,5);
    rect(cX+80,cY+80,5,5);
    rect(cX+85,cY+85,5,5);
    rect(cX+90,cY+90,5,5);
    rect(cX+90,cY+95,5,5);
    rect(cX+95,cY+100,5,5);
    rect(cX+95,cY+105,5,5);
    rect(cX+95,cY+110,5,5);
    rect(cX+95,cY+115,5,5);
    rect(cX+90,cY+120,5,5);
    rect(cX+85,cY+120,5,5);
    rect(cX+80,cY+120,5,5);
    rect(cX+75,cY+115,5,5);
    rect(cX+70,cY+115,5,5);
    rect(cX+70,cY+120,5,5);
    rect(cX+70,cY+125,5,5);
    rect(cX+65,cY+115,5,5);
    rect(cX+60,cY+115,5,5);
    rect(cX+55,cY+115,5,5);
    rect(cX+55,cY+120,5,5);
    rect(cX+55,cY+125,5,5);
    rect(cX+50,cY+115,5,5);
    rect(cX+45,cY+115,5,5);
    rect(cX+45,cY+120,5,5);
    rect(cX+40,cY+120,5,5);
    rect(cX+35,cY+120,5,5);
    rect(cX+30,cY+115,5,5);
    rect(cX+30,cY+110,5,5);
    rect(cX+30,cY+105,5,5);
    rect(cX+40,cY+125,5,5);
    rect(cX+40,cY+130,5,5);
    rect(cX+45,cY+135,5,5);
    rect(cX+50,cY+140,5,5);
    rect(cX+55,cY+140,5,5);
    rect(cX+60,cY+140,5,5);
    rect(cX+65,cY+140,5,5);
    rect(cX+70,cY+140,5,5);
    rect(cX+75,cY+140,5,5);
    rect(cX+80,cY+135,5,5);
    rect(cX+85,cY+130,5,5);
    rect(cX+85,cY+125,5,5);



   if(b4==true) {  //moving right
      b3 = false;
      b5 = false;
      cX += 2;
    }
    else if(b5==true) { //moving left
      b3 = false;
      b4 = false;
      cX -= 2;
    }

   if( (b4 == true && cX >= 780) || (b5 == true && cX <= -1)) 
   {
      b3 = true;  //moving down
      b4 = false;
      b5 = false;
      //b4=false;
      //b5=false;
    }

   if( b3 == true) //moving down
   {
       cY += 3;
      if (cY % 10 == 0)  //moving down 10 pixels
      {
        b3=false;
        if ( cX >= 780)  //if it is on the right edge
        {
          b4 = false;
          b5 = true;
        }
        else if ( cX <= -1 ) //if it is on the left edge
        {
         b4 = true;
          b5 = false;
          cY += 3;
        }
      }
  }
  }

}

class player {
         float x = 0;
         float y = 0;
         float widthX;
         float widthY;
         float speedX = 3;

         player(float x, float y, float widthX, float widthY) {
         this.x = x;
         this.y = y;
         this.widthX = widthX;
         this.widthY = widthY;
       }
    void hero() {
      stroke(0);
      fill(255);
         rect(x, y-30, 10, 10);
         rect(x-10, y-40, 10, 10);
         rect(x-20, y-30, 10, 10);
         rect(x-30, y-20, 10, 10);
         rect(x+10, y-20, 10, 10);
         rect(x-40, y-10, 10, 10);
         rect(x-20, y-10, 10, 10);
         rect(x-10, y-10, 10, 10);
         rect(x, y-10, 10, 10);
         rect(x+20, y-10, 10, 10);
         rect(x-40, y, 10, 10);
         rect(x-20, y, 10, 10);
         rect(x-10, y, 10, 10);
         rect(x, y, 10, 10);
         rect(x+20, y, 10, 10);
         rect(x-60, y, 10, 10);
         rect(x+40, y, 10, 10);
         rect(x-60, y+10, 10, 10);
         rect(x-50, y+10, 10, 10);
         rect(x-40, y+10, 10, 10);
         rect(x-30, y+10, 10, 10);
         rect(x-10, y+10, 10, 10);
         rect(x+10, y+10, 10, 10);
         rect(x+20, y+10, 10, 10);
         rect(x+30, y+10, 10, 10);
         rect(x+40, y+10, 10, 10);
         rect(x-40, y+20, 10, 10);
         rect(x-30, y+20, 10, 10);
         rect(x-20, y+20, 10, 10);
         rect(x-10, y+20, 10, 10);
         rect(x, y+20, 10, 10);
         rect(x+10, y+20, 10, 10);
         rect(x+20, y+20, 10, 10);
         rect(x+20, y+30, 10, 10);
         rect(x, y+30, 10, 10);
         rect(x-20, y+30, 10, 10);
         rect(x-40, y+30, 10, 10);


if(b1==true) {
  x+=4;
}
if(b1==false) {
   x=x;
}
     if(b2==true) {
   x-=4;
 }
if(b2==false) {
   x=x;
}
     } 
   void right() {
   }
 } 

    void screen1() {  
         PImage img;
         img = loadImage("Pictures/MenuScrn.jpg");
         image(img, 0, 0);
         textFont(font1, 50);
         fill(203, 208, 55);
         text("Space Mushrooms", 70, 100);
         textFont(font2, 40);
         fill(255);
         text("Press SpaceBar to start", 90, 300);
       }
 void screen2() {
         background(0);
         hero1.hero();
         fleet.display2();
       }

需要我们为您调试的代码太多了。您应该尝试张贴 MCVE。比如你的问题跟玩家的移动、子弹、游戏模式、字体都没有关系,那你还是直接去掉吧

您的问题的 MCVE 可能如下所示:

Cell[][] grid;

boolean movingDown=false;
boolean movingRight=true;
boolean movingLeft=false;

void setup() {
  size(900, 600);

  int cols = 5;
  int rows = 2;
  grid = new Cell[cols][rows];
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      // Initialize each object
      grid[i][j] = new Cell(i*(width-800), j*200, 20, 20);
    }
  }
}

void draw() {
  background(0);

  for (int i = 0; i < grid.length; i++) {
    for (int j = 0; j < grid[i].length; j++) {
      grid[i][j].display2();
    }
  }
}

class Cell {
  float cX, cY;   

  Cell(float tempX, float tempY, float tempW, float tempH) {
    cX = tempX;
    cY = tempY;
  } 

  void display2() {
    stroke(255, 0, 0);
    fill(255, 0, 0);
    rect(cX+30, cY+100, 5, 5);
    rect(cX+35, cY+95, 5, 5);
    rect(cX+35, cY+90, 5, 5);
    rect(cX+40, cY+85, 5, 5);
    rect(cX+45, cY+80, 5, 5);
    rect(cX+50, cY+80, 5, 5);
    rect(cX+55, cY+75, 5, 5);
    rect(cX+60, cY+75, 5, 5);
    rect(cX+65, cY+75, 5, 5);
    rect(cX+70, cY+75, 5, 5);
    rect(cX+75, cY+80, 5, 5);
    rect(cX+80, cY+80, 5, 5);
    rect(cX+85, cY+85, 5, 5);
    rect(cX+90, cY+90, 5, 5);
    rect(cX+90, cY+95, 5, 5);
    rect(cX+95, cY+100, 5, 5);
    rect(cX+95, cY+105, 5, 5);
    rect(cX+95, cY+110, 5, 5);
    rect(cX+95, cY+115, 5, 5);
    rect(cX+90, cY+120, 5, 5);
    rect(cX+85, cY+120, 5, 5);
    rect(cX+80, cY+120, 5, 5);
    rect(cX+75, cY+115, 5, 5);
    rect(cX+70, cY+115, 5, 5);
    rect(cX+70, cY+120, 5, 5);
    rect(cX+70, cY+125, 5, 5);
    rect(cX+65, cY+115, 5, 5);
    rect(cX+60, cY+115, 5, 5);
    rect(cX+55, cY+115, 5, 5);
    rect(cX+55, cY+120, 5, 5);
    rect(cX+55, cY+125, 5, 5);
    rect(cX+50, cY+115, 5, 5);
    rect(cX+45, cY+115, 5, 5);
    rect(cX+45, cY+120, 5, 5);
    rect(cX+40, cY+120, 5, 5);
    rect(cX+35, cY+120, 5, 5);
    rect(cX+30, cY+115, 5, 5);
    rect(cX+30, cY+110, 5, 5);
    rect(cX+30, cY+105, 5, 5);
    rect(cX+40, cY+125, 5, 5);
    rect(cX+40, cY+130, 5, 5);
    rect(cX+45, cY+135, 5, 5);
    rect(cX+50, cY+140, 5, 5);
    rect(cX+55, cY+140, 5, 5);
    rect(cX+60, cY+140, 5, 5);
    rect(cX+65, cY+140, 5, 5);
    rect(cX+70, cY+140, 5, 5);
    rect(cX+75, cY+140, 5, 5);
    rect(cX+80, cY+135, 5, 5);
    rect(cX+85, cY+130, 5, 5);
    rect(cX+85, cY+125, 5, 5);

    if (movingRight) {  //moving right
      movingDown = false;
      movingLeft = false;
      cX += 2;
    } else if (movingLeft) { //moving left
      movingDown = false;
      movingRight = false;
      cX -= 2;
    }

    if ( (movingRight && cX >= 780) || (movingLeft == true && cX <= -1)) 
    {
      movingDown = true;  //moving down
      movingRight = false;
      movingLeft = false;
    }

    if (movingDown) //moving down
    {
      cY += 3;
      if (cY % 10 == 0)  //moving down 10 pixels
      {
        movingDown=false;
        if ( cX >= 780)  //if it is on the right edge
        {
          movingRight = false;
          movingLeft = true;
        } else if ( cX <= -1 ) //if it is on the left edge
        {
          movingRight = true;
          movingLeft = false;
          cY += 3;
        }
      }
    }
  }
}

你的问题是因为你决定整个舰队的移动方向是基于舰队的每个成员 .这不太正确 - 当中间成员位于屏幕边缘时你不会向下移动,当 left-mostright-most 时你向下移动 成员在屏幕边缘。

您的逻辑遍历每个成员,说 "this member is not at the edge of the screen, better move right. Now this member is at the edge of the screen, better move down. But this member was already moving down, so we're done moving down"。所有这些矛盾都扰乱了你的逻辑,最后你不停地左移右移,下移停止……最后你根本就不动了。

您需要重构您的逻辑,以便您的行动仅基于重要的舰队成员。

尝试使用调试器或仅使用一张纸和一支铅笔单步调试您的代码,直到您准确理解代码中发生的事情。这是作为程序员需要学习的最重要的技能之一。

这是旨在教您有关二维数组的作业,因此为您这样做是学术不诚实。如果您有一个较小的示例需要帮助而不是完整的家庭作业项目,我们将很乐意提供帮助。祝你好运。