Have you ever dreamed of developing your own game but felt it was too complicated? The good news is that starting in the world of game development is easier than you think. In this tutorial, we will take you on a fun, step-by-step journey to create a classic Memory (Matching) Game using C# and the Windows Forms environment in Visual Studio.
This project is an ideal starting point for beginners, as it covers fundamental programming concepts such as user interface (UI) design, event handling, randomization, and implementing game logic. By the end of this article, you will not only have a fully functional game but also a deeper understanding of the basics of desktop application development.
Windows Forms App (.NET Framework)
and select it.MemoryGame
, and then click Create.Once the project is created, Visual Studio will open a blank form named Form1
. Let's customize it:
Text
property and change its value to "Memory Game".Size
property to 550, 550
or manually drag the form's corners to get a symmetrical square.TableLayoutPanel
control and drop it onto the form. This control is essential for arranging the game's icons neatly in a grid.Now, let's make the layout panel fill the game window and prepare it to hold the icons:
TableLayoutPanel
you just added.BackColor
: Set it to a calm color like CornflowerBlue
to distinguish the game's background.Dock
: Set it to Fill
to make the panel automatically expand to fill the entire form.CellBorderStyle
: Change it to Inset
to add visible borders between cells.TableLayoutPanel
to open its task menu. Click Add Row twice, and then Add Column twice to create a 4x4
grid (16 cells).Our game will use a set of symbols from the Webdings
font, which is built into Windows. We will place each symbol inside a Label
control in each cell of the grid.
Label
control and drop it into the first cell of the TableLayoutPanel
.Label
and, in the Properties window, modify the following properties:
BackColor
: Make it the same as the panel's background color (CornflowerBlue
) to hide the icon initially.AutoSize
: Set it to False
.Dock
: Set it to Fill
.TextAlign
: Set it to MiddleCenter
to place the icon exactly in the middle of the cell.Font
: Choose Webdings
font with a size of 48
.Text
: Temporarily change the default text to the letter c
to see what the icon looks like.Label
you just created and pasting it into each of the remaining cells.To make the game fun every time, the icons must be distributed randomly. We will do this through code.
1. Open the form's code editor by right-clicking on Form1.cs
in the Solution Explorer and choosing View Code.
public partial class Form1 : Form
{
// Used to generate random numbers
Random random = new Random();
// A list of characters representing the icons in the Webdings font
// Each character is duplicated to create matching pairs
List icons = new List()
{
"!", "!", "N", "N", ",", ",", "k", "k",
"b", "b", "v", "v", "w", "w", "z", "z"
};
// ... remaining code
}
3. Create a method that randomly assigns each icon from the list to a Label
. Add this method inside the Form1
class:
private void AssignIconsToSquares()
{
// Iterate over each Label in the TableLayoutPanel
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
if (iconLabel != null)
{
int randomNumber = random.Next(icons.Count);
iconLabel.Text = icons[randomNumber];
// Change the font color to match the background, hiding the icon
iconLabel.ForeColor = iconLabel.BackColor;
icons.RemoveAt(randomNumber);
}
}
}
For more information on how lists work in C#, you can visit Microsoft's official documentation for the List<T> Class.
We need two references to track the first and second Label
clicked, as well as a Timer to hide the two icons if they don't match.
// Variables to track clicks
Label firstClicked = null;
Label secondClicked = null;
2. From the designer view, drag a Timer
control from the Toolbox and drop it anywhere on the form. It will appear in the bottom tray. Select it and change its Interval
property to 750
(which is 750 milliseconds).
3. Now, we need to create a single Click
event handler for all the Label
controls.
private void label_Click(object sender, EventArgs e)
{
// If the timer is running, ignore any other clicks
if (timer1.Enabled == true)
return;
Label clickedLabel = sender as Label;
if (clickedLabel != null)
{
// If the icon is already visible, do nothing
if (clickedLabel.ForeColor == Color.Black)
return;
// If this is the first click
if (firstClicked == null)
{
firstClicked = clickedLabel;
firstClicked.ForeColor = Color.Black; // Show the icon
return;
}
// If this is the second click
secondClicked = clickedLabel;
secondClicked.ForeColor = Color.Black;
// Check for a winner
CheckForWinner();
// If the two icons match
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
}
else
{
// If they don't match, start the timer to hide them after a short delay
timer1.Start();
}
}
}
When the icons don't match, the timer will run. We need to program what happens when the timer's interval has passed.
private void timer1_Tick(object sender, EventArgs e)
{
// Stop the timer
timer1.Stop();
// Hide both icons again
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
// Reset the variables so the player can try again
firstClicked = null;
secondClicked = null;
}
Finally, add a method to check if the player has revealed all matching pairs:
private void CheckForWinner()
{
// Iterate over every Label on the board
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
if (iconLabel != null)
{
// If we find any icon that is still hidden, the game isn't over
if (iconLabel.ForeColor == iconLabel.BackColor)
return;
}
}
// If no hidden icons were found, the player has won
MessageBox.Show("You've matched all the icons!", "Congratulations");
Close(); // Close the game
}
The programming is almost complete! One final step remains: calling the icon distribution method when the game starts.
public Form1()
{
InitializeComponent();
AssignIconsToSquares(); // Distribute the icons when the game starts
}
Now, press F5 or the green Start button in Visual Studio to compile and run your program. You will see your game window, ready to play! Click on the squares and try to find the matching pairs.
Congratulations! You have successfully built a complete memory game from scratch using C# and Windows Forms. Through this project, you have learned how to design interfaces, handle events, use timers, and apply basic game logic—essential skills in any developer's toolkit.
For deeper learning, you can start a structured learning journey with the C# learning path on Microsoft Learn.