Building a Simple Snake Game in C#

Share

Learning Programming Through a Real Project

One of the best ways to learn programming is by building something interactive. Instead of only reading about variables and loops, projects allow you to see how programming concepts work together to create actual behavior on the screen.

For this project, we built a simple Snake game in C# using the terminal window. Even though the graphics are only text characters, the programming concepts behind the game are very real. The project introduced game loops, movement systems, lists, collision detection, rendering, and state management.


Setting Up the Game

The first part of the project focused on creating the basic game settings and importing the libraries we needed.

using System;using System.Collections.Generic;using System.Threading;

These libraries gave us access to important tools like console output, lists, and timing controls.

We then created variables for the game board and snake:

int width = 30;int height = 15;int score = 0;int snakeX = 15;int snakeY = 7;

This introduced one of the most important programming concepts: variables. Variables allow programs to store information that changes while the game is running.

The snake’s position uses X and Y coordinates, which are commonly used in games and graphics systems.


Movement and Direction

Next, we created the snake’s movement system:

int moveX = 1;int moveY = 0;

These variables control the snake’s direction.

Every frame, the game updates the snake’s position:

snakeX += moveX;snakeY += moveY;

This introduced real-time movement and frame updates. Instead of manually moving the snake each time, the game continuously updates its position automatically.

We also added keyboard controls:

if (key == ConsoleKey.UpArrow){    moveX = 0;    moveY = -1;}

This allowed the player to change direction using the arrow keys.


Building the Snake Body

One of the most important parts of the project was creating the snake body using a List.

List<(int X, int Y)> snake = new();

Each coordinate pair inside the list represents one body segment of the snake.

As the snake moves, the game adds new positions:

snake.Add((snakeX, snakeY));

and removes old ones:

snake.RemoveAt(0);

This creates the illusion of movement.

This section introduced data structures, which are used heavily throughout software development.


Adding Food and Randomness

To make the game interactive, we added food that spawns randomly inside the map.

Random random = new();int foodX = random.Next(1, width - 1);int foodY = random.Next(1, height - 1);

When the snake touches the food:

if (snakeX == foodX && snakeY == foodY){    score++;    snakeLength++;}

the score increases and the snake grows longer.

This introduced randomness and conditional logic into the program.


The Main Game Loop

The core of the entire project was the game loop:

while (!gameOver)

This loop continuously repeats the game logic until the player loses.

Inside the loop, the game:

  • checks keyboard input
  • updates movement
  • checks collisions
  • redraws the screen
  • repeats again

This is the same general structure used in most real-time games.


Collision Detection

The game constantly checks whether the snake collides with walls or its own body.

if (snakeX == 0 || snakeX == width - 1){    gameOver = true;}

We also used a loop to detect collisions with the snake body:

foreach (var bodyPart in snake)

Collision systems are one of the most important concepts in game development because they allow the game to react to interactions between objects.


Rendering the Game

Every frame, the terminal screen is cleared and redrawn.

Console.Clear();

The game then loops through every coordinate on the board and decides what to display:

Console.Write("#");Console.Write("*");Console.Write("O");

Walls are drawn with #, food with *, and the snake with O.

Although simple, this introduced the concept of rendering frames continuously.


Adding a Restart Feature

Finally, we expanded the project by adding a restart system.

Console.WriteLine("Press R to Restart");

If the player presses R, the game resets all important variables back to their starting values.

This introduced state management, which is the process of resetting and maintaining application data during runtime.


Final Thoughts

Even though this Snake game was intentionally simple, it introduced many real programming concepts used in professional development. Through one small project, we worked with variables, loops, lists, rendering systems, input handling, collision detection, randomness, and state management.

Projects like this are valuable because they turn programming from abstract theory into something interactive and understandable. Building even a simple game demonstrates how many small systems work together to create a functioning application.