Moving a snake in snake game with SFML C++

Viewed 437

I am trying to make a snake game with SFML+CPP. For now i want to focus on collision, so when a snake goes to the left it needs to come back on the right side of the screen. With some help of the similar questions i have succeeded to make the snake move partially correct to the left and upper side (x and y coordinates)(partially because when going left it jumps like 20px coming out of right side). Here's the code that works like described,the main problem is the right and lower parts.

void Game::update()
{
    sf::Vector2f movement(0.0f, 0.0f);
    if (movingUp) { movement.y -= 0.1f;    }
    if (movingDown) { movement.y += 0.1f;  }
    if (movingLeft) { movement.x -= 0.1f;  }
    if (movingRight) { movement.x += 0.1f; }

    for(auto& snakey : snake){
        if (snakey.getPosition().x <= 0 ) { movement.x = myWindow.getSize().y; }
        if (snakey.getPosition().y <= 0 ) { movement.y = myWindow.getSize().x; }
        if (snakey.getPosition().x + snakey.getLocalBounds().width >= myWindow.getSize().x )  { movement.y = 0.0f;}
        if (snakey.getPosition().y + snakey.getLocalBounds().height >= myWindow.getSize().y ) { movement.x = 0.0f;}
        snakey.move(movement);
    } /*If the whole background would be a map of vectors i could technically push the snake vector from head to tail (stack) onto the beginning?*/
}

Thanks in advance. Edit It seems like i missed to ask the question directly so here it is. How do i move the snake as intended(go to the left, come out of the right etc)?

0 Answers
Related