C++ Tutorial 18 - Simple Snake Game (Part 2) - playproduction.de

C++ Tutorial 18 – Simple Snake Game (Part 2)

NVitanovic
Views: 654956
Like: 6134
C++ Tutorial 18 – Simple Snake Game (Part 2)

Welcome to my simple game tutorial on C++. In this tutorial i will going to show you how to make a fun snake game.

We will finish our drawing method and add the logic and input to our game.

Source code:
Finished game:

Great C++ books that I recommend for beginners:
C++ Without Fear: A Beginner’s Guide That Makes You Feel Smart (3rd Edition)

C++ Primer Plus (6th Edition) (Developer’s Library)

Programming: Principles and Practice Using C++ (2nd Edition)

Download link for visual studio 2012 express:

If you have any questions I’ll be glad to answer, please leave a comment on the video.

Thanks for watching and please subscribe.

424 Comments

  1. instead of using system("cls") you should use:

    COORD cursorPosition = { 0, 0 };

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPosition);

  2. not sure why but I cant move in the console I think it has to do with the input function may I please get some help

  3. the fruit is not moving to another position and score is not updating. only after several attempts, its happening…

  4. is there like any way too slow down movement? Because mine speeds around the map

  5. if ur fruit is not random generating try this:
    add #include<time.h> in header
    in main()
    before setup();
    add srand(time(NULL));

    The rand() function does not generate a truly random number; it actually returns the next pseudo-random value in a sequence of values ranging from 0 to RAND_MAX. You can change the starting point in that sequence using srand().

  6. Pls I've got a difficulty: My head keeps moving faster than normal, and everywhere I use sleep they say "sleep not declared in this scope", even after I include de windows.h header file. Pls help

  7. it's so funny that he put the break first for the down lol

  8. how the snake head is moving continuously if there is only one time (x++) increment statement??

  9. how can you code this using directional arrows instead of wasd?

  10. someone in 2021 that could help me with _kbhit and _getch? They didn't work because of the library I think and I don't know what to do.

  11. hello, nice video and thank you. Ihave a question, when i inserted the first logic movements, and when i excuted and moved the head, the limits moves one case when the head is moving

  12. I will come back I hope a year from now I can execute this snake game easily so future me hello I hope you made it I believe in you by the way this is my first semester in Uni( IT )

  13. In the new version of this software I couldn't see where you play the code

  14. Amazing
    Can u plz tell how to make tower of Hanoi game using data structure

  15. can someone find whats my error in this
    fruitX=rand() % wigth;

    fruitY=rand() % height;

  16. I love that you left the problem solving part in when there's a error. Makes the vid feel more relatable.

  17. yeah i have a question how do you slow down the game though? like when i run mine the snake goes too fast that i cant eat any fruits, pls help

  18. Im doing the same thing but its not working for me I am half way to giving up XD

  19. I have an issue where I eat the fruit the 3rd time and it doesn't spawn anymore I checked the values in the debugger and they were legit values. Why are they not getting displayed

  20. hi, im having a bit of an issue. i have written the code exactly, yet my fruits dont spawn after the third time theyre eaten. autos says their dimensions are within the board. i give ive tried everything. but nothing is specific enough to my situation. pls help thanks in advance.

  21. For me the w and s only go down into oblivion

  22. when i press a button program stop running..plz help..

  23. dont click on the source this guy has in his description box. a bunch of virus warnings popped up on my computer.

  24. If I put the boundary drawing part in setup it does not fliker . But then the fruit and snake from the draw function are not working right. Can anyone tell why it is so ?

  25. The enum dir when using in switch case is showing ambiguity. What to do ?

  26. My <conio.h> file not on my mac device, <curses.h> also doesn't have khbit() :((

  27. "you called break before the case logic!!!" – me screaming at the screen

  28. I am learning C++ and you made it fun and intresting. I also love the comments #include <windows.h> helped in slowing the snake and #include<time.h> to make the fruit appear randomly. Thanks.

  29. how is your board on the console so calm? mine is flickering all the time, and i have sleep(100) and it still is flickering as hell. any ideas how to stop that?

  30. I would not recommend clicking on the link in the description that shows the code. It showed a link to a fake page that said that I have some viruses.

  31. When I include the conio.h library it includes but there are a bunch of warnings

  32. can't use conio.h library in Xcode in mac

  33. @NVitanovic
    i guess where you defined up and down movement there we should have this code
    case UP:

    heady++; // i.e for up y coordinate should increase

    break;

    case DOWN:

    heady–;

    break;

  34. why is my map going for an infinite loop
    #include<iostream>

    #include<conio.h>

    #include<windows.h>

    using namespace std;

    bool gameOver;

    const int width=20;

    const int height=20;

    int fruitx,fruity,x,y,score;

    int tailX[100],tailY[100];

    int nTail;

    enum direction { stop=0,LEFT,RIGHT,UP,DOWN};

    direction dir;

    void setup()

    {

    gameOver=false;

    dir=stop;

    x=width/2;

    y=height/2;

    fruitx=rand()%width;

    fruity=rand()%height;

    score=0;

    }

    void Draw()

    {

    system("cls");

    for(int i=0;i<width+2;i++)

    {

    cout<<"#";

    }

    cout<<endl;

    for(int i=0;i<height;i++)

    {

    for(int j=0;j<width;j++)

    {

    if(j==0 ) cout<<"#";

    if(i==y && j==x)

    cout<<"O";

    else if(i==fruity && j==fruitx)

    cout<<"F";

    else

    cout<<" ";

    if(j==width-1)

    cout<<"#";

    }

    cout<<endl;

    }

    for(int i=0;i<width+2;i++)

    {

    cout<<"#";

    }

    cout<<endl;

    cout<<"Score"<<score;

    }

    void Input()

    {

    if(_kbhit())

    {

    switch(_getch())

    {

    case 'a':

    dir= LEFT;

    break;

    case 'w':

    dir=UP;

    break;

    case 's':

    dir= DOWN;

    break;

    case 'd':

    dir=RIGHT;

    break;

    case 'x':

    gameOver=true;

    break;

    }

    }

    }

    void Logic()

    {

    switch(dir)

    {

    case LEFT:

    x–;

    break;

    case UP:

    y–;

    break;

    case DOWN:

    y++;

    break;

    case RIGHT:

    x++;

    break;

    default:

    break;

    }

    if(x>width||x<0||y>height||y<0)

    gameOver=true;

    if(x==fruitx && y==fruity)

    {

    score+=1;

    fruitx=rand()%width;

    fruity=rand()%height;

    }

    }

    int main()

    {

    setup();

    while(!gameOver)

    {

    Draw();

    Input();

    Logic();

    }

    return 0;

    }

  35. My problem is that snake moves so fast can anyone help me out…?

  36. I honestly love this tutorial so far. very nice, simple, and easy to understand. I'm personally using ncurses and cstdlib instead of conio since isn't available on macOS and Linux. There's not much difference in its implementation though, so it works out fine enough.

Leave a Reply

Your email address will not be published.