2D数组导致动画故障(处理)

时间:2016-03-01 12:19:04

标签: processing

附图: 对于作业,我必须使用自己的主题创建一个Space Invaders游戏(使用Processing)。我创造了一个左右移动的太空飞船并且也射击。我还创造了一个一直向下移动的敌人。我现在想要使用2D数组(我必须使用2D数组,分配中的规范)来创建一个敌人队伍。我想舰队是3排,每排5个敌人。

问题: 当我自己实施2D阵列时,舰队的移动在下降一点之后停止。当我不使用2D阵列并且只有一个敌人时,它会很好。所以我希望Stackoverflow的优秀人员能够帮助我弄清楚为什么会这样,以及我们如何解决这个问题。如果你可以成功地实现一个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();
       }

1 个答案:

答案 0 :(得分:1)

要求我们为您调试的代码很多。您应该尝试发布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;
        }
      }
    }
  }
}

您的问题是由于您决定从哪个方向移动整个车队,基于该车队的每个成员。这不太对 - 当中间成员位于屏幕边缘时,您不会向下移动,当最左侧最右侧成员时,您向下移动在屏幕的边缘。

你的逻辑正在遍历每个成员,说“这个成员不在屏幕的边缘,最好向右移动。现在这个成员位于屏幕的边缘,最好向下移动。但是这个成员已经向下移动了,所以我们完成了向下移动“。所有这些矛盾都会混淆你的逻辑,你最终会不断地向左和向右移动,向下移动并停止......所以你最终都不会移动。

你需要重构你的逻辑,这样你才能将你的动作从重要的舰队成员身上移开。

尝试使用调试器逐步执行代码,或者只使用一张纸和一支铅笔,直到您完全了解代码中的内容。这是您作为程序员学习所需的最重要的技能之一。

这是专门为你教授2D阵列的作业,所以这样做对你来说就是学术上的不诚实。如果您有一个较小的例子,您需要帮助而不是完整的家庭作业项目,我们将很乐意为您提供帮助。祝你好运。