× 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




7 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()
    {
    InitializeComponent();

    new settings();

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

    startGame();

    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
    eat
    die

    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

    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?

  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?

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

%d bloggers like this: