Programming a Snake Game in C# - Full Guide -

Programming a Snake Game in C# – Full Guide

Views: 67316
Like: 1552
Learn how to program a C# Snake game!

Download the assets here:

In this video you will learn how to write a Snake game in C# without using any game engine. We go over everything you need to know to program Snake step-by-step. The video even includes helpful animations to help you understand everything! The code for the game is written in C#, but the is well decoupled from the UI and can be adapted for other programming languages like Java and C++. Have fun developing a Snake game in C#!

0:00 – The Game
0:28 – Project Setup
1:37 – Importing Assets
3:04 – Grid Representation
4:26 – GridValue Enum
5:31 – Directions
9:53 – Positions
12:10 – The Game State
15:27 – Adding The Snake
16:49 – Adding Food
19:40 – Snake Helpers
21:10 – Snake Modification
23:20 – Moving The Snake
29:36 – Loading Images
32:10 – Color/Font Resources
34:30 – Coding UI
37:33 – Image Grid
41:28 – Adding Grid Images
44:35 – Bitmap Scaling & Removing Gaps
46:00 – Drawing The Game State
49:54 – Changing Direction (User Input)
51:33 – Moving Snake (Game Loop)
53:07 – Updating The Score
53:37 – A Strange Bug
56:13 – Changing Direction Properly
1:00:53 – Fixing Scaling
1:02:26 – Intro/End Screen
1:09:31 – Countdown
1:10:56 – Restarting The Game
1:12:56 – Googly Eyes!
1:16:08 – What The ****?
1:16:26 – Fixing Image Rotation
1:17:28 – RIP Animation
1:20:19 – Other Grid Sizes?
1:21:49 – Thanks!


  1. Нет реализации Google Play и SQL. 🙂

  2. Hi Otto, I would like to say this tutorial is absolutely the best ever. Thanks and well done!
    I have modified my version to include a pause/resume feature and currently playing on a 32 x 32 grid. The longest snake I have to date is 176 cells. This took several minutes to construct but with the pause feature I was able to stop for a tea break halfway through. 🙂

  3. When the game starts, my snake dies immediately. What’s the problem?

  4. Thank you very much! This is awesome! May I ask you how can I save the score with a nickname?

  5. I followed this guide as an intro to WPF during my studies.
    Tempo was great, explainations were great. Overall a pretty fun video!

  6. Dear OttoBotCode,
    Thank you so much for the video. You are explaining very well and the temp is good. I am still newbie in programming, but you are explaining in very understandable and helpful way.
    Before I started with the learning to code in c#, friend of mind recommended me to start to work under some projects following a step by step the professionalist. I am very glad that I found that your video. Thank you again for doing all this hard work.

    I followed all of your steps and I (thought I did the codes the same like you). Of course it is not the same because I have an issue for solving.

    Everything at the game goes at the way you did ( the colors, the snake, the food, the dead body snake, the counter from 3 to 1, the welcome text) except the important thing – the game it is not working prorerly(after the back counting the snake just dying and the game is restarting). It is not giving me any opportunity to move it.
    That was even when I wrote the codes with you. You showed that your game is working (I think may be from the 20min in the video) but mine did not moved at all. I reviewed and compared mine and your code tripple and everything seems the same as your. Of course I am sure I have missed someting😢.
    (By the way the 3 watching of the full video helped me to understand a lot of things).
    I decided to write you an comment, because I didn't find similar issue at the comments I read.

    The other think that I wanted to ask is – what I have to do to send the game to my friend to try it out?
    I tried, but on my computer is opening just on visual studio.
    Is it possible to set a instalation file, because I did not found it at the saved folder?

    I will be very glad if I receive your back comment.

    Thank you again for all your hard work under this project and thank you for sharing and helping people with it. Wish you many luck.

  7. Thats a nice Tutorial! One Tip: you can double a row of text by using ctrl+D. This speeds up code like:

    public readonly static Direction Left = new Direction(0, -1);

    public readonly static Direction Right = new Direction(0, 1);

    public readonly static Direction Up = new Direction(1, 0);

    public readonly static Direction Down = new Direction(-1, 0);

  8. Hi there, I’ve just emailed you again regarding my code. When I press any key to start the game the snake doesn’t move and dies on the spot. Do you know what I’ve done wrong?

  9. Hi Otto, Thank you so much for the tutorial. This helped me learn a lot about creating things in C#. I was using this to try and learn how to create windows and games in C#, however, what I want to do will need a lot larger grid then the 15×15 for snake. I was wondering if this method of creating and drawing the grid is scalable to something like H:350xW:1000? When I try to use large numbers with the code of the snake game (i.e H:200xW:400) the game window doesn't open.

  10. Hello Otto,
    I would like to thank you from the bottom of my heart about this guide but i would like to ask you if you can help me with a problem i got. Right now i am at 49:51 of the video and when i run the program it says Exceptions Unhandled in a small box, and behind a new screen pops up with the title Symbol Loading Skipped. Can you please help ?

  11. Hi,
    Can someone please explain to me why we override equals?

  12. hey! great video, im having trouble since this is kind of an old version , can u help and create an updated version? thanks!

  13. i need help but must send photo to see the problem where can send photo and you see

  14. ​ @OttoBotCode im having problem with this line "List<Position> empty = new List<Position>(Convert.ToInt32(EmptyPositions()));​" in the AddFood() method

  15. Hello, can you tell me how i change the speed of the snake ?

  16. hello OttoBotCode ! I would like to say that your tutorial is amazing to understand C# syntaxt and principles. Although, I have a question : In the Direction Class, won't the lines where you initialize LEFT, RIGHT, DOWN, UP create a spiral and themselves create a Direction class that will repeat these lines etc? because you used "new Direction(0, -1)" won't this create a new Direction class?
    PS : I'm french so my question could be badly formulated

  17. Hello OttobotCode i have a problem with the snake and the grid. The problem is that the snake and the grid are not showing on the screen but they exist so i can score points and the snake dies but i can not see anything that is happening

  18. I will Like and Subscribe in my 10 devices
    Very good video and easy way of coding
    thanks a lot , i hoke you will be doing such good coding videos

  19. Hey. May you tell which patterns of OOP do you use here, please?

  20. i have a problem with the snake not showing up when i start the game i tried to go back to check on the video but everything is right idk can anybody help ??

  21. Hey, I really love your content so far very high quality and informative. I unfortunately have been running into a problem for quite sometime now and can't seem to figure it out. At 44:34 is where I first start to have problems. Everything works up until then but, unlike you, when I run the program the empty grid image that is in the assets folder does not show up. I decided to continue till about 53:00 and the same issue still persits, none of the ImageSource images that are shown when you run it, show up for me. The images are indeed marked as "resource" and i have checked over most of the code. Any Ideas? Thanks

  22. At this part in the code under the gamestate class i am getting an error saying inconsistent accessibility for headposition, tailposition, and snakeposition. I have went over everything even rewatched your video up til this point to make sure everything was correct and it is and I can not figure out what is going on. Any ideas?

    public Position HeadPosition()

    return snakePositions.First.Value;

    public Position TailPosition()
    return snakePositions.Last.Value;

    public IEnumerable<Position> SnakePositions()
    return snakePositions;

  23. Scratch my last comment I figured it out. Thank you for this tutorial it is helping me alot!

  24. can somebody give me some advice? Mine works great but one error msg "System.NullReferenceException: Object reference not set to an instance of an object" on method named GetLastDirection

  25. where is the right place to call SnackPositions() ?

  26. hi i love the tutorial, but i have a problem with the code the food of the snake is not showing can you help me fix it?

  27. this is very good
    many thanks for your great effort

  28. hey i was following our steps but i run into problem, it tells me error on (Icon="Assets/icon.ico"). when i deleted it. it launched game but there no grid lines on game it only sad SCORE 0 and game box. i recheck everything but still no difference, even it shows (no issues found). everything is correct but still no lines on game. then i added game states but still nothing. can you help me?

  29. hi! ive ran into a bit of a problem where the grid doesnt appear when i launch the game

  30. really good tutorial! helped me with my college project!!

  31. HI! I got an error in the switch (e.Key). There is an error on the word key saying 'RoutedEventArgs' does not contain a definition for 'Key' and no accessible extension method 'Key' accepting a first argument of type 'RoutedEventArgs' could be found. What might be the problem?

    I'm not sure what I'm doing wrong so a help would be much appreciated!

  32. Could you please make tutorial about how to add a high score on the snake thank you.

  33. Hi there, haven't finished the tutorial yet but it seems really good. One problem I have is that App.xaml is not appearing on the solution explorer sidebar, what is the specific fix for that?

  34. This video is really great
    Hope you will continue making games with c#

  35. hi, I want to ask how to fix the error on position class line 15(translate is giving me an error what can i use instead).?

  36. bro i fucced everything up, i just want to commit suic1de

  37. Hello, i'm at 52:55, when i run the application sometimes the key presses do not work. And when they do, after i change the direction 3 times it stops working completely. Then the game "pauses"/freezes, not sure where i went wrong though.

  38. Amazing Tutorial by the way !!!! Can anyone help me with this : The tag 'ViewBox' does not exist in XML namespace ???

  39. By the way i found a bug…sometimes food appears on snake body…did you notice ?

  40. I am completely lost 🙂 My Snake turns dead just after one step. Can you share the source code? P.S. Step 1 – I've found an error, I just missed the loop in the GameLoop :). Step 2 – now I don't have a snake's head texture – I've just missed DrawSnakeHead() in drawGrid. Step 3 – snake's head not turning rightfully when moving to the left – Direction Left = 2700 instead of 270. Step 4 – score not updates at all – changed DrawGrid to Draw in MainLoop. Now all working perfectly 🙂
    I know how to add sound, but how to add background music in the loop?

  41. hey how could one make an 'easy gamemode' and once the snake hits the edge of the board it reapears on the other side?

  42. At 1:00:00 the snake still dies when I make it move in the opposite direction can anyone help me?

Leave a Reply

Your email address will not be published.