Tutorial 4: Create a Matching Game

In this tutorial, you build a matching game, where the player must match pairs of hidden icons. You learn how to:

  • Hold objects using a List object.

  • Use a foreach loop in Visual C# or a For Each loop in Visual Basic.

  • Keep track of a form's state using reference variables.

  • Build an event handler that you can use with multiple objects.

  • Make a timer fire exactly once when started.

When you finish, your program will look like the following picture.

Game that you create in this tutorial

Game that you create in this tutorial

Note

In this tutorial, both Visual C# and Visual Basic are covered, so focus on information specific to the programming language that you're using.

Title

Description

Step 1: Create a Project and Add a Table to Your Form

Begin by creating the project and adding a TableLayoutPanel control.

Step 2: Add a Random Object and a List of Icons

Add a Random object and a List object, to create a list of icons.

Step 3: Assign a Random Icon to Each Label

Assign the icons randomly to the Label controls.

Step 4: Add a Click Event Handler to Each Label

Add a Click event handler that changes the color of the label that is clicked.

Step 5: Add Label References

Add reference variables to keep track of which labels are clicked.

Step 6: Add a Timer

Add a timer to the form.

Step 7: Keep Pairs Visible

Keep pairs of icons visible, if a matching pair is selected.

Step 8: Add a Method to Verify Whether the Player Won

Add a CheckForWinner() method to verify whether the player won.

Step 9: Try Other Features

Try other features, such as changing icons and colors, adding a grid, and adding sounds. Try making the board bigger and adjusting the timer.