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

你已创建了一个有趣的游戏,但需要添加其他项来完成制作。该游戏应当在玩家获胜时结束,因此你需要添加 CheckForWinner() 方法以验证玩家是否获胜。

添加方法以验证玩家是否获胜

  1. 在你的代码底部,CheckForWinner() 事件处理程序下方添加一个 timer1_Tick() 方法,如以下代码所示。

    ''' <summary> 
    ''' Check every icon to see if it is matched, by  
    ''' comparing its foreground color to its background color.  
    ''' If all of the icons are matched, the player wins 
    ''' </summary> 
    Private Sub CheckForWinner()
    
        ' Go through all of the labels in the TableLayoutPanel,  
        ' checking each one to see if its icon is matched 
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel = TryCast(control, Label)
            If iconLabel IsNot Nothing AndAlso 
               iconLabel.ForeColor = iconLabel.BackColor Then Exit Sub 
        Next 
    
        ' If the loop didn't return, it didn't find  
        ' any unmatched icons 
        ' That means the user won. Show a message and close the form
        MessageBox.Show("You matched all the icons!", "Congratulations")
        Close()
    
    End Sub
    
    /// <summary> 
    /// Check every icon to see if it is matched, by  
    /// comparing its foreground color to its background color.  
    /// If all of the icons are matched, the player wins 
    /// </summary> 
    private void CheckForWinner()
    {
        // Go through all of the labels in the TableLayoutPanel,  
        // checking each one to see if its icon is matched 
        foreach (Control control in tableLayoutPanel1.Controls)
        {
            Label iconLabel = control as Label;
    
            if (iconLabel != null) 
            {
                if (iconLabel.ForeColor == iconLabel.BackColor)
                    return;
            }
        }
    
        // If the loop didn’t return, it didn't find 
        // any unmatched icons 
        // That means the user won. Show a message and close the form
        MessageBox.Show("You matched all the icons!", "Congratulations");
        Close();
    }
    

    该方法使用 foreach 循环(Visual C# 中)或 For Each 循环(Visual Basic 中)遍历 TableLayoutPanel 中的每个标签。它使用相等运算符(在 Visual C# 中为 ==,在 Visual Basic 中为 =)检查每个标签的图标颜色以验证它是否与背景匹配。如果颜色匹配,图标将保持不可见,玩家还没有匹配所有剩余的图标。在这种情况下,程序使用 return 语句跳过其余方法。如果循环遍历所有标签而不执行 return 语句,则意味着窗体上的所有图标均已匹配。该程序将显示一个恭喜玩家获胜的 MessageBox,然后调用窗体的 Close() 方法来结束游戏。

  2. 接下来,让标签的 Click 事件处理程序调用新的 CheckForWinner() 方法。请确保程序在显示玩家选择的第二个图标后立即检查是否有赢家。查找设置第二个选中图标颜色的行,然后在其后直接调用 CheckForWinner() 方法,如以下代码所示。

    ' 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
    
    ' Check to see if the player won
    CheckForWinner()
    
    ' 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 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;
    
    // Check to see if the player won
    CheckForWinner();
    
    // 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;
    }
    
  3. 保存并运行程序。玩游戏并匹配所有图标。当你获胜时,该程序将显示一个祝贺性的 MessageBox(如下图所示),然后关闭该框。

    具有 MessageBox 的匹配游戏

    具有 MessageBox 的匹配游戏

继续或查看