I can move in good direction with a snake game but i can't seem to animate "where the head" goes. I have the sprite sheet i made myself(and if i need to upload somehow feel free to tell me) and i refered to this question because apparently the writer already had solved what i need to solve but with its help i still couldn't make it work. this
Here is the code i have come up with that does not work. It is all in the constructor.
Game::Game() : myWindow(sf::VideoMode(640, 480), "snakeGame!", sf::Style::Close | sf::Style::Titlebar), snake(3) {
direction = Dir::UP;
xPos = 20;
yPos = 40;
if (!texture.loadFromFile("wholeSnake.png", sf::IntRect(x, y, w, h)))
{
throw "error";
}
switch(direction){
case Dir::RIGHT: x = 0; y = 0; w = 40; h = 10; xPos += 1; sprite.setPosition(xPos + 1, yPos); break;
case Dir::LEFT: x = 0; y = 10; w = 40; h = 10; xPos -= 1; sprite.setPosition(xPos - 1, yPos); break;
case Dir::UP: x = 0; y = 20; w = 10; h = 40; yPos -= 1; sprite.setPosition(xPos, yPos - 1); break;
case Dir::DOWN: x = 10; y = 20; w = 10; h = 40; yPos += 1; sprite.setPosition(xPos, yPos + 1); break;
}
I want to literally, for now, snake change from going left(head to the left) to going up(head up), later i will worry about stuff like animation as intended(i guess this style of doing things first similar but not exactly as i should suits me to certain degree).
Thanks in advance.
EDIT
When i was testing this code out before it showed the sprite for a snake going iirc right or left.
Now, it does not show at all, anything. I even changed the xPos - 1
etc to only xPos
(because that was what i originally intended to do) and it is still like that.
Lately my code is having these unpredicted behaviors and i am wondering why.