1

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.

  • Don't you think that the first step would be to create all the pngs that represent your animation? Have you done that already? – Sam Varshavchik Aug 15 '20 at 13:31
  • I have, and it is all in one place. Well, only for when snake moves straight LEFT, RIGHT, UP and DOWN. It is in a sprite sheet. – logoslavikus Aug 15 '20 at 15:37
  • 1
    If anyone's wondering, here's how it looks. https://easyupload.io/wrifkn – logoslavikus Aug 15 '20 at 15:46
  • And when you used your debugger to run your program, what did you see? This is precisely what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly figure out exactly what your code is doing, and why it differs from your expected results. – Sam Varshavchik Aug 15 '20 at 15:47
  • Well to be honest i have never used debugger. I am using Visual Studio Code and i do not even know if i can debug on it, for a fact i know i can on VS2019. – logoslavikus Aug 16 '20 at 16:45
  • You cannot expect to be able to complete a non-trivial c++ program without needing to debug it, at some point. I spend just as much time debugging code as writing it. – Sam Varshavchik Aug 16 '20 at 16:50
  • Well, maybe the lack of mentioning is to blame for me not ever trying it, but i guess it's more on me for not researching deeper. – logoslavikus Aug 16 '20 at 16:58
  • @SamVarshavchik Well, when it try to debug this and set the breakpoint at constructor, i get redirected to `contextSettings.hpp` then it goes two lines there and it makes me quit. I get this error `Unable to open 'dl-error-skeleton.c': Unable to read file '/build/glibc-YYA7BZ/glibc-2.31/elf/dl-error-skeleton.c' (Error: Unable to resolve non-existing file '/build/glibc-YYA7BZ/glibc-2.31/elf/dl-error-skeleton.c').` What is with this? – logoslavikus Aug 20 '20 at 15:00
  • I am, by the way, going to `step into` always because everything else makes me exit – logoslavikus Aug 20 '20 at 15:00
  • That's part of the C runtime library. There's rarely a need to step into low-level libraries, but if there's a need to do that there's usually an optional debug package that can be installed to do that. – Sam Varshavchik Aug 20 '20 at 16:57
  • @SamVarshavchik What optional debug package? Are you able to share a link? – logoslavikus Aug 21 '20 at 14:19
  • The package is different in every Linux distribution. In the current Fedora distribution, this would be the gcc-debuginfo and gcc-debugsource RPMs; and the `debuginfo-install` tool would find and install them. Whatever Linux distribution you're using, you need to investigate and figure out what needs to be done to install debug support for your libraries. – Sam Varshavchik Aug 21 '20 at 15:14
  • @SamVarshavchik As far as i know, i have gdb installed on my Ubuntu but i will investigate deeper. – logoslavikus Aug 23 '20 at 17:13

0 Answers0