There is code for the snake game i am trying to recreate, but it fails on line 44 after eating food with ArrayIndexOutOfBoundsException : 1
Can anybody explain why this happens and how can i fix this?
(only death by overlapping yourself is not coded)
void setup(){
size(200,200);
background(0);
stroke(24,201,46);
fill(24,201,46);
}
float cs = 20;
int hx = 0, hy = 0, s = 10, xs = 1, ys = 0, t, fx = floor(random(cs))*10, fy = floor(random(cs))*10, tail = 0;
int[] xt = {0,}, yt = {0,};
void keyPressed(){
if(key == CODED){
if (keyCode == UP && ys != 1){
xs = 0;
ys = -1;
}
if (keyCode == DOWN && ys != -1){
xs = 0;
ys = 1;
}
if(keyCode == RIGHT && xs != -1){
xs = 1;
ys = 0;
}
if (keyCode == LEFT && xs != 1){
xs = -1;
ys = 0;
}
}
}
void draw(){
if (t == 8){
append(xt,hx);
append(yt,hy);
hx+=xs*10;
hy+=ys*10;
if (hx >= 201){hx = 0;}
if(hx < 0){hx = 200;}
if (hy > 200){hy = 0;}
if(hy < 0){hy = 200;}
t = 0;
background(0);
rect(hx, hy, s, s);
for (int i = tail; i > 0; i--){
xt[i] = xt[i-1];
yt[i] = yt[i-1];
rect (xt[i],yt[i],s,s);
}
}
t++;
if(hx == fx && hy == fy){
tail++;
fx = floor(random(cs))*10;
fy = floor(random(cs))*10;
}
stroke(255,0,0);
fill(255,0,0);
rect(fx, fy, s, s);
stroke(24,201,46);
fill(24,201,46);
}
Thanks for any help!