教程:在匹配的游戏 WinForms 应用中显示消息
在此系列四个教程中,你将制作一个配对游戏,玩家需要匹配成对的隐藏图标。
在本教程中,你将修改匹配游戏,使匹配对保持可见,并在玩家获胜时显示祝贺消息。
本教程介绍如何:
- 保持对可见。
- 验证玩家是否获胜。
- 尝试其他功能。
先决条件
本教程基于前面的教程:
保持对可见
当玩家匹配对时,游戏应当进行重置,这样游戏就不再跟踪使用 firstClicked
和 secondClicked
引用变量的任何标签。
它不应重置匹配的两个标签的颜色。
这些标签将继续显示。
- 将以下
if
语句添加到label_Click()
事件处理程序方法。 将其置于代码末尾,就在启动计时器的语句上方。
// 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
语句检查玩家选择的第一个标签中的图标是否与第二个标签中的图标相同。
如果图标相同,程序将运行其三个语句。
前两个语句重置 firstClicked
和 secondClicked
引用变量。
它们不再跟踪任何标签。
第三个语句是一个 return
语句,它跳过方法中的其余语句而不运行它们。
- 运行程序,然后开始在窗体上选择正方形。
如果选择的是不匹配的对,则将触发计时器的 Tick 事件。 这两个图标都消失了。
如果选择匹配的对,则新的 if
语句会执行。
return 语句导致该方法跳过启动计时器的代码。
图标保持可见。
验证玩家是否获胜
你已经创建了一个有趣的游戏。 玩家获胜后,游戏应结束。 本部分添加了一种方法来验证玩家是否获胜。
- 将
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 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();
}
该方法在 C# 中使用另一个 foreach
循环,在 Visual Basic 中使用 For Each
循环来浏览 TableLayoutPanel中的每个标签。
它会检查每个标签的图标颜色,以验证它是否与背景匹配。
如果颜色匹配,图标将保持不可见,并且玩家未匹配所有剩余图标。
在这种情况下,程序使用 return
语句跳过该方法的其余部分。
如果循环通过所有标签而不执行 return
语句,这意味着表单上的所有图标都匹配。
程序显示一个 MessageBox 以祝贺玩家获胜,然后调用 Close()
方法来结束游戏。
- 让标签的 Click 事件处理程序调用新的
CheckForWinner()
方法。
// 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;
}
确保程序在显示玩家选择的第二个图标后立即检查优胜者。 查找在其中设置第二个所选图标颜色的行,然后在该行后面调用 CheckForWinner()
方法。
保存并运行程序。 玩游戏并匹配所有图标。 获胜后,程序会显示贺电。
选择“确定”后,匹配游戏将关闭。
尝试其他功能
你的匹配游戏已完成。 可以添加更多功能,使此游戏更具挑战性和有趣性。 下面是一些选项。
将图标和颜色替换为您选择的图标和颜色。
尝试查看标签的 ForeColor 属性。
添加一个游戏计时器,用于跟踪玩家获胜所需的时间。
可以添加一个标签,以在表单上显示经过的时间。 将其置于 TableLayoutPanel 上方。 将另一个计时器添加到窗体以跟踪时间。 使用代码在玩家启动游戏时启动计时器,并在与最后两个图标匹配后停止计时器。
在玩家找到匹配项时添加一种声音,在玩家揭开两个不匹配的图标时添加另一种声音,当程序再次隐藏图标时则添加第三种声音。
若要播放声音,可以使用 System.Media 命名空间。 有关详细信息,请参阅在 Windows 窗体应用中播放声音 (C#) 或如何在 Visual Basic 中播放音频。
让棋盘变大以增加游戏难度。
你需要的不仅仅是向 TableLayoutPanel 添加行和列。 还需要考虑创建的图标数。
如果玩家反应太慢,则隐藏第一个图标,使游戏更具挑战性。
后续步骤
祝贺! 你已完成这一系列教程。 已在 Visual Studio IDE 中完成这些编程和设计任务:
- 列表中存储的对象,如图标
- 在 C# 或 Visual Basic 中使用循环遍历列表
- 使用引用变量来跟踪状态
- 生成事件处理程序以响应多个对象的事件
- 添加了一个计时器,该计时器倒计时并触发事件
请继续访问本文,深入了解 Windows 窗体设计器。