Creating a death animation for snake

//Defining necessary variables and arrays

ArrayList<Integer> x = new ArrayList<Integer>(), y = new ArrayList<Integer>();                                  //creates array lists for the x and y positions of the snake
int gamewidth = 42;                                                                                             //amount of squares on the x-axis
int gameheight = 24;                                                                                            //amount of squares on the y-axis
int entitysize = 20;                                                                                            //size of game-units
int direction = 2;                                                                                              //sets the starting direction to go right
int applex = (int)random(0, gamewidth);                                                                         //randomizes a spawning location for the apple on the x-axis
int appley = (int)random(0, gameheight);                                                                        //randomizes a spawning location for the apple on the x-axis
int speed = 10;                                                                                                 //controls game speed
int[]x_direction = {0, 0, 1, -1};                                                                               //creates an integer-array for the snakes movement on the x-axis
int[]y_direction = {1, -1, 0, 0};                                                                               //creates an integer-array for the snakes movement on the y-axis
boolean gameover = false;                                                                                       //if set to "true" ends the game

//setting up the basics of the program

void setup(){
  size(840,480);                                                                                                //nod to screen ratio of old Nokia 3310 on which "Snake" gained popularity, defines size of the game window
  x.add(0);                                                                                                     //sets the snakes starting coordinate on the x-axis (the left border)
  y.add(12);                                                                                                    //sets the snakes starting coordinate on the y-axis (the middle of the games height)
  }

//running the program

void draw(){
  background(#00CC00);                                                                                          //colors game background green
  stroke(#00CC00);                                                                                              //colors snake units have a green border around them
  fill(0);                                                                                                      //colors the snake units black
  for (int l = 0; l < x.size(); l++) rect(x.get(l)*entitysize, y.get(l)*entitysize, entitysize, entitysize);    //creates the snake so that its part of the ArrayList
  if (!gameover){                                                                                               //runs as long as the player hasnt gotten a game over
    fill(#FF3333);                                                                                              //colors the apples red
    rect(applex * entitysize, appley * entitysize, entitysize, entitysize);                                     //creates an apple
    textAlign(LEFT);                                                                                            //puts score-text on the left side of the screen
    fill(255);                                                                                                  //colors score-text black
    text("Score: "+ x.size(), 20, 20, gamewidth, 50);                                                           //creates score-text and a score which increases by 1 everytime the snake collects an apple
    if (frameCount%speed == 0){                                                                                 //divides the frame count of the game with the speed-variable, the higher the frame count, the faster the game -> the lower the speed-varible, the faster the game
      x.add(0, x.get(0) + x_direction[direction]);                                                              //makes the snake longer
      y.add(0, y.get(0) + y_direction[direction]);                                                              //makes the snake longer
      if (x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= gamewidth || y.get(0) >= gameheight) gameover = true;     //forces a game over when the snake goes past the screen borders
      for (int l = 1; l < x.size(); l++)
      if (x.get(0) == x.get(l) && y.get(0) == y.get(l)){  
        gameover = true;                                                                                        //forces a game over when the snake crashes into itself
      }
      if (x.get(0) == applex && y.get(0) == appley){                                                            //if an apple gets collected
        if (x.size()%5 == 0 && speed >= 2) {                                                                    //for every 5 apples, if the games speed-variable is higher than 2
          speed -= 1;                                                                                           //increases the games speed
        }
        applex = (int)random(0, gamewidth);                                                                     //randomizes a spawning location for a new apple on the x-axis if one has been collected
        appley = (int)random(0, gameheight);                                                                    //randomizes a spawning location for a new apple on the y-axis if one has been collected
      }else{                                                                                                    //every move that an apple doesnt get collected, the snakes size gets reduced by 1. Thus, the snake doesnt get longer constantly, but only on the frames on which an apple is collected
        x.remove(x.size()-1);                                                                                
        y.remove(y.size()-1);
      }
    }
  }else{                                                                                                        //the following gets executed when the player gets a game over
    int score = x.size();
    fill(255);
    textSize(50);                                                                                               //sets the game over text size to 50
    textAlign(CENTER);                                                                                          //puts game over text in the middle of the screen
    if(x.size() == 1){                                                                                          //puts out this text when the players score was 1
      text("GAME OVER \nYou reached a score of "+ score +" point! \nPress ENTER", width/2, height/3);
    }else{                                                                                                      //puts out this text for all other scores
      text("GAME OVER \nYou reached a score of "+ score +" points! \nPress ENTER", width/2, height/3);
    }
  }
}

//defining the actions that get executed when certain keys are pressed

void keyPressed(){                                                                                              
  if (direction == 0 || direction == 1){                                                                        //if the player is moving on the y-axis
     int directChange = keyCode == RIGHT? 2:(keyCode == LEFT? 3: -1);                                           //can press the correct key to change the direction to left or right
     if (directChange != -1) direction = directChange;                                                          //changes direction to the direction the player selected
  }else{                                                                                                        //if the player is moving on the x-axis
     int directChange = keyCode == DOWN? 0:(keyCode == UP? 1: -1);                                              //can press the correct key to change the direction to up or down
     if (directChange != -1) direction = directChange;                                                          //changes direction to the direction the player selected
  }
  if (gameover == true){                                                                                        //gets executed 
    if(keyCode == ENTER){                                                                                       //if ENTER gets pressed
      x.clear();
      y.clear();
      x.add(0);
      y.add(12);
      direction = 2;
      speed = 10;
      textAlign(LEFT);                                                                                          //puts score-text on the left side of the screen
      text("Score: "+ x.size(), 20, 20, gamewidth, 50);                                                         //creates score-text and a score which increases by 1 everytime the snake collects an apple
      gameover = false;
    }
  }
}

Hi!
I was tasked to make a snake game inside of processing. I got it to work so far, but now I have to create an animation for the snake when it hits the side of the screen or crashes into itself. It has to look like the individual parts of the snake are removed one at a time, beginning from the back. I dont really know how to do this, so any help would be greatly appreciated.