Tutorial 3: Create a Matching Game

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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

  • Store objects, such as icons, in a List object.

  • Use a foreach loop in Visual C# or a For Each loop in Visual Basic to iterate through items in a list.

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

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

  • Make a timer that counts down and then fires an event exactly once after being started.

    When you finish this tutorial, 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.

If you get stuck or have programming questions, try posting your question on one of the MSDN forums. See Visual Basic Forum and Visual C# Forum. Also, there are great, free video learning resources available to you. To learn more about programming in Visual Basic, see Visual Basic Fundamentals: Development for Absolute Beginners. To learn more about programming in Visual C#, see C# Fundamentals: Development for Absolute Beginners.

Title Description
Step 1: Create a Project and Add a Table to Your Form Begin by creating the project and adding a TableLayoutPanel control to keep the controls aligned properly.
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, so that each game is different.
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 to keep track of the time that has passed in the game.
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.