# How to make Snake Body in Snake Game?

I'm new in cocos2d and game dev. I'm still learning in game dev. My goal is to make one game like Doodle Grub. But problem is snake body. I wrote like following

-(void)update:(ccTime)delta
{
//NSLog(@"%f",[GameScence sharedGameScence].playerVelocity.y);

CGPoint tmp=self.position;
float tmprotation;
self.position=[GameScence sharedGameScence].WormHeadPos;

[GameScence sharedGameScence].WormHeadPos=tmp;
tmprotation=self.rotation;

[self setRotation:[GameScence sharedGameScence].Headrotation];

[GameScence sharedGameScence].Headrotation=tmprotation;

}


snake is moving but problem is all of the body segments are same position when snake stop. And all of the body segments is so close and can't see segments like Doodle Grub. Any idea or Any suggestion ?

## 2 Answers

The Snake game is one of the most simplest yet addicting games built ever. I'll show you its implementation idea without code.

Consider an array containing n elements as in snake[n], where snake[0] is the head of the snake. Each element in the array snake i.e. for every snake[i] there should exist some mechanism to store its x and y coordinates.(I preferred arrays).We also need two variables which holds the x and y coordinates of the snake[0]. When the game loop begins, according to the desired direction, the snake[0] is moved accordingly.So since the snake[0] has moved to a different location what we have to do is 1.) assign into snake[1] the previous value(which we stored temporarily) of snake[0]. 2.) Iterate within a loop and change the values of snake[i] to snake[i-1] till snake[i] becomes equal to snake[n]. Feel free to ask me if you have more doubts. But I would recommend you first try these before asking someone a question.

1. Google
2. Read articles
3. Try try try doing as in trial and error
4. Read forums

That is all, Best wishes.

• yes , normal snake game is easy like that. Doodle Grub can move 360 deg . not like old snake game. old snake game is non-stop moving but Doodle Grub is not non-stop moving. when I stop head, all the segment are the same with head. When I moving, it's working fine like your logic. Commented Dec 3, 2010 at 7:44
• @saturngod: that is because you don't stop the snake array elements value swapping when the game gets over.So in the n+1th frame of the game loop 0,1,2 th body part come in the same position and in the n+2th frame: 0,1,2,3 come in the same position Commented Dec 3, 2010 at 13:04

There is a 2D implementation of snake logic for one of the "enemy" entities in Grid Wars, a clone of the great hit Geometry Wars. Source code isn't the greatest readability (imho), but nice that it's available at that link, the game is written in BlitzMax. If you read through it, you'll want to look at the "Type nme6" in the main "gridwars.bmx" file.

That snake moves randomly, but it will help you see how you iteratively position each segment and move segments to the position of the segment in front of it, using a linked list. The head determines the direction of travel. Even just playing the game and using pause / step (P and O in F7 debug mode), and then watching it can help you see how the drawing mechanics is approached.