new to processing, trying to use the easiest way to code the game snake, looking for some help here.

I have done the part which controls the snake to move, but I am still trying to figure out a way to duplicate the snake's tail after eating the apple. Here below is my code. Looking out for any suggestion and inspiration. Thanks a lot.

here is my code:

int x;
int y;
int a;
int b;
int a2;
int b2;
int aa,bb;
float dis;
int xpos=200;
int ypos=300;
int applex=int(random(1, 20))*20;
int appley=int(random(1, 30))*20;




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


}

void draw() {
  background(225);
  //drawlines();
  drawsnake();
  eatfood();
  drawfood();

}

void drawlines() {
  fill(0);
  stroke(0);
  x+=20;
  y+=20;
  if (x<=600) {
    line(x, 0, x, 600);
  }
  if (y<=600) {
    line(0, y, 600, y);
  }
}

void keyPressed() {
  if (keyCode == LEFT) {
    a=-5;
    b=0;
    a2=28;
    b2=0;
  } else if (keyCode == RIGHT) {
    a=5;
    b=0;
    a2=-28;
    b2=0;
  } else  if (keyCode == UP) {
    a=0;
    b=-5;
    b2=28;
    a2=0;
  } else if (keyCode == DOWN) {
    a=0;
    b=5;
    b2=-28;
    a2=0;
  }
}

void drawsnake() {
  fill(0);
  rect(xpos, ypos, 20, 20);
  xpos+=a;
  if (xpos>600) {
    xpos=0;
  }
  if (xpos<0) {
    xpos=600;
  }
  ypos+=b;
  if (ypos>600) {
    ypos=0;
  }
  if (ypos<0) {
    ypos=600;
  }
}

void drawfood() {
  fill(0, 225, 225);
  rect(applex, appley, 20, 20);
}

void eatfood(){
  dis=dist(xpos,ypos,applex,appley);
  if (dis<15){
applex=int(random(1, 20))*20;
appley=int(random(1, 30))*20;
clonesnake();
  }
}

void clonesnake(){

  fill(0);
  rect(xpos+a2, ypos+b2, 20, 20);



}
Tagged:

Answers

This discussion has been closed.