HELP PAcman

MO2MO2
edited October 2013 in Questions about Code

please help i have this pacman code but i keep getting the error array index out of bound please tell me where in my code my error is cause i cant find it :( and dont think about the comments

int B=40;
int H=25;
int x=1; 
int y=1; 
int z=0; 
float rot=0; // rotation af hvor pacman skal se hen, og det er i radius.
int speed=1; // speed styre hvor hurtigt pacman skal køre
int[][] area = new int[B][H]; 
// oven på har jeg erklæret mine værdier, x og y er position for hvor pacman
// skal tegnes, z er den værdi som får pacman til at lukke munden op
//og ned, jeg kaldt det for z

void setup()
{
  size(800, 500);
  for (int i = 0; i<B;i++) {
    for (int j = 0; j<H;j++) {
      area[i][j] = int (random(5));
    }
  }


  smooth();
  frameRate(11);
  // oven på er window størelse og smooth er så den fjerner
  // kanterne. og framerate er antalet af rate som den viser hvert second
  //her er den 11 rates hvert second. med framerate kan man ændre
  // på den måde kan jeg også kontrollere hvor hurtigt munden skal åbne og lukke
}

void draw()
{


  background(0);
  for (int i=0; i<B;i++) {
    for (int j=0; j<H;j++) {
      if (area [i][j]==1) {
        fill(255);
        ellipse(20*i+10, 20*j+10, 10, 10);
      }
      else if (area[i][j]==2) {
        fill(0, 0, 255);
        rect(20*i, 20*j, 20, 20);
      }
      else if (area[i][j]==3) {
        fill(100, 100, 100);
        ellipse(20*i+10, 20*j+10, 10, 10);
      }
      else if   (area[i][j]==4) {
        fill(150, 255, 255);
        rect(20*i, 20*j, 20, 20);
      }
    }
  }

  fill(255, 255, 0);
  if (z==0) {
    z=1; 
    arc(20*x, 10*y, 20, 20, QUARTER_PI+rot, TWO_PI-QUARTER_PI+rot);
  }
  else if (z==1) 
  {
    z=2; 
    arc(20*x, 10*y, 20, 20, QUARTER_PI-0.4+rot, TWO_PI-QUARTER_PI+0.4+rot);
  }
  else 
  {
    z=0;
    arc(20*x, 10*y, 20, 20, 0+rot, TWO_PI+rot);
  }


  /*

   if(200+x>width) 
   {
   x=-200; 
   }
   if(200+x<0)
   {
   x=width-200;
   }
   if(200+y<0)
   {
   y=height-200;
   }
   if(200+y>height)
   {
   y=-200;

   }
   */
}
void demolish (int x, int y) {
  if (x>=0 && x<B && y>=0 && y<H) {
    switch(area[x][y]) {
    case 1:
    case 2:
    case 3:
      area[x][y]=0;
      break;
    default:
    }
  }
}

void move( int x1, int y1) {
  x1=(x1+B)%B;
  y1=(x1+H)%H;

  switch(area[x1][y1]) {
  case 0:
    break;
  case 1: 
    area[x1][y1]=0; 
    break;
  case 2:
  case 3:
    x1=x;
    y1=y;
    break;
  case 4:
    for (int i=-1; i<2; i++) {
      for (int j=-1; j<2; j++) {
        demolish(x1+i, y1+j);
      }
    }
    break;
  default:
  }
  x=x1;
  y=y1;
}

void keyPressed() {
  if (key==CODED) {
    if (keyCode==RIGHT)


    {

      x=x+speed; 
      println("pos: "+x+","+y);
      if (area[x][y] == 1) area[x][y] = 0;
      rot=0;
    }
    if (keyCode==LEFT)
    {
      x=x-speed;
      rot=PI;
    }
    if (keyCode==UP)
    {
      y=y-speed;
      rot=-HALF_PI;
    }
    if (keyCode==DOWN)
    {
      y=y+speed;
      rot=HALF_PI;
    }
  }
}

Answers

  • edited October 2013

    I've clean up your code just enough for it to compile and run.
    Take notice that programming languages and math aren't the same:
    They generally don't recognize that something like 20j means 20*j!!! :O)

    // forum.processing.org/two/discussion/296/help-pacman
    
    final static int B = 40, H = 25, SPD = 2;
    final static byte[][] area = new byte[B][H];
    
    int x = 1, y = 1, z = 0;
    float rot = 0;
    
    void setup() { 
      size(800, 500);
      smooth(); 
      frameRate(11);
    
      for (int i = 0; i!=B; i++)   for (int j = 0; j!=H; j++)
        area[i][j] = (byte) random(5);
    }
    
    void draw() {
      background(0);
    
      for (int i=0; i!=B; i++)   for (int j=0; j !=H; j++) {
        if (area[i][j] == 1) { 
          fill(-1);
          ellipse(20*i + 10, 20*j + 10, 10, 10);
        }
    
        else if (area[i][j] == 2) { 
          fill(#0000FF); 
          rect(20*i, 20*j, 20, 20);
        }
    
        else if (area[i][j] == 3) { 
          fill(0200); 
          ellipse(20*i + 10, 20*j + 10, 10, 10);
        }
    
        else if (area[i][j] == 4) { 
          fill(150, 255, 255); 
          rect(20*i, 20*j, 20, 20);
        }
      }
    
      fill(#FFFF00);
    
      if (z == 0) {
        z = 1;
        arc(20*x, 10*y, 20, 20, QUARTER_PI + rot, TWO_PI - QUARTER_PI + rot);
      }
    
      else if (z == 1) {
        z = 2;
        arc(20*x, 10*y, 20, 20, QUARTER_PI -.4 + rot, TWO_PI-QUARTER_PI + .4 +rot);
      }
    
      else {
        z = 0;
        arc(20*x, 10*y, 20, 20, 0 + rot, TWO_PI + rot);
      }
    }
    
    void demolish (int x, int y) {
      if (x>=0 & y<H) {
        switch(area[x][y]) { 
        case 1: 
        case 2: 
        case 3: 
          area[x][y]=0;
        }
      }
    }
    
    void move(int x1, int y1) {
      x1 = (x1 + B)%B;
      y1 = (x1 + H)%H;
    
      switch(area[x1][y1]) {
      case 1:
        area[x1][y1] = 0;
        break;
    
      case 2:
      case 3:
        x1 = x;
        y1 = y;
        break;
    
      case 4:
        for (int i=-1; i<2; i++)  for (int j=-1; j<2; j++)
          demolish(x1+i, y1+j);
      }
    
      x = x1; 
      y = y1;
    }
    
    void keyPressed() {
      if (key != CODED)  return;
    
      final int k = keyCode;
    
      if (k == RIGHT) {
        x += SPD;
        if (area[x][y] == 1)   area[x][y] = 0;
        rot=0;
      }
    
      else if (k == LEFT) {
        x -= SPD;
        rot = PI;
      }
    
      else if (k == UP) {
        y -= SPD;
        rot = -HALF_PI;
      }
    
      else if (k == DOWN) {
        y += SPD;
        rot = HALF_PI;
      }
    }
    
Sign In or Register to comment.