On my device, the snake game seems to stutter. Maybe it is too slow(it is quite old).
I guess the problem is the print method? (not quite sure, though)
I tried to make the print method run faster but I am not quite satisfied yet.
The way I am iterating through the snake's body is bugging me, I guess this take a long time. But I can't really figure out a better way to do so. Maybe you have better ideas? I appreciate your help.
public void print() {
StringBuilder builder = new StringBuilder();
for(int y = 0; y < height; y++){
builder.append("\n");
for(int x = 0; x < width; x++){
//I don't like this part:
boolean body = false;
for(int i = 1; i < snake.getSections().size(); i++){
if(snake.getSections().get(i).getY() == y && snake.getSections().get(i).getX() == x){
body = true;
}
}
//
if(y == snake.getY() && x == snake.getX()){
builder.append("X");
}
else if(body == true){
builder.append("x");
}
else if(y == mouse.getY() && x == mouse.getX()){
builder.append("^");
}
else{
builder.append(".");
}
}
}
System.out.println(builder);