I am new to programming, and have decided to make the game snake for my introductory programming class. I am having trouble getting the snake to increase in length, and for each block to follow the head. My professor told me to use ArrayLists, which I am unfamiliar with. Can someone please direct me in what I should do to make this work? Below is my code, and thank you in advance.
ArrayList snake;
boolean food = true, game = true;
float snakeX = 400, snakeY = 300;
float foodX = random(750), foodY = random(550);
int score = 1, dir =1;
void setup() {
size (800, 600);
snake = new ArrayList();
snake.add(new PVector(width/2, height/2));
}
void draw() {
background(255);
drawSnake();
if (game == true) {
movesnake();
}
collison();
drawFood();
fill (0);
textSize (12);
text ("score:", width-65, 20);
text (score, width-30, 20);
}
void movesnake() {
if (keyPressed) {
if (key == 'w' && dir !=3|| key == 'W' && dir !=3|| keyCode == UP && dir !=3) {
dir = 1;
}
else if (key == 'd' && dir !=4|| key == 'D' && dir !=4|| keyCode == RIGHT && dir !=4) {
dir = 2;
}
else if (key == 's' && dir !=1|| key == 'S'&& dir !=1|| keyCode == DOWN && dir !=1) {
dir = 3;
}
else if (key == 'a' && dir !=2|| key == 'A' && dir !=2|| keyCode == LEFT && dir !=2) {