C++ Snake game using raylib - Beginner Tutorial 🐍 (OOP)

Поділитися
Вставка
  • Опубліковано 11 січ 2025

КОМЕНТАРІ • 121

  • @programmingwithnick
    @programmingwithnick  Рік тому +2

    I hope you took away lots from this video. Let me know below!
    PS: I created a course named "Object Oriented Programming Made Easy"! Sign up at bit.ly/3NaMfg4. Enroll now.

  • @rohantipnis6863
    @rohantipnis6863 Рік тому +18

    Outstanding tutorial!!!. I was trying to make a snake game but couldn't find any tutorials with C++ and raylib and this video popped up out of nowhere. Very nice explanations.
    Waiting for more tutorials😄

    • @programmingwithnick
      @programmingwithnick  Рік тому +3

      Thank you so much for taking the time to watch my tutorial and for your kind words! I'm glad to hear that you found the explanations helpful in making your own snake game. I'm always working on new tutorials, so stay tuned for more!

  • @felipeoliveira895
    @felipeoliveira895 Рік тому +15

    Fantastic! Looking forward to your new Raylib tutorials! You're very pedagogic!
    Thanks for the content!

  • @hunin27
    @hunin27 Рік тому +7

    I just started learning c++ a couple hours ago, i want to use it to make game systems in unreal engine.
    i don't know much but i kind of understand what is going on in this video, this means you are clear and concise, and explain really well.
    You are underrated!!
    Subscribing right now!

    • @ChouibBalla
      @ChouibBalla 2 місяці тому

      Tell us how did it go my friend?

    • @TheAsker-inYourArea
      @TheAsker-inYourArea Місяць тому +1

      @@ChouibBallaI bet he gave up lol

    • @ChouibBalla
      @ChouibBalla Місяць тому +1

      @@TheAsker-inYourArea I'm sure he did XD(;

  • @R2Sam
    @R2Sam Рік тому +6

    Amazing tutorials for both c++ oop and raylib thanks very much

  • @dorian5574
    @dorian5574 2 місяці тому +3

    I KISSED YOUR HANDS! YOU ARE MY HERO! Lord, you saved me.. THANK YOU FOR SUCH A WONDERFUL JOB! WRITING WITH YOU WAS A JOY! When I was writing code for another tutorial, I almost died from a breakdown, and there were smiles with you! All the best to you, my hero!!!!❤❤❤❤❤❤❤❤❤❤

  • @Tims_Projects
    @Tims_Projects Рік тому +3

    Thankyou Nick, I stayed to the end .
    I am sure a lot of hard work went into that exhalent tutorial. It must have taken you ages to do.

    • @programmingwithnick
      @programmingwithnick  Рік тому +1

      Thank you so much for your kind words, Tim! I'm thrilled to hear that you enjoyed the tutorial and found it helpful. It did take a lot of hard work and time to put together, but comments like yours make it all worth it. I hope the tutorial inspires you to create some awesome games with raylib. Thanks again for watching!

  • @joaocesarlima7339
    @joaocesarlima7339 Рік тому +6

    Amazing! Please keep it up!

    • @programmingwithnick
      @programmingwithnick  Рік тому +1

      Thank you so much for your kind words and support! I'm glad you found the tutorial helpful and informative. I will definitely keep creating more content like this in the future. Stay tuned!

  • @mishanshah6827
    @mishanshah6827 4 місяці тому

    I wish more people could appreciate your tutorials! Absolutely underrated.

  • @Vectorez1
    @Vectorez1 Рік тому +5

    Hey Can you please keep teaching raylib?, like dealing with sprite sheets, physics and other topics. honestly you are the best teacher I've encounter on youtube.

    • @programmingwithnick
      @programmingwithnick  Рік тому +2

      Sure, may raylib tutorials are coming! I love raylib! Just give me some time because creating these tutorials take a lot of time! Thanks for your kind words!

  • @cubingguy7851
    @cubingguy7851 10 місяців тому

    Amazing tutorial now I have a snake game and knowledge in raylib finally I can create on my own games thank you so much

  • @unkn0wn40
    @unkn0wn40 28 днів тому

    Thank you so much for this tutorial dude, I finally can start making c++ games

  • @Anubis10110
    @Anubis10110 Місяць тому

    Simple, straight to the point, great explanation.. thanks ❤

  • @LBCreateSpace
    @LBCreateSpace 10 місяців тому

    Thank you so much for making this. The way you explain things is fantastic and very easy to follow.

  • @hjte2253
    @hjte2253 Рік тому

    Thankyou nick. your voice is so calming.

  • @mohammadaasim8519
    @mohammadaasim8519 Рік тому

    Awesome Ways to Explain Everythings of this Project Looking for more videos

  • @ata243y
    @ata243y Рік тому +1

    thank youuu for your efforts and also for the crystal clear tutorial video.

  • @bendsants-et2zw
    @bendsants-et2zw Рік тому +1

    very good video. your explanation is so good. please keep it up. looking forward for new tutorials. 👍👍👍

  • @thorn8395
    @thorn8395 Рік тому +1

    Amazing tutorial!

  • @modiddymo
    @modiddymo 2 місяці тому

    Excellent tutorial! Thank you :)

  • @charlo-d2
    @charlo-d2 4 місяці тому

    That was a cool tutorial, really enjoyed it, thanks!

  • @ВячеславЛеонов-ч9я
    @ВячеславЛеонов-ч9я 7 місяців тому

    Awesome! Great content! Thank you for this!

  • @manuelgarciagarcia2501
    @manuelgarciagarcia2501 Рік тому +1

    I love it, another incredible video tutorial. Thanks to you I am learning a lot, thank you very much for all the effort in making these incredible tutorials. 👍
    What game will the next tutorial be about, maybe space invader.

    • @programmingwithnick
      @programmingwithnick  Рік тому +4

      Thank you so much for your kind words and support! It makes me really happy to know that my tutorials are helping you learn and improve your skills. As for the next tutorial, I haven't decided yet, but I'll definitely take your suggestion of Space Invader into consideration. Stay tuned and keep learning!

  • @armindalton195
    @armindalton195 8 місяців тому

    you are amazig Nick keep going man

  • @Ramzigamedeveloper
    @Ramzigamedeveloper 7 місяців тому

    oh man i love your video very very much, waiting for another tutorial ❤

  • @billymosis
    @billymosis Рік тому +5

    Hi Nick, thanks for the tutorial.
    I watch for your Tetris video first. And the I try to implement my own snake game using class that I learn from your Tetris video. I'm here to compare how our code differs.
    I use vector instead deque, and I also use 2d int array for the board. In the end the game is work and I learn so much from your videos. Thanks ❤

  • @deekshithr862
    @deekshithr862 3 місяці тому +1

    Hey there this a great tutorial! But i have noticed a bug if the snake has its direction towards left if you press up and right before the game updates in quick succession you will get a game over instantly. It's just a small bug which we can fix by adding Vector2 directionBeforeUpdate = {1,0}; in snake class and setting this insted of direction when getting input and in update function of snake we can add direction = directionBeforeUpdate; these changes will fix the bug.

  • @Roblox_Player_Du_95
    @Roblox_Player_Du_95 Рік тому

    ok that was helpful now i maked my own game !

  • @BlueBirdgg
    @BlueBirdgg 3 місяці тому

    Good tutorial, thanks!

  • @BibiSappiri
    @BibiSappiri Рік тому

    Very good video!

  • @GurjotSingh-j2x
    @GurjotSingh-j2x 13 днів тому

    Thank you so much for such outstanding videos!
    Can you please make a video on creating pacman game using raylib!

  • @afsal_shaji
    @afsal_shaji 7 місяців тому +1

    47:45 my code is not detecting the fruit.. its not changing the position of the fruit.. what could be the problem? Im new to C++

    • @aagavojha9375
      @aagavojha9375 3 місяці тому

      I found the same problem in my code. Did you find the solution?

  • @StarFury2
    @StarFury2 Рік тому +1

    Great tutorial! Making a game is the most practical way of learning a programming language!
    One question - why method names begin with a capital letter? Is it a convention coming from C language?
    This way it's not immediately obvious if "CallSomething()" is a class constructor or a method.

    • @programmingwithnick
      @programmingwithnick  Рік тому +2

      Thank you for your comment! I appreciate your observation. You're right, it's not a convention that applies universally. In this case, I chose to adopt the naming convention used by raylib, the library we are working with. They start their functions with a capital letter, and to maintain consistency, I followed the same convention in my methods. I hope this clarifies the reasoning behind my approach. Feel free to reach out if you have any more questions!

  • @davidefarona
    @davidefarona 7 місяців тому

    Thanks, this is great !!

  • @akaa3144
    @akaa3144 11 місяців тому

    Thx man, another great tutorial! On to Tetris!
    Also to practice coding, I'm gonna start building a small game using what I learned in this tutorial, along with the pong tutorial!
    Thx again!
    (Also 1 had a question, if you wanted to add an image of like a character, and make it move. How would you make the image move?)
    From the tutorials I already know how to add images, and make object move!

    • @programmingwithnick
      @programmingwithnick  11 місяців тому +1

      Thanks for watching! If you want to learn how to move an image, check my latest tutorial published 2 days ago. It is the Space Invaders in C++, it will help you achieve that easily.

  • @hendoitechnologies
    @hendoitechnologies 6 місяців тому

    can you post more c++ oriented game project full video tutorial...like this at complex level

  • @EigenMaster
    @EigenMaster 2 місяці тому

    Thank you man.

  • @sobottrachhuth75
    @sobottrachhuth75 Рік тому +2

    I'm having a problem with the control code; for example when the snake is traveling left, if the player would to press down and right at the same time the snake will immediately turn right, at the beginning I didn't think anything of it but once we get close to the end of the tutorial this became a game breaking bug. does anybody else experience this problem?

    • @programmingwithnick
      @programmingwithnick  Рік тому +7

      You are right, that's something we can fix. We need to declare a new variable
      static bool allowMove = false;
      at the top of the file. Then in the game loop the control block looks like this:
      if (EventTriggered(0.2))
      {
      allowMove = true;
      game.Update();
      }
      if (IsKeyPressed(KEY_UP) && game.snake.direction.y != 1 && allowMove)
      {
      game.snake.direction = {0, -1};
      game.running = true;
      allowMove = false;
      }
      if (IsKeyPressed(KEY_DOWN) && game.snake.direction.y != -1 && allowMove)
      {
      game.snake.direction = {0, 1};
      game.running = true;
      allowMove = false;
      }
      if (IsKeyPressed(KEY_LEFT) && game.snake.direction.x != 1 && allowMove)
      {
      game.snake.direction = {-1, 0};
      game.running = true;
      allowMove = false;
      }
      if (IsKeyPressed(KEY_RIGHT) && game.snake.direction.x != -1 && allowMove)
      {
      game.snake.direction = {1, 0};
      game.running = true;
      allowMove = false;
      }
      You can also find the updated code in the github repository. Thanks, for letting me know about this issue!

    • @sobottrachhuth75
      @sobottrachhuth75 Рік тому +1

      @@programmingwithnick Thank you! And thank you for making these tutorials they're extremely helpful.

  • @abpu_upbgeedvin4722
    @abpu_upbgeedvin4722 18 днів тому +1

    Can I use visual studio 2022 for this tutorial

  • @saalimabdullah2283
    @saalimabdullah2283 11 місяців тому

    Great tutorial!!
    I have one question tho:
    does the game logic work as if we never added a border, and we are only drawing the snake and food offset to inside the border?
    Like does the actual snake exist in the same x and y coordinates as before we added the border?
    And thank youuu for the great tutorial again, I learned a lot!

  • @emerson2463
    @emerson2463 Рік тому

    Amazing!

  • @Tiktokcatch22catchTiktok
    @Tiktokcatch22catchTiktok 2 місяці тому

    I followed the course and everything went well…. But when I arrived on drawtext score with Text format my windows defender warns me of a virus, I narrowed it down to the “%i”. Why?

  • @sbd19
    @sbd19 5 місяців тому

    I seem to have run into an issue having the food appear in the correct cell size in the draw function. Any recommendations?

  • @areeshamemon6570
    @areeshamemon6570 2 місяці тому

    How do I solve the fatal error around the locale.h file? Its not recognised.

  • @jesusmanits5023
    @jesusmanits5023 Рік тому +1

    Hey! Really nice tutorial, I loved it!
    I just started out with C++, but I have a question, to which I can't find a solution anywhere.
    How can I add textures to the snake?
    I tought about loading the image, then the texture in the snake class, but I don't know how to set the position of the texture on top of the snake.

    • @programmingwithnick
      @programmingwithnick  Рік тому +1

      In the Snake class, in the Draw method, you should draw a texture instead of a rectangle. So remove this line: DrawRectangleRounded(segment, 0.5, 6, darkGreen); and use the DrawTexture function. Just load texture and display it.

  • @JsoProductionChannel
    @JsoProductionChannel 7 місяців тому +1

    I need to write std::deque instead of just deque. nvm, just saw that you use: using namespace std;

  • @leonemigone8803
    @leonemigone8803 Рік тому +1

    Thank you nick from Henry everitt

  • @markaster7677
    @markaster7677 Рік тому +4

    I can't load textures everytime I try even with your image the game doesn't start I have no idea why.
    code:
    #include
    #include
    using namespace std;
    Color green = { 173,204,96,255 };
    Color darkGreen = { 43,51,24,255 };
    int cellSize = 30;
    int cellCount = 25;
    class Food {
    public:
    Vector2 position = { 5,6 };
    Texture2D texture;
    Food()
    {
    Image image = LoadImage("food.png");
    texture = LoadTextureFromImage(image);
    UnloadImage(image);
    }
    ~Food()
    {
    UnloadTexture(texture);
    }
    void Draw()
    {
    DrawTexture(texture, position.x * cellSize, position.y * cellSize, WHITE);
    }
    };
    int main()
    {
    Food food = Food();
    cout

    • @foofoo17
      @foofoo17 Рік тому

      Me too. Did you find a solution?

    • @markaster7677
      @markaster7677 Рік тому

      @@foofoo17 nope :(

    • @foofoo17
      @foofoo17 Рік тому +4

      @markaster7677 Initialize your Food object after InitWindow() fixed the issue for me

    • @dp2120
      @dp2120 10 місяців тому

      @@foofoo17 That did it! Thanks so much. Does anyone know why?

    • @foofoo17
      @foofoo17 10 місяців тому

      @@dp2120 I'm assuming you need a window first before loading the textures otherwise there may be errors

  • @MuhammadAssodais
    @MuhammadAssodais 2 місяці тому +2

    How to get that food image??

    • @waseeriqbal1829
      @waseeriqbal1829 2 місяці тому

      Please tell me also

    • @nicolaistenstrm3255
      @nicolaistenstrm3255 Місяць тому

      I found it in the description in the link called final code.
      You can find it on the left side where there is a folder called "Graphics".

  • @markaster7677
    @markaster7677 Рік тому +1

    how do i remove the console?

  • @LeFede
    @LeFede Рік тому +1

    is it possible to scale pixels? so instead of using 1 pixel in game / 1 pixel screen ratio say for example 1 pixel in game / 16 pixels screen . (reduce total pixels in game but keep the size)

    • @programmingwithnick
      @programmingwithnick  Рік тому +1

      In raylib, you can achieve pixel scaling by rendering your game at a lower resolution and then upscaling it to fit the screen resolution. Here are the steps in short: 1. Decide on a lower resolution for your in-game world. This resolution should be smaller than your screen resolution. For example, if your screen resolution is 800x600, you might choose a game resolution of 100x75 2. Create a special drawing surface called a "RenderTexture." This surface will be used to draw your game's world at the lower resolution. Think of it as a canvas for your game. 3. Inside the RenderTexture, draw all your game assets, characters, and objects just like you normally would. However, remember to use the smaller game resolution (e.g., 100x75) for positioning and sizing. 4. Now, render the contents of the RenderTexture to the screen. When doing this, raylib will automatically scale up the smaller resolution to fit your screen. Your game pixels will appear larger due to the scaling effect.

    • @venoenix
      @venoenix 10 місяців тому

      thanks!@@programmingwithnick

  • @KilluaDeska
    @KilluaDeska Рік тому

    hi
    how become snakeBody equals to snake.body , i did not understand

  • @antonioibarra-brooks2031
    @antonioibarra-brooks2031 2 місяці тому

    I got lost trying to add texture to the food, can someone explain to me where to put the image I want to use, so it can be accessed by the code. I am using Visual Studio 2022, I've moved the image to many different folders but it cant seem to access it
    Edit: My file was the wrong size

  • @game4sato280
    @game4sato280 6 місяців тому

    how to print game over message ?

  • @madeariartha2546
    @madeariartha2546 Рік тому +1

    i am new to cpp, is deque equal to ArrayList?

    • @Crux161
      @Crux161 6 місяців тому

      In short, deque does insert/delete from the ends, where as ArrayList supports random elements additionally.
      Deque uses a dynamic array internally, but elements might not be stored contiguously in memory. While ArrayList stores elements contiguously in memory. Both have a constant time complexity of (O(1)).
      Lastly, C++ also has a std::vector container that's similar to ArrayList and it might be a better choice for most use cases due to its contiguous memory allocation and generally better performance.
      Hope this helps to answer your question.

  • @sontay2206
    @sontay2206 8 місяців тому

    can we make it by window.h ? thank so much

  • @BulkFlame-f7m
    @BulkFlame-f7m 2 місяці тому

    why in you computer 750x750 is small, but in my computer it took the screen height??

    • @programmingwithnick
      @programmingwithnick  2 місяці тому

      What is your screen resolution?

    • @BulkFlame-f7m
      @BulkFlame-f7m 2 місяці тому

      @@programmingwithnick 1366x768

    • @BulkFlame-f7m
      @BulkFlame-f7m 2 місяці тому

      @@programmingwithnick can i change the cellSize to 28 so it will 700x700?

  • @akshitkhokhar9118
    @akshitkhokhar9118 11 місяців тому

    bro i dont have any graphics folder or the image in your files what can i do

    • @jampleygameses
      @jampleygameses 4 місяці тому

      Maybe this is too late, but you can simply create a folder named "graphics" inside de game folder, and create a 30x30 image with Paint, that's what I did and it works fine.

  • @itgaming136
    @itgaming136 Рік тому

    Hi, I have a question. What if i want to make .exe file from it? I tried running mingw32-make And its not working. Can you please help me? Thanks much and love your videos man.

    • @programmingwithnick
      @programmingwithnick  Рік тому

      If you succefully complile and run the game using F5 with VS code, in the game folder you are going to see an execetable file created.

    • @itgaming136
      @itgaming136 Рік тому

      @@programmingwithnick ohh Thanks much man

  • @danbranco7800
    @danbranco7800 Рік тому

    What about saving, could you explain?

    • @programmingwithnick
      @programmingwithnick  Рік тому +1

      Yes, I will prepare a detailed video on that topic. Thanks for the comment!

  • @Albertmars32
    @Albertmars32 3 місяці тому +1

    off topic question, how did you learn so speak English so eloquently? ive been living in the USA since i was 10 and my english isnt even half as good as yours. teach me the secret, i want to be fluent as well.🙏🙏

    • @programmingwithnick
      @programmingwithnick  3 місяці тому

      I am not fluent. I write what I have to say, and then record the sound.

  • @djent2689
    @djent2689 Рік тому

    57:43

  • @the_tophatfish
    @the_tophatfish Рік тому

    I cant figure out how to build my game aka. get the executable.

    • @programmingwithnick
      @programmingwithnick  Рік тому

      Download the Github repository. Double click the main.code-workspace to open the VScode project. Then open the main.cpp file. If you have downloaded raylib on the default path you should be able to complile and run the game by pressing f5 on your keyboard. Then on the project folder a main.exe file will be available.

    • @the_tophatfish
      @the_tophatfish Рік тому

      @@programmingwithnick Got it, thanks.

  • @major_138
    @major_138 8 місяців тому +1

    python on c++!

  • @akshitkhokhar9118
    @akshitkhokhar9118 11 місяців тому

    can anybody help

  • @paniex2.026
    @paniex2.026 2 місяці тому

    I don't understand

  • @Crux161
    @Crux161 6 місяців тому

    Coming from Java, C++ feels pretty familiar - I can’t describe just how unnerving that is to me in this moment 😅

  • @EquipteHarry
    @EquipteHarry 3 місяці тому

    Robinson Thomas Jackson Sandra Robinson Thomas

  • @ИринаКим-ъ5ч
    @ИринаКим-ъ5ч 2 місяці тому

    Walker Charles Martin Cynthia White Larry

  • @jobl1978
    @jobl1978 Рік тому +11

    A perfect example for bad C++ code. Container copied by value instead of const ref. Counting for loops instead of range based loops and so on…

    • @programmingwithnick
      @programmingwithnick  Рік тому +37

      Thank you for your feedback!

    • @calengo454
      @calengo454 Рік тому +27

      ​@@programmingwithnickabsolute chad

    • @ntre.
      @ntre. 4 місяці тому +11

      this is a tutorial for beginners, to help them dip their toes in c++ and raylib. no need to over-complicate it as that will only put people who dont understand the syntax of pointers ect

    • @RueKit-s9d
      @RueKit-s9d 2 місяці тому

      There is no bad code as long as it works and no bugs. And coding can be done in many ways, as long as it works.
      Maybe should be better

    • @princeshantu2480
      @princeshantu2480 2 місяці тому

      Then u have -0 knowledge of coding