Step 7: Keep Pairs Visible
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
The game works well, as long as the player only chooses pairs of icons that don't match. But consider what should happen when the player chooses a matching pair. Instead of making the icons disappear by turning on the timer (using the Start()
method), the game should reset itself so that it's no longer keeping track of any labels using the firstClicked
and secondClicked
reference variables, without resetting the colors for the two labels that were chosen.
To keep pairs visible
Add the following
if
statement to thelabel_Click()
event handler method, near the end of the code just above the statement where you start the timer. Take a close look at the code while adding it to the program. Consider how the code works.// If the player gets this far, the timer isn't // running and firstClicked isn't null, // so this must be the second icon the player clicked // Set its color to black secondClicked = clickedLabel; secondClicked.ForeColor = Color.Black; // If the player clicked two matching icons, keep them // black and reset firstClicked and secondClicked // so the player can click another icon if (firstClicked.Text == secondClicked.Text) { firstClicked = null; secondClicked = null; return; } // If the player gets this far, the player // clicked two different icons, so start the // timer (which will wait three quarters of // a second, and then hide the icons) timer1.Start(); } }
' If the player gets this far, the timer isn't ' running and firstClicked isn't Nothing, ' so this must be the second icon the player clicked ' Set its color to black secondClicked = clickedLabel secondClicked.ForeColor = Color.Black ' If the player clicked two matching icons, keep them ' black and reset firstClicked and secondClicked ' so the player can click another icon If firstClicked.Text = secondClicked.Text Then firstClicked = Nothing secondClicked = Nothing Exit Sub End If ' If the player gets this far, the player ' clicked two different icons, so start the ' timer (which will wait three quarters of ' a second, and then hide the icons) Timer1.Start() End If End Sub
The first line of the
if
statement you just added checks whether the icon in the first label that the player chooses is the same as the icon in the second label. If the icons are identical, the program executes the three statements between the curly braces in C# or the three statements within theif
statement in Visual Basic. The first two statements reset thefirstClicked
andsecondClicked
reference variables so that they no longer keep track of any of the labels. (You may recognize those two statements from the timer's Tick event handler.) The third statement is areturn
statement, which tells the program to skip the rest of the statements in the method without executing them.If programming in Visual C#, you may have noticed that some of the code uses a single equal sign (
=
), while other statements use two equal signs (==
). Consider why=
is used in some places but==
is used in other places.This is a good example that shows the difference. Take a careful look at the code between the parentheses in the
if
statement.firstClicked.Text = secondClicked.Text
firstClicked.Text == secondClicked.Text
Then look closely at the first statement in the block of code after the
if
statement.firstClicked = Nothing
firstClicked = null;
The first of those two statements checks whether two icons are the same. Because two values are being compared, the Visual C# program uses the
==
equality operator. The second statement actually changes the value (called assignment), setting thefirstClicked
reference variable equal tonull
to reset it. That's why it uses the=
assignment operator instead. Visual C# uses=
to set values, and==
to compare them. Visual Basic uses=
for both variable assignment and comparison.Save and run the program, and then start choosing icons on the form. If you choose a pair that doesn't match, the timer's Tick event triggers, and both icons disappear. If you choose a matching pair, the new
if
statement executes, and the return statement causes the method to skip the code that starts the timer, so the icons stay visible, as shown in the following picture.Matching game with visible icon pairs
To continue or review
To go to the next tutorial step, see Step 8: Add a Method to Verify Whether the Player Won.
To return to the previous tutorial step, see Step 6: Add a Timer.