× 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 –

c-sharp-create-a-classic-snake-game-88795




5 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:

    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

    startGame();
    [i] < not capitalised like in the guide
    food
    die

    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?

  4. montana says:

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

  5. 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.

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

%d bloggers like this: