My code is simply like this
for segments:
for (let i = this.snakeArray.length - 1; i > 0; i--) {
this.snakeArray[i].position = this.snakeArray[i - 1].position;
}
for moving the snake head:
if (this.snakeDirection === Direction.UP) {
this.snakePos.y += Math.floor(this.MOVE_SPEED * deltaTime);
} else if (this.snakeDirection == Direction.LEFT) {
this.snakePos.x -= Math.floor(this.MOVE_SPEED * deltaTime);
} else if (this.snakeDirection == Direction.RIGHT) {
this.snakePos.x += Math.floor(this.MOVE_SPEED * deltaTime);
} else if (this.snakeDirection == Direction.DOWN) {
this.snakePos.y -= Math.floor(this.MOVE_SPEED * deltaTime);
}
The problem is the segments when spawned collided with the head and caused game over state. How can I fix this? Thanks.