C# Tutorial – How to make a Classic Snakes Game with Windows form and Visual Studio [Updated]

Hi Welcome to this tutorial. This tutorial is an updated version of the C# Snakes game tutorial we have done before on the website.  This video tutorial will explain all of the core components needed to make this classic game in windows form in visual studio with C# programming language. We wont be using any oher game frameworks to make this work however we will be using some OOP programming practices in this tutorial. Everything you need to know and how to use them inside of the game effectively is explained in the tutorial.

Lesson objectives –

  1. Make a full snakes game in visual studio
  2. Using custom classes to load and reload default settings
  3. Using custom classes to draw the snake and food to the game
  4. Working with Keyboard controls
  5. Keeping score and high score in the current game session
  6. Able to take snap of the game when it ends
  7. Starting and Restarting the game to replay

Full Video Tutorial on How to make the classic snake game in Visual Studio with C#

 

Download the Full Project and Source Code on the MOO ICT GitHub Repo

 

Source Code –

Circle Class –

This class will help us draw circles to the form, we will use this one to determine the X and Y position of the circles in the screen. This will be used for both the SNAKE and the Food objects inside of the game.

 

Settings Class

This is the settings class for the game. This class will be used to load up the default settings for the game objects only. It will load the size in height and width of the circles that will be drawn to the project also the direction the snake should be moving when the game initially loads.

 

Main Form C# Scripts

This is the main form1.cs code. In this script we have given all of the instructions necessary for the game and how it will behave when the game loads up. We have several events in the code below for example the Key Down, Key Up, Timer,  2 Click Events and a Paint Event. Also we have 3 custom functions such as Restart Game, Game Over and Eat Food function for the snake.

 

 

 




2 responses to “C# Tutorial – How to make a Classic Snakes Game with Windows form and Visual Studio [Updated]”

  1. Volkan Coskun says:

    is there any reason you have flags to indicate left,right,up & down?
    Also, is the KeyIsUp function needed ?

  2. Volkan Coskun says:

    I refactored your code to :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    static class Constants
    {
    public const int headPos = 0;
    public const int gameTimerInterval = 500;
    }

    namespace snake
    {
    public partial class Form1 : Form
    {
    private List Snake = new List();
    private Circle food = new Circle();
    int maxWidth;
    int maxHeight;
    Random rand = new Random();

    public Form1()
    {
    InitializeComponent();
    new setting();
    }
    private void KeyIsDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.Left && setting.direction != “RIGHT”)
    {
    setting.direction = “LEFT”;
    }
    if (e.KeyCode == Keys.Right && setting.direction != “LEFT”)
    {
    setting.direction = “RIGHT”;
    }
    if (e.KeyCode == Keys.Up && setting.direction != “DOWN”)
    {
    setting.direction = “UP”;
    }
    if (e.KeyCode == Keys.Down && setting.direction != “UP”)
    {
    setting.direction = “DOWN”;
    }
    }
    private void StartGame(object sender, EventArgs e)
    {
    RestartGame();
    }
    private void GameTimerEvent(object sender, EventArgs e)
    {
    checkFood();

    for (int bodyIndex = Snake.Count – 1; bodyIndex >= 0; bodyIndex–)
    {
    if (bodyIndex == Constants.headPos)
    {
    updateSnakePos();
    }
    else
    {
    Snake[bodyIndex].X = Snake[bodyIndex – 1].X;
    Snake[bodyIndex].Y = Snake[bodyIndex – 1].Y;
    }
    }
    checkSnakeCollision();
    picCanvas.Invalidate();
    }
    private void updateSnakePos()
    {
    switch (setting.direction)
    {
    case “LEFT”:
    Snake[Constants.headPos].X–;

    if (Snake[Constants.headPos].X maxWidth)
    {
    gameOver();
    }
    break;
    case “DOWN”:
    Snake[Constants.headPos].Y++;

    if (Snake[Constants.headPos].Y > maxHeight)
    {
    gameOver();
    }
    break;
    case “UP”:
    Snake[Constants.headPos].Y–;

    if (Snake[Constants.headPos].Y < 0)
    {
    gameOver();
    }
    break;
    default:
    break;
    }
    }
    private void drawSnake(object sender, PaintEventArgs e)
    {
    Graphics canvas = e.Graphics;

    for (int i = 0; i < Snake.Count; i++)
    {
    canvas.FillEllipse((i == Constants.headPos) ? Brushes.Black : Brushes.DarkGreen, new Rectangle(Snake[i].X * setting.Width, Snake[i].Y * setting.Height, setting.Width, setting.Height));
    }
    canvas.FillEllipse(Brushes.DarkRed, new Rectangle(food.X * setting.Width, food.Y * setting.Height, setting.Width, setting.Height));
    }

    private void RestartGame()
    {
    maxWidth = picCanvas.Width / setting.Width – 1;
    maxHeight = picCanvas.Height / setting.Height – 1;

    Snake.Clear();

    startButton.Enabled = false;

    Circle head = new Circle { X = rand.Next(2, maxWidth), Y = rand.Next(2, maxHeight)};
    Snake.Add(head); // adding the head part of the snake to the list

    for (int i = 0; i < 3; i++)
    {
    Circle body = new Circle();
    Snake.Add(body);
    }

    food = new Circle { X = rand.Next(2, maxWidth), Y = rand.Next(2, maxHeight) };

    gameTimer.Start();
    }

    private void checkSnakeCollision()
    {
    for (int j = 1; j < Snake.Count; j++)
    {
    if (Snake[Constants.headPos].X == Snake[j].X && Snake[Constants.headPos].Y == Snake[j].Y)
    {
    gameOver();
    }
    }
    }

    private void gameOver()
    {
    gameTimer.Interval = Constants.gameTimerInterval;
    gameTimer.Stop();
    startButton.Enabled = true;
    }
    private void checkFood()
    {
    if (Snake[Constants.headPos].X == food.X && Snake[Constants.headPos].Y == food.Y)
    {
    Circle body = new Circle
    {
    X = Snake[Snake.Count – 1].X,
    Y = Snake[Snake.Count – 1].Y
    };

    if(gameTimer.Interval / 2 == 0)
    {
    gameTimer.Interval = 5;
    }
    else
    {
    gameTimer.Interval = gameTimer.Interval / 2;
    }
    Snake.Add(body);

    food = new Circle { X = rand.Next(2, maxWidth), Y = rand.Next(2, maxHeight) };
    }
    }
    }
    }

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

%d bloggers like this: