如果游戏始终将同一图标隐藏在相同的位置,这就没有挑战性了。您需要向窗体中的 Label 控件随机分配图标。为此,需要添加 AssignIconsToSquares() 方法。
向每个标签分配一个随机图标
在添加以下代码之前,请考虑该方法的工作原理。有一个新的关键字:foreach(Visual C# 中)或 For Each(Visual Basic 中)。(其中有一行被故意注释掉,本过程的结尾对此进行了解释)。
''' <summary> ''' Assign each icon from the list of icons to a random square ''' </summary> ''' <remarks></remarks> Private Sub AssignIconsToSquares() ' The TableLayoutPanel has 16 labels, ' and the icon list has 16 icons, ' so an icon is pulled at random from the list ' and added to each label For Each control In TableLayoutPanel1.Controls Dim iconLabel As Label = TryCast(control, Label) If iconLabel IsNot Nothing Then Dim randomNumber As Integer = random.Next(icons.Count) iconLabel.Text = icons.ElementAt(randomNumber) ' iconLabel.ForeColor = iconLabel.BackColor icons.RemoveAt(randomNumber) End If Next End Sub
/// <summary> /// Assign each icon from the list of icons to a random square /// </summary> private void AssignIconsToSquares() { // The TableLayoutPanel has 16 labels, // and the icon list has 16 icons, // so an icon is pulled at random from the list // and added to each label foreach (Control control in tableLayoutPanel1.Controls) { Label iconLabel = control as Label; if (iconLabel != null) { int randomNumber = random.Next(icons.Count); iconLabel.Text = icons[randomNumber]; // iconLabel.ForeColor = iconLabel.BackColor; icons.RemoveAt(randomNumber); } } }
按上一步骤所示,添加 AssignIconsToSquares() 方法。可以将它放在紧靠步骤 2:添加随机对象和图标列表中添加的代码的下方。
在 AssignIconsToSquares() 方法中有一个新增功能:foreach 循环(在 Visual C# 中)或 For Each(在 Visual Basic 中)。无论何时想要重复执行相同操作,您都可以使用 foreach 循环。在本例中,您要对 TableLayoutPanel 中的每个标签执行相同的语句,下面的代码对此进行了解释。
For Each control In TableLayoutPanel1.Controls ' The statements you want to execute ' for each label go here ' The statements use iconLabel to access ' each label's properties and methods Next
foreach (Control control in tableLayoutPanel1.Controls) { // The statements you want to execute // for each label go here // The statements use iconLabel to access // each label's properties and methods }
说明
其中使用了名称 iconLabel 和控件,因为它们具有描述性。您可以将这些名称替换为任何名称,其作用完全相同(只要您更改了大括号内每个语句中的名称)。
AssignIconsToSquares() 方法遍历 TableLayoutPanel 中的每个 Label 控件,并对每个控件执行相同的语句。这些语句从您在步骤 2:添加随机对象和图标列表中添加的列表提取随机图标。(这就是该列表中每个图标都有两个的原因,因此将向随机 Label 控件分配一对图标。)
一旦程序启动,您就需要调用 AssignIconsToSquares() 方法。如果编写 Visual C# 代码,则在 Form1 构造函数中紧靠对 InitializeComponent() 方法的调用下方添加一条语句,这样窗体便可以调用新方法以在显示之前对自身进行设置。
public Form1() { InitializeComponent(); AssignIconsToSquares(); }
对于 Visual Basic,首先添加该构造函数,然后添加对该构造函数的方法调用。在刚创建的 AssignIconsToSquares() 方法之前,首先请键入代码 Public Sub New()。当您按 Enter 键移动到下一行时,IntelliSense 应显示下面的代码来完成该构造函数。
Public Sub New() ' This call is required by Windows Form Designer InitializeComponent() ' Add any initialization after the InitializeComponent() call End Sub
添加 AssignIconsToSquares() 方法调用以使构造函数如下所示。
Public Sub New() ' This call is required by Windows Form Designer InitializeComponent() ' Add any initialization after the InitializeComponent() call AssignIconsToSquares() End Sub
保存并运行程序。它应该显示一个窗体,其中每个标签都分配了随机图标。
关闭程序,然后重新运行。现在每个标签都分配了不同的图标,如下图所示。
具有随机图标的匹配游戏
现在停止该程序,并在 foreach 循环中取消注释该代码行。
iconLabel.ForeColor = iconLabel.BackColor
iconLabel.ForeColor = iconLabel.BackColor;
单击**“全部保存”**工具栏按钮保存您的程序,然后运行该程序。图标看起来消失了 - 只显示蓝色背景。但是,图标是随机分配的,仍然存在。因为图标与背景颜色相同,所以它们不可见。
继续或查看
若要转到下一个教程步骤,请参见步骤 4:向每个标签添加一个 Click 事件处理程序。
若要返回上一个教程步骤,请参见步骤 2:添加随机对象和图标列表。