I have some problems Snake game

Firstly i am so sorry for my so bad english. :slight_smile: I am new on game developing and i try learn. I try writing a snake game, but have 2 problems for now. My first problem snake dont stable while on move and i when change the rotation,then the all body parts changing rotation but i want the part of body come to on head starting point rotation.
Some images on my problems;

My moving method;

void GameScene::snakeMove(float x,float y){
x=snakeHead->getPositionX();
y=snakeHead->getPositionY();
if(snakeHead->getRotation()==90 || snakeHead->getRotation()==270 ){
auto move = MoveBy::create(0.1f,
Vec2(-cos((snakeHead->getRotation())*M_PI / 180.0f)snakeHead->getContentSize().width/4SNAKE_BODY_SPACE,
-sin((snakeHead->getRotation())*M_PI / 180.0f)snakeHead->getContentSize().height/4SNAKE_BODY_SPACE));
snakeHead->runAction(move);
}
else {
auto move = MoveBy::create(0.1f,
Vec2(-cos((snakeHead->getRotation()+180)*M_PI / 180.0f)snakeHead->getContentSize().width/4SNAKE_BODY_SPACE,
-sin((snakeHead->getRotation()+180)*M_PI / 180.0f)snakeHead->getContentSize().height/4SNAKE_BODY_SPACE));
snakeHead->runAction(move);
}

if(snakeHead->getPositionX()>(origin.x+visibleSize.width)
   ||snakeHead->getPositionX()<origin.x
   ||snakeHead->getPositionY()>(origin.y+visibleSize.height)
   ||snakeHead->getPositionY()<origin.y)
Director::getInstance()->pushScene(TransitionFade::create(0,GameScene::createScene()));


  for (int i = 1; i < snakeSize; i++){

      auto move = MoveTo::create(0.1f, snakeBodyParts.at(i - 1)->getPosition());
      snakeBodyParts.at(i)->runAction(move);
    /*
      if(snakeBodyParts.at(i)->getPositionX()==x && snakeBodyParts.at(i)->getPositionY()==y){
          auto rotate = RotateTo::create(0.1f, snakeRotation);
          snakeBodyParts.at(i)->runAction(rotate);
      }*/ //I tried this for moving rotation but not working


  }

}

1 Like

So, all the body parts are rotating at the same time, right?
If that is the case, you should add a delay.

2 Likes
if(snakeBodyParts.at(i)->getPositionX()==x && snakeBodyParts.at(i)->getPositionY()==y){

I guess here x and y are head’s position, but you should compare with previous body part position. Like “if previous part rotated, then current one must do the same rotation”

1 Like

i’ll try. And plus how can find stability for snake while add the body part.

I would try to separate the movement from the rotation.
A simple idea would be to make the snake move by adding an offset instead of using MoveBy.

newPosition = currentPosition + offset;

The offset is a Vec2 that defines the stepSize of your snake. Let’s say it’s (0,1) for moving upwards.
Then when the player changes direction you can set the offset like this:
(1,0) = move right
(-1,0) = move left
(0,-1) = move down

That way you will not have problems with the stability, as you only ever change one dimension of the position at a time (notice the 0 in the second axis of the offset).

Of course this behaviour should only be applied to the snake’s head.