As you can probably guess from the title, I want to make a snake clone, but I'm stuck at how to move the snake. I'm very close so far, I already have him moving like a snake, but the problem occurs when you move him in more then one direction (see below), his whole body moves and collides with the rest.
For example, the dashes represent the body, periods represent space, '0' represents the head and '<' represents the tail:
0---------
.............|
<---------
...the lower portion of his body moves up (ie tail) moves up. Here is a little code snippit of how the snake's body reacts just when the snake moves right:
Code:
if (moveRight)
{
tempR = true;
tempL = false;
if (tempD) //before moving right, he was going down
{
for (f = 0; f < numDots; f++)
{
if (dot[f].y < dot[0].y)
dot[f].y++;
else
dot[f].x++;
}
}
else if (tempU) //before moving right, he was going up
{
for (f = 0; f < numDots; f++)
{
if (dot[f].y <= dot[0].y)
dot[f].x++;
else
dot[f].y--;
}
}
numDots is the number of...body pieces the snake has (ya know, each apple he eats he gains one more). I'm storing the snake's body as an array of...body pieces.
That's pretty much self-explanatory I think. Hopefully from that you'll get an idea of how the rest of my code works. I've actually got it working so far that you can move him two directions and it will work, but my code just gets more and more messier. All I want to know is what is the easiest way to do this? I was thinking of keeping track of each point when the player turns directions, and then based off that point the rest of the snake will know where to move. The trouble is getting the snake to move piece by piece. There has to be a simple solution, I've just been racking my brain and can't think of one. It's probably not even necessary to keep track of the last direction he was moving...or is it?
Thanks in advance.