Creating a basic Snake Game using WinUI 3 in C# (C# 13) and .NET 9 involves the following steps:
🧩 Prerequisites
- Visual Studio 2022 or newer with:
- .NET 9 SDK installed
- WinUI 3 workload enabled
- Basic knowledge of XAML, C#, and UI layout
🐍 Snake Game – WinUI 3 Project Structure
We'll build a simple version where:
- The snake moves on a grid.
- It grows when it eats food.
- Game over occurs if the snake hits itself.
✅ Step-by-step Guide
1. Create the WinUI 3 Project
Open Visual Studio → Create new project → Select Blank App, Packaged (WinUI 3 in Desktop)
Name it SnakeGameWinUI
Target framework: .NET 9
2. Update MainWindow.xaml
This will contain a Canvas for drawing the game elements.
<Window
x:Class="SnakeGameWinUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Snake Game - WinUI 3"
Width="600"
Height="600">
<Grid>
<Canvas x:Name="GameCanvas" Background="White"/>
</Grid>
</Window>
3. Add Key Event Handling in MainWindow.xaml.cs
Update MainWindow.xaml.cs to handle key input and start the game loop.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SnakeGameWinUI;
public sealed partial class MainWindow : Window
{
private const int GridSize = 20;
private const int CellSize = 20;
private List<Point> snake = new();
private Point direction = new(1, 0);
private Point food = new();
private DispatcherTimer gameTimer = new();
private SolidColorBrush snakeBrush = new(Windows.UI.Color.FromArgb(255, 0, 128, 0));
private SolidColorBrush foodBrush = new(Windows.UI.Color.FromArgb(255, 255, 0, 0));
public MainWindow()
{
InitializeComponent();
InitializeGame();
this.KeyDown += OnKeyDown;
}
private void InitializeGame()
{
snake.Clear();
snake.Add(new Point(10, 10));
direction = new Point(1, 0);
PlaceFood();
gameTimer.Interval = TimeSpan.FromMilliseconds(200);
gameTimer.Tick += GameLoop;
gameTimer.Start();
}
private void PlaceFood()
{
var rand = new Random();
food = new Point(rand.Next(0, GridSize), rand.Next(0, GridSize));
}
private void GameLoop(object? sender, object e)
{
var head = snake[0];
var newHead = new Point(head.X + direction.X, head.Y + direction.Y);
// Collision with wall or self
if (newHead.X < 0 || newHead.X >= GridSize ||
newHead.Y < 0 || newHead.Y >= GridSize ||
snake.Contains(newHead))
{
GameOver();
return;
}
snake.Insert(0, newHead);
if (newHead.Equals(food))
{
PlaceFood();
}
else
{
snake.RemoveAt(snake.Count - 1);
}
DrawGame();
}
private void DrawGame()
{
GameCanvas.Children.Clear();
foreach (var segment in snake)
{
var rect = new Rectangle
{
Width = CellSize,
Height = CellSize,
Fill = snakeBrush
};
Canvas.SetLeft(rect, segment.X * CellSize);
Canvas.SetTop(rect, segment.Y * CellSize);
GameCanvas.Children.Add(rect);
}
var foodRect = new Rectangle
{
Width = CellSize,
Height = CellSize,
Fill = foodBrush
};
Canvas.SetLeft(foodRect, food.X * CellSize);
Canvas.SetTop(foodRect, food.Y * CellSize);
GameCanvas.Children.Add(foodRect);
}
private void OnKeyDown(object sender, KeyRoutedEventArgs e)
{
switch (e.Key)
{
case Windows.System.VirtualKey.Left when direction.X != 1:
direction = new Point(-1, 0);
break;
case Windows.System.VirtualKey.Right when direction.X != -1:
direction = new Point(1, 0);
break;
case Windows.System.VirtualKey.Up when direction.Y != 1:
direction = new Point(0, -1);
break;
case Windows.System.VirtualKey.Down when direction.Y != -1:
direction = new Point(0, 1);
break;
}
}
private void GameOver()
{
gameTimer.Stop();
var dialog = new ContentDialog
{
Title = "Game Over",
Content = "Press OK to restart.",
CloseButtonText = "OK"
};
_ = dialog.ShowAsync();
InitializeGame(); // Restart automatically after OK
}
}
🧪 Run the Application
- Press
F5 to run the app.
- Use arrow keys to control the snake.
- Eat red squares to grow.
- Avoid hitting walls or yourself!
📦 Optional Enhancements
You can later add:
- Score counter
- Difficulty levels
- Sound effects
- Pause/resume functionality
- Better graphics using sprites or animations
📁 Final Project Structure
SnakeGameWinUI/
├── MainWindow.xaml
├── MainWindow.xaml.cs
├── App.xaml
├── App.xaml.cs
└── Package.appxmanifest
Let me know if you'd like the full downloadable project template or want to convert this into a UWP or MAUI app!