如果游戏始终将同一图标隐藏在相同的位置,这就没有挑战性了。您需要向窗体中的 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 = TryCast(control, Label) If iconLabel IsNot Nothing Then Dim randomNumber = random.Next(icons.Count) iconLabel.Text = icons(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 中)。无论何时想要重复执行相同操作,您都可以使用 For Each 循环。在本例中,您要对 TableLayoutPanel 中的每个标签执行相同的语句,下面的代码对此进行了解释。第一行创建存储每个控件一次一个名为 control 的一个变量,则该控件使用语句在此期间执行的循环。
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 Nextforeach (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 控件分配一对图标。)
仔细查看运行在 foreach 或 For Each 循环内的代码。此代码中重现此处。
Dim iconLabel = TryCast(control, Label) If iconLabel IsNot Nothing Then Dim randomNumber = random.Next(icons.Count) iconLabel.Text = icons(randomNumber) ' iconLabel.ForeColor = iconLabel.BackColor icons.RemoveAt(randomNumber) End IfLabel iconLabel = control as Label; if (iconLabel != null) { int randomNumber = random.Next(icons.Count); iconLabel.Text = icons[randomNumber]; // iconLabel.ForeColor = iconLabel.BackColor; icons.RemoveAt(randomNumber); }第一行转换 control 变量转换为名为 iconLabel的 Label 。是 if 语句检查以确定转换的行后工作。如果转换工作,在运行的 if 条语句。在 if 语句的第一行创建包含一个随机数分别对应于一个在图标的项列表 randomNumber 名为的变量。为此,它使用先前创建 Random 对象的 Next 方法。Next 方法返回随机数。此行也使用 icons 的 Count 属性列表来选择随机数的大小。下一行分配图标列表项到标签的 Text 属性。注释行在本主题的后面部分将。最后,在 if 语句的最后一行从列表中移除已添加到窗体的图标。
确保,因此,如果您为任何不确定的代码的某些部分执行,可以停留在代码元素的鼠标指针和检查发生的工具提示。
一旦程序启动,您就需要调用 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保存并运行程序。它应该显示一个窗体,其中每个标签都分配了随机图标。
关闭程序,然后重新运行。现在每个标签都分配了不同的图标,如下图所示。
具有随机图标的匹配游戏
.png)
现在停止该程序,并在 For Each 循环中取消注释该代码行。
iconLabel.ForeColor = iconLabel.BackColoriconLabel.ForeColor = iconLabel.BackColor;单击**“全部保存”**工具栏按钮保存您的程序,然后运行该程序。图标看起来消失了 - 只显示蓝色背景。但是,图标是随机分配的,仍然存在。因为图标与背景颜色相同,所以它们不可见。
继续或查看
若要转到下一个教程步骤,请参见步骤 4:向每个标签添加一个 Click 事件处理程序。
若要返回上一个教程步骤,请参见步骤 2:添加随机对象和图标列表。