Fruit is not being eaten by Snake

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

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

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

Where exactly do you call the eat() function?

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?

Hello,

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

:)