# Thread: How to move the snake?

1. ## How to move the snake?

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?

2. Maybe you could use a node approach. Ie, create a class, and an instance of the class for each point on the body. Each node points to the next and previous nodes. Ie:

Code:
```class snBit
{
public:
snBit *prev;
snBit *next;
POINT loc;
};```
Then, for each frame of your program, create a loop that begins with the tail. For each node, make its loc member (x,y coordinates) those of the next node:

Code:
```snBit *snake;

snake=&theActualSnake;
for (someloop)
{
snake->loc=snake->next.loc;
snake=snake->next;
}```
Kapeesh?