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.
In this tutorial, both Visual C# and Visual Basic are covered, so focus on information specific to the programming language that you're using.
Begin by creating the project and adding a TableLayoutPanel control.
Add a Random object and a List object, to create a list of icons.
Assign the icons randomly to the Label controls.
Add a Click event handler that changes the color of the label that is clicked.
Add reference variables to keep track of which labels are clicked.
Add a timer to the form.
Keep pairs of icons visible, if a matching pair is selected.
Add a CheckForWinner() method to verify whether the player won.
Try other features, such as changing icons and colors, adding a grid, and adding sounds. Try making the board bigger and adjusting the timer.