I am making the classic snake game. I have managed to complete the basics of the game (e.g. eating food, collision detection, etc.) and it works fairly well. However, occasionally when I eat the food, the snake stops, and void draw no longer loops. I checked for errors, but couldn’t find any. I tried printing messages inside of void draw to see if it was still looping, and nothing printed. What really confuses me is that this problem randomly occurs when eating at different snake lengths, so it seems like the stopping in void draw occurs randomly. Is there a logic error that I am missing? I am fairly new to programming so the code might not be too efficient.
Snake Game:
import java.util.ArrayList;
public static ArrayList snakeX = new ArrayList();
public static ArrayList snakeY = new ArrayList();
public static boolean food = false;
long increment = 250;
String direction = “Forward”;
int score = 0;
int foodX = 0;
int foodY = 0;
void setup(){
size(800,800);
background(0,191,255);
snakeX.add(400);
snakeY.add(400);
snakeX.add(375);
snakeY.add(400);
snakeX.add(350);
snakeY.add(400);
}
void draw(){
//Drawing
background(0,191,255);
for(int i = 0; i < snakeX.size(); i++){
rect(snakeX.get(i), snakeY.get(i), 25, 25);
}
//Calling spawnFood
if(!food){
int [] foods = spawnFood();
foodX = foods[0];
foodY = foods[1];
}
rect(foodX, foodY, 25, 25);
//Every 75 miliseconds
if((millis() - increment) > 0){
increment+=75;
//Exchanging Values
for(int i = snakeX.size()-1; i > 0; i–){
snakeX.set(i, snakeX.get(i-1));
snakeY.set(i, snakeY.get(i-1));
}
//Moving
if(direction.equals("Forward")){
snakeX.set(0, snakeX.get(0)+25);
}else if(direction.equals("Up")){
snakeY.set(0, snakeY.get(0)-25);
}else if(direction.equals("Down")){
snakeY.set(0, snakeY.get(0)+25);
}else{
snakeX.set(0, snakeX.get(0)-25);
}
//Eating
if(foodX == snakeX.get(0) && foodY == snakeY.get(0)){
println(snakeX.size());
if(backFacing(snakeX.get(snakeX.size()-2), snakeX.get(snakeX.size()-1), snakeY.get(snakeY.size()-2), snakeY.get(snakeY.size()-1))==1){
snakeX.add(snakeX.get(snakeX.size()-1)-25);
snakeY.add(snakeY.get(snakeY.size()-1));
}else if(backFacing(snakeX.get(snakeX.size()-2), snakeX.get(snakeX.size()-1), snakeY.get(snakeY.size()-2), snakeY.get(snakeY.size()-1))==2){
snakeX.add(snakeX.get(snakeX.size()-1)+25);
snakeY.add(snakeY.get(snakeY.size()-1));
}else if(backFacing(snakeX.get(snakeX.size()-2), snakeX.get(snakeX.size()-1), snakeY.get(snakeY.size()-2), snakeY.get(snakeY.size()-1))==3){
snakeX.add(snakeX.get(snakeX.size()-1));
snakeY.add(snakeY.get(snakeY.size()-1)-25);
}else if(backFacing(snakeX.get(snakeX.size()-2), snakeX.get(snakeX.size()-1), snakeY.get(snakeY.size()-2), snakeY.get(snakeY.size()-1))==4){
snakeX.add(snakeX.get(snakeX.size()-1));
snakeY.add(snakeY.get(snakeY.size()-1)+25);
}
println(snakeX.size());
food = false;
score++;
}
}
//Collisions
for(int i = 1; i < snakeX.size(); i++){
if((snakeX.get(0).equals(snakeX.get(i)))&&(snakeY.get(0).equals(snakeY.get(i)))){
score = 0;
snakeX.clear();
snakeY.clear();
snakeX.add(400);
snakeY.add(400);
snakeX.add(375);
snakeY.add(400);
snakeX.add(350);
snakeY.add(400);
}
}
}
void keyPressed(){
if(key==CODED){
if(((keyCode==UP)&&!(direction.equals(“Down”)))||((keyCode==UP)&&(snakeX.size()==1))){
direction = “Up”;
}else if(((keyCode==DOWN)&&!(direction.equals(“Up”)))||((keyCode==DOWN)&&(snakeX.size()==1))){
direction = “Down”;
}else if(((keyCode==RIGHT)&&!(direction.equals(“Backward”)))||((keyCode==RIGHT)&&(snakeX.size()==1))){
direction = “Forward”;
}else if(((keyCode==LEFT)&&!(direction.equals(“Forward”)))||((keyCode==LEFT)&&(snakeX.size()==1))){
direction = “Backward”;
}
}
}
public static int [] spawnFood(){
int coords [] = new int [2];
int x = 0;
int y = 0;
boolean allGood = false;
while(!allGood){
x = ((int)(Math.random()*20)+1)*25;
y = ((int)(Math.random()*20)+1)*25;
allGood = true;
for(int i = 0; i < snakeX.size(); i++){
if((snakeX.get(i) == x)||(snakeY.get(i)==y)){
allGood = false;
break;
}
}
}
coords[0] = x;
coords[1] = y;
food = true;
return coords;
}
public static int backFacing(int x1, int x2, int y1, int y2){
if(x1-x2>0){
return 1;//Going Right
}else if(x1-x2<0){
return 2;//Going Left
}else if(y1-y2>0){
return 3;//Going Down
}else if(y1-y2<0){
return 4;//Going Up
}
return 1;
}