initialize array

CysCys
edited March 2018 in Questions about Code

Hey Guys, I started my first little project using processing, which turned out to become a snake game (Code below). I finished the head and the moving mechanisms. Now ive come to that point, where i have to define the X and Y positions of each part of the body. I created 2 arrays (tailx[] and tail[]) where i wanna save the locations. Ive also created an algorithm to initialize the position of tailx[n] based on tailx[n - 1], which should move on till n > 0 is false. Then i defined tailx[0] as current X position of the head. After that the Head moves and gets a new X position. But it wont work, instead it says Array index out of bounds. The error is somewhere inside the tail() function from line 127 to line 143. Thankful for every answer.

float gridsize = 20;
float speed = 0.05;
int window = 900;
int score = 0;
int n = score;
float snakex = (int((window/ gridsize)/ 2)) * gridsize;
float snakey = (int((window/ gridsize)/ 2)) * gridsize;
float accelx = 0;
float accely = 0;
float gridnumber = window / gridsize;
float pointa = random(2, gridnumber - 3);
float pointb = random(2, gridnumber - 3);
float pointx = int(pointa) * gridsize;
float pointy = int(pointb) * gridsize;




void setup(){
 size(900, 900); 
 frameRate(100);
}

void draw(){
 background(10);
 run();
}



void food(){
  if(snakex == pointx){
   if(snakey == pointy){
     pointa = random(2, gridnumber - 3);
     pointb = random(2, gridnumber - 3);
     score++;
     println(score);
   }
 }
  pointx = int(pointa) * gridsize;
  pointy = int(pointb) * gridsize;
}




void move(){
 if(keyPressed){
   if(keyCode == UP){
     if(snakex % gridsize == 0){
       if(accely != speed){
         accelx = 0;
         accely = -speed;
       }

     }

   }
   if(keyCode == DOWN){
     if(snakex % gridsize == 0){
       if(accely != -speed){
         accelx = 0;
         accely = speed;
       }
     }
   }
   if(keyCode == RIGHT){
     if(snakey % gridsize == 0){
       if(accelx != -speed){
         accelx = speed;
         accely = 0;
       }
     }
   }
   if(keyCode == LEFT){
     if(snakey % gridsize == 0){
       if(accelx != speed){
         accelx = -speed;
         accely = 0;
       }
     }
   }
 }
}




 void bordercheck(){
   if(snakex > height - gridsize){
     snakex = (int((window/ gridsize)/ 2)) * gridsize;
     snakey = (int((window/ gridsize)/ 2)) * gridsize;
     accelx = 0;
     accely = 0;
     score = 0;
     println("---");
   }
    if(snakey > width - gridsize){
     snakex = (int((window/ gridsize)/ 2)) * gridsize;
     snakey = (int((window/ gridsize)/ 2)) * gridsize;
     accelx = 0;
     accely = 0;
     score = 0;
     println("---");
   }
    if(snakex < 0){
     snakex = (int((window/ gridsize)/ 2)) * gridsize;
     snakey = (int((window/ gridsize)/ 2)) * gridsize;
     accelx = 0;
     accely = 0;
     score = 0;
     println("---");
   }
    if(snakey < 0){
     snakex = (int((window/ gridsize)/ 2)) * gridsize;
     snakey = (int((window/ gridsize)/ 2)) * gridsize;
     accelx = 0;
     accely = 0;
     score = 0;
     println("---");
   }
 }




 void tail(){
   n = score;
   float[] tailx = new float[n + 1];
   float[] taily = new float[n + 1];
   while(n > 0){
     tailx[n] = tailx[n - 1];
     taily[n] = taily[n - 1];
     fill(255);
     rect(tailx[n], taily[n], gridsize, gridsize);
     n--;
   }
   tailx[0] = snakex;
   taily[0] = snakey;
  fill(255);
  rect(tailx[0], taily[0], gridsize, gridsize);

 }


 void design(){
   fill(255,0,0); 
   rect(pointx,pointy,gridsize,gridsize);
   fill(0,255,0);
   rect(snakex,snakey,gridsize,gridsize);
 }



  void run(){
   food();
   tail();
   move();
   snakex = snakex + accelx * gridsize;
   snakey = snakey + accely * gridsize;
   bordercheck();

   design();
  }
Tagged:

Answers

Sign In or Register to comment.