# How do I make the snake's tail add on when i hit the apple?

**URL:** <https://discourse.processing.org/t/how-do-i-make-the-snakes-tail-add-on-when-i-hit-the-apple/23322>\
**Category:** Coding Questions\
**Created:** [August 18, 2020, 12:25am UTC](https://discourse.processing.org/t/how-do-i-make-the-snakes-tail-add-on-when-i-hit-the-apple/23322 "2020-08-18T00:25:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdixo82](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@cdixo82](https://discourse.processing.org/u/cdixo82)\
**Post date:** [August 18, 2020, 12:25am UTC](https://discourse.processing.org/t/how-do-i-make-the-snakes-tail-add-on-when-i-hit-the-apple/23322/1 "2020-08-18T00:25:22Z")

</div>

````auto
ArrayList<Integer> x = new ArrayList<Integer>();
ArrayList<Integer> y = new ArrayList<Integer>();

int gridSize = 20;
int h = height;
int w = height;
int xdir = 0;
int ydir = 0;
boolean eaten = false;
int applex;
int appley;

void setup() {
  size(400, 400);
  frameRate(8);
  x.add(width - gridSize*12);
  y.add(height - gridSize*12);
  x.add(width - gridSize*13);
  y.add(height - gridSize*12);
  
  appley = int(random(0, w/gridSize));
  applex = int(random(0, w/gridSize));
}

void draw() {
  background(0);
  grid();
  testMovement();
  fill(255, 0, 0);
  rect(applex*gridSize, appley*gridSize, gridSize, gridSize);
  
  for (int i = 0; i < x.size(); i++) {
    int xpos = x.get(i);
    int ypos = y.get(i);
    int lastxpos = x.get(0);
    int lastypos = y.get(0);
    fill(0, 255, 0);
    rect(x.get(i), y.get(i), gridSize, gridSize);
    
    x.add(xpos += xdir*gridSize);
    y.add(ypos += ydir*gridSize);
    x.remove(0);
    y.remove(0);
//this is where im struggling
    if (xpos == applex*gridSize && ypos == appley*gridSize) {
      x.add(lastxpos += xdir);
      y.add(lastypos += ydir);
    }
  }
}

void grid() {
  stroke(150);
  for (int j = 0; j < width; j++) {
    line(j*gridSize, 0, j*gridSize, height);
  }
  for (int k = 0; k < height; k++) {
    line(0, k*gridSize, width, k*gridSize);
  }
}

void testMovement() {
  if (keyPressed) {
    if (keyCode == UP) {
      xdir = 0; ydir = -1;
    }
    if (keyCode == DOWN) {
      xdir = 0; ydir = 1;
    }
    if (keyCode == LEFT) {
      xdir = -1; ydir = 0;
    }
    if (keyCode == RIGHT) {
      xdir = 1; ydir = 0;
    }
  }
}```
````

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 20, 2020, 7:34am UTC](https://discourse.processing.org/t/how-do-i-make-the-snakes-tail-add-on-when-i-hit-the-apple/23322/2 "2020-08-20T07:34:40Z")

</div>

Hello and welcome to the forum!

Great to have you here!

Can you please state a question about your current code?

Thank you!

Chrisir

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 20, 2020, 10:38am UTC](https://discourse.processing.org/t/how-do-i-make-the-snakes-tail-add-on-when-i-hit-the-apple/23322/3 "2020-08-20T10:38:50Z")

</div>

Hello,

You posted code and a subject but have not asked a specific question.

Please read and update your post:

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147/1):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

[https://discourse.processing.org/faq](https://discourse.processing.org/faq)

`:)`

---

<div class="post-metadata">

**Author:** ![cdixo82](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@cdixo82](https://discourse.processing.org/u/cdixo82)\
**Post date:** [August 20, 2020, 9:14pm UTC](https://discourse.processing.org/t/how-do-i-make-the-snakes-tail-add-on-when-i-hit-the-apple/23322/4 "2020-08-20T21:14:47Z")

</div>

Hi Chrisir,

I have wanted to make snake since I started processing.  
As soon as i found out what an arraylist was, i tried to make it.  
However, i can’t figure out how to make the snake add on properly.  
As you can see, it adds on, but in the wrong spot and goes at a random speed.  
I think it is around here that i am struggling:

> [@cdixo82](#):
>
> ```auto
> if (xpos == applex*gridSize && ypos == appley*gridSize) {
> x.add(lastxpos += xdir);
> y.add(lastypos += ydir);
> }
> 
> ```

Hopefully you can help,  
Thanks
