# Fruit is not being eaten by Snake

**URL:** <https://discourse.processing.org/t/fruit-is-not-being-eaten-by-snake/27132>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 17, 2021, 7:20am UTC](https://discourse.processing.org/t/fruit-is-not-being-eaten-by-snake/27132 "2021-01-17T07:20:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learning-Everyday](https://avatars.discourse-cdn.com/v4/letter/l/49beb7/32.png) [@Learning-Everyday](https://discourse.processing.org/u/Learning-Everyday)\
**Post date:** [January 17, 2021, 7:20am UTC](https://discourse.processing.org/t/fruit-is-not-being-eaten-by-snake/27132/1 "2021-01-17T07:20:54Z")

</div>

Hi,

I am a beginner in Processing and am having an issue where snakes isn’t eating the fruit. Could you assist with fixing this bug.

Thanks in advance

Provided below is the code

==============================================================================

```auto
int scrollspeed = 30;
int cols = width/scrollspeed;
int rows = height/scrollspeed;
PVector fruit;
float x = 0;
float y = 0;
float xspeed = 1;
float yspeed = 0;
int total = 0;
ArrayList<PVector> tail = new ArrayList<PVector>();

void setup() { //start void setup
  size(600, 600);
  frameRate(5);
  pickLocation();
} //End void setup

void draw() { // start void draw
  background(51);
if (eat(fruit)) {   
pickLocation();
}
  Snake();
  restart();
  update();
  display();
  fill(255, 198, 13);
  rect(fruit.x, fruit.y, scrollspeed, scrollspeed);
} //End void draw

void Snake() { // Start Snake
} //End Snake

void pickLocation() { //Start pickLocation
  fruit = new PVector(floor(random(cols)), floor(random(rows)));
  fruit.mult(scrollspeed);
} // End pickLocation

boolean eat(PVector pos) {
  float d = dist(x, y, pos.x, pos.y);
  if (d < 1) {
    total++;
    return true;
  } else {
    return false;
  }
}

void restart() {
  for (int i = 0; i < tail.size(); i++) {
    PVector pos = tail.get(i);
    float d = dist(x, y, pos.x, pos.y);
    if (d < 1) {
      println("starting over");
      total = 0;
      tail.clear();
    }
  }
}

void dir(float x, float y) {
  xspeed = x;
  yspeed = y;
}

void update() {
  if (total > 0) {
    if (total == tail.size() && !tail.isEmpty()) {
      tail.remove(0);
    }
    tail.add(new PVector(x, y));
  }
  x = x + xspeed*scrollspeed;
  y = y + yspeed*scrollspeed;
  x = constrain(x, 0, width-scrollspeed);
  y = constrain(y, 0, height-scrollspeed);
}

void display() {
  fill(255, 0, 11);
  for (PVector v : tail) {
    rect(v.x, v.y, scrollspeed, scrollspeed);
  }
  rect(x, y, scrollspeed, scrollspeed);
}

void mousePressed() { // Start mousePressed 
  total++; } //End mousePressed

void keyPressed() { //Start ketPressed
  if (keyCode == UP) {
    dir(0, -1);
  } else if (keyCode == DOWN) {
    dir(0, 1);
  } else if (keyCode == RIGHT) {
    dir(1, 0);
  } else if (keyCode == LEFT) {
    dir(-1, 0);
  }

} //End keyPressed

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [January 17, 2021, 10:28am UTC](https://discourse.processing.org/t/fruit-is-not-being-eaten-by-snake/27132/2 "2021-01-17T10:28:49Z")

</div>

Where exactly do you call the `eat()` function?

---

<div class="post-metadata">

**Author:** ![Learning-Everyday](https://avatars.discourse-cdn.com/v4/letter/l/49beb7/32.png) [@Learning-Everyday](https://discourse.processing.org/u/Learning-Everyday)\
**Post date:** [January 17, 2021, 9:41pm UTC](https://discourse.processing.org/t/fruit-is-not-being-eaten-by-snake/27132/3 "2021-01-17T21:41:10Z")

</div>

Good catch! Thanks for looking out for me. I’ve updated the code.  
While the code is functional, some how the fruit keep showing up within the top left area. Any suggestion to make fruit appear real random, all over the game, nit just in top left area?

---

<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:** [January 17, 2021, 10:05pm UTC](https://discourse.processing.org/t/fruit-is-not-being-eaten-by-snake/27132/4 "2021-01-17T22:05:43Z")

</div>

Hello,

Please format your code as well:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`
