I am new to processing, and I am trying to use the easiest way to code the game snake.

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.

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

  • Format your code -
    Press the gear icon to edit your post.
    Select your code, ctrl + o to indent, leave a line above and below.

  • what should i do if I use a mac.

  • Answer ✓

    no duplicate threads please

    first in processing hit ctrl-t

    then copy paste to forum

    in forum:

    then Select your code, press with mouse the C icon in the small command bar to indent, leave a line above and below the code

  • Read this page.
    ctrl + o should work on Mac also (latest OS version).

  • afaik the key ctrl on mac is denoted as CMD

  • the format question is solved, anyone can help me with the actual problem?

  • Did you read the Objects tutorial?
    And how about doing a Google search on how to make a snake game in Processing?

  • Those online versions are too complicated for a bigginer

  • I there a solution on the code I am working on to make the tails?

  • is there a way to just add functions to my code in order to complete a snake game?

  • I there a solution on the code I am working on to make the tails?

    Yes.

    is there a way to just add functions to my code in order to complete a snake game?

    yes

  • // snake variables
    int snakesize=1; //size of the snake
    int MAXLENGTH=1011;
    int headx[]=new int[MAXLENGTH]; //xlocation
    int heady[]=new int[MAXLENGTH]; //ylocation
    int i;
    
    //FOOD VARIABLES
    int yumx=(round(random(49))+1)*10;
    int yumy=(round(random(49))+1)*10;
    
    //SPEED
    //int speed;        
    int xspeed, yspeed;
    
    void setup () {
    
      size (500, 500);
    
      headx[0] = 250;
      heady[0] = 250;
      // SNAKE HEAD BEGINS UNMOVING.
      //speed=1;          
      xspeed=0;    
      yspeed=0;
    }
    
    void draw () {
      background (0);
    
      fill(255, 0, 0);
      ellipseMode(CENTER);
      ellipse(yumx, yumy, 15, 15);
    
      for (int i =0; i < snakesize; i++) {
        //tail
        fill(200, 20, 200);
        rectMode(CENTER);
        rect(headx[i], heady[i], 20, 20);
      }
      headx[0]=headx[0]+xspeed;
      heady[0]=heady[0]+yspeed;
    
      for (int i = snakesize; i > 0; i--) {
        headx[i] = headx[i-1];
        heady[i] = heady[i-1];
      }
      // FOOD
      if (snakesize<headx.length)
      {
        if (headx[0]>=yumx-20 && headx[0]<=yumx+20 && heady[0]>=yumy-20 && heady[0]<yumy+20)
        {
          yumx=(round(random(49))+1)*10;
          yumy=(round(random(49))+1)*10;
    
          snakesize++;
    
          if (i!=1)
          {
            headx[snakesize -1] = headx[snakesize - 2] - 0;
            heady[snakesize -1] = heady[snakesize - 2] - 20;
          }
        }
      }
    }
    
    
    void keyPressed()
    {
      // ARROW KEYS OR LETTER KEYS PRESSED.
      // RIGHT & LEFT HAND CONTROLS.
    
      if (keyCode==UP || key == 'w')
      {
        xspeed=0;
        yspeed=-2;
      } else if (keyCode==DOWN || key =='s')
      {
        xspeed=0;
        yspeed=2;
      } else if (keyCode==LEFT || key=='a')
      {
        xspeed=-2;
        yspeed=0;
      } else if (keyCode==RIGHT || key =='d')
      {
        xspeed=2;
        yspeed=0;
      }
    } 
    
Sign In or Register to comment.