//everything here is in vectors
moveSegment(){
vector direction = next_segment_position - this_segment_position
float gap = direction.length() - LENGTH_OF_SEGMENT
// has the next segment moved away?
if(gap > 0){
//close the gap
this_segment.position += normalize(direction) * gap
}
}
This method will cause the snake to move realistically (like a real snake), however that might not be what you want for your game. With this formula, there might be some “sliding” of segments around tightly knit curves. It should however get you started until you implement something more complex.
As with calculus: If you want to avoid “sliding” subdivide your snake into as many discreet pieces as you can. ex: Instead of adding a 5 pixel segment every time the snake eats something, add 5x one pixel segments. This will allow you to simulate a “continous” snake. Hint: If this hinders your rendering methods, you don't have to render every one of the 5 segments. You could render gap the middle segment as a bigger segment, but check collisions against the smaller ones.
The other solution (more percise [and much harder] ) is to stay with a discreet model, and only choose speeds which are whole integers of the segment lengths. You could still use an array then, because your snake, and all of it's movement are still discreet. However, unless this is a 2d game, it would make the rendering algorithm much harder.