Step 7: Keep Pairs Visible

The game works well, as long as the player only clicks pairs of icons that don't match. But consider what should happen when the player clicks 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 clicked.

To keep pairs visible

  1. Add the following if statement to the label_Click() event handler method, near the end 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();
        }
    }
    
    
    

    The first line of the if statement you just added checks whether the icon in the first label that the player clicks is the same as the icon in the second label. If the icons are the same, the program executes the three statements between the curly braces in C# or the three statements within the if statement in Visual Basic. The first two statements reset the firstClicked and secondClicked reference variables so that they don't keep track of any of the labels. (You may recognize those two statements from the timer's Tick event handler.) The third statement is a return 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 the firstClicked reference variable equal to null 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 assignment and comparison.

  2. Save and run the program, and start clicking in the form. If you click a pair that doesn't match, the timer's Tick event triggers, and both icons disappear. If you click 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

    Game that you create in this tutorial

To continue or review

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft