Thread: How to move the snake?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    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?

    Thanks in advance.
    Last edited by funkydude9; 07-27-2003 at 02:27 AM.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM