Snake game, attempt of a beginner C/C++

Asked

Viewed 211 times

2

Hello, I’m doing the snake game, but I’m doing it with a matrix, the way I’m doing it is to give prints and system("cls") quickly, but I’d like to know more efficient ways, like by expelling gotoxy Code to print the screen:

int i, j;
cout<<setw(MaxL/2 +5)<<"SNAKE GAME"<<endl;
for(i=0; i<MaxC; i++)
{
    for(j=0; j<MaxL; j++)
    {
        if(j==MaxL-1)
            cout<< ScreenPrint[i][j]<<endl;
        else
            cout<<ScreenPrint[i][j];

    }

}

This way I can easily know the coordinates where everything is and know when I reach the limit: Code that updates the values of the snake according to the keys W,A,S,D: Gety Returns the y value of Snake, Getx returns the x value of Snake Sety() updates coordinates, -1 to do y-- and 1 to do y++

switch(toupper(c))    {
case 'W':
    ScreenPrint[GetY()][GetX()]=' ';
    setY(-1);
    ScreenPrint[GetY()][GetX()]=254;
    break;
case 'A':
    ScreenPrint[GetY()][GetX()]=' ';
    setX(-1);
    ScreenPrint[GetY()][GetX()]=254;
    break;
case 'S':
    ScreenPrint[GetY()][GetX()]=' ';
    setY(1);
    ScreenPrint[GetY()][GetX()]=254;
    break;
case 'D':
    ScreenPrint[GetY()][GetX()]=' ';
    setX(1);
    ScreenPrint[GetY()][GetX()]=254;
    break;
default:
    break;
}

/*  SCREEN LIMIT */
if(GetX()<=0 || GetY()<=0 || GetX()>=MaxL-1 || GetY()>=MaxC-1)
         return -1;

else
    return 1;

To conclude, I would like to know possible libraries to add, different ways to make, new ideas, I still do not know how to use a lot of libraries beyond standards, I would add help. Thank you

1 answer

1


  • That sounds a little complicated, but I’ll try, thank you.

  • @Fábiomoral have a look at the tutorials of Sdl of this site, they can help you http://lazyfoo.net/tutorials/SDL/

  • That’s just what I needed, can I do the whole game first on the console and then start using the SDL2 library? Or are there functions that help build the Snake game? I have to download the library and then import it into the project?

  • @Fábiomoral With sdl you can for example load the image of the snake onto the screen and move it with the http://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.phpkeystrokes

  • http://lazyfoo.net/tutorials/SDL/04_key_presses/index.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.