× Main Menu

C# Tutorial – Create a Classic Snake Game in Visual Studio

In this tutorial, we will take an indebt consider how to create a super fun classic snake game in visual studio using C# programming language. I still remember this game from my old NOKIA phones, it has gone through lots of different iterations over the years but the game is still lots of fun. We will be creating this awesome game in Visual Studio using the Windows Form Application template. We will not be using any game engines or external libraries to make this game. All you need is any version of visual studio and you can simply follow the tutorial through.

This tutorial is based on a game created by Michiel Wouters @ https://www.youtube.com/watch?v=i6W-aGhlq7M. He made this awesome game and we have found it to be a very effective tool to teach coding. Therefore we are going to create a text-based tutorial around it.  Michiel has a lot more online tutorials and you should check them out.

Lesson Objectives –

  • To create a snake game in visual studio
  • To create and manage an array of snake parts in the game
  • To spawn and respawn food across the screen
  • To detect hit test with the border and snakes own body
  • Start and restart the game
  • Keep score in the game
  • Manage the project and follow good programming practice by using comments and indentation
  • Using different OOP (object-oriented programming) classes to allocate the snake body and game controls
  • Using the system PAINT event to draw and animate SNAKE parts across the screen
  • Using Keyboard events and optimising the events to respond to up, down, left and right keys

Demo –


22 responses to “C# Tutorial – Create a Classic Snake Game in Visual Studio”

  1. Kevie Craig says:

    I’m positive my code is correct. It runs. I did have to add a line to the Program.cs main to actually get it to do anything. Not sure if I did that correctly [Form1 newGame = new Form1();], but you didn’t show that window. Nothing pops up when I run it. It gets to generateFood() inside startGame(), but after that the code stops running. I feel like the gameTimer.Tick += updateSreen line is supposed to draw something, but it doesn’t.

  2. Anhar Ali says:

    Hi Kevie, we are using a windows form application for this tutorial. From the comment I’m not sure how you followed the tutorial here, this program has 3 different classes added to it and it has the main game window running with all the components. Did you add the timer for the game.?

  3. kyle says:

    hello, iv followed this guide carefully up to writing code in form1.cs..
    i went ahead and wrote the first code i was told to enter

    public partial class Form1 : Form
    private List Snake = new List(); // creating a list array for the snake
    private Circle food = new Circle(); // creating a single circle class called food

    public Form1()

    new settings();

    gameTimer.Interval = 1000 / settings.Speed;
    gameTimer.Tick += updateScreen;


    all of that is fine but a red line appears under startGame(); and im not sure why? so i left it anyway and carried on following the guide and entering the code, i noticed these things are also being highlighted by a red line

    [i] i wrote this non caps like it says in the guide, but its highlighted with a red line

    these things above including startGame are all highlighted red so i ran the code in debugger because it was annoying me, and it gave me 20 errors saying the things i mentioned above do not exist in the current context, im not sure why as i copied this guide very carefully? iv read through it three times and i can’t see what’s gone wrong, its windows form application ect so i know its not that, can anyone help me please?

  4. kyle says:

    i got up to forms1.cs and typed the code very carefully following exactly what it says in the guide case sensitive, immediately i noticed a redline highlighting startGame();
    i ignored it and continued up until food function where i stopped because there are multiple words or letters highlighted like this, the following are highlighted red

    [i] < not capitalised like in the guide

    these are all highlighted red through the code that is written EXACTLY as shown in the guide, i ran it in debuggers and got 20 errors saying, " the name ' die ' doesn't exist in the current context "
    each error was the same for each of these words above, i went back through the guide 3 more times before giving up because i really can't find where i've gone wrong i feel like something is missing from the guide that defines these words and letters? it is windows form application, can someone help me please?

  5. montana says:

    how do you work this thing I don`t know how to work it at all

  6. Anhar Ali says:

    I think you might have missed the die function fro the code. The die function will end the game and show the end text to the form. Can you double check this.

  7. Reece says:

    What would be the appropriate coding to have game play with snake running through the edges of the canvas to come out the other side?

  8. Ryan says:

    Where did you get the updatescreen? i looked all thru the tutorial and nothing is showing up

  9. Gintas says:

    Tutorials first step incorrect. Need to choose project template visual c#->windows desktop->windows forms apps . Template populate program.cs with required statements.

  10. Damka says:

    How do we write that little star in this part:
    new Rectangle(
    Snake[i].x Settings.Width,
    Snake[i].y Settings.Height,
    Settings.Width, Settings.Width

  11. Foxxhunter says:

    I always get errors regards to Keypress, why is that ?

  12. Nicole Schmidlin says:

    How can I make this program run by pressing a button?????? The form loads when I use private void button1_Click(object sender, EventArgs e)


    but I can’t play it.

  13. Rácz Balázs says:

    Dear Author,
    I followed the instructions in the PDF, and I ended up not having any warning signs, but when I start the game (F5) the head doesn’t show up, Do you have any idea what can cause this?

  14. Feuzme says:

    The code code work, but i don’t understand why it semms that the Food isn’t where my red circle is… When I run in debug with some breakpoints the code works, but normal speed don’t work.

  15. Aroun Le Bricodeur says:

    No, but yes it’s the tutorial that worries … Ha bah no! I just did it and everything works flawless … Thanks for your work I’m learning a lot of stuff ..
    I specify that I am FR and that the translation does not work on the tutorial so it is very clear!

  16. Léon23 says:

    i have an System.NullReferenceException in the class Input at the line 22. i check my code and i searsh on google but nothing. please help me

  17. Shawn says:

    Only Error im getting is with the gameTimer.Tick += updateScreen; its saying no overload for ‘updateScreen’ matches delegate ‘EventHandler’

    any suggestions?

  18. mendes says:

    i have followed the tutorial but I have ended up with a window without any circles (red-fruit or black – head)! Do you know which step should i have followed better in order to generate those circles at all

  19. Aech says:

    it cannot eat the generated food it just passes the food. even though i thoroughly follow the instructions .

  20. MattBoyce_12 says:

    For some reason I am unable to see the food, I know it is there because I can accidentally run into it and the Snake gets bigger but I cannot see it.

  21. MattBoyce_12 says:

    nevermind, I fixed it

  22. Patrik says:

    I get an error under this class ” private void movePlayer() ”

    // If there are no collisions then we continue moving the snake and its parts
    snake[i].x = snake[i – 1].x;
    snake[i].y = snake[i – 1].y;

    I can start the game if i change the value from 1 to 0, but then i can only eat 1 apple then its game over. Dont really know whats wrong. I get an unhadled exception error when i try to debug.

Comment on this tutorial and let us know how you got on -

%d bloggers like this: