教程:将引用变量和计时器控件添加到匹配的游戏 WinForms 应用

在本系列的四个教程中,你将制作一个配对游戏,玩家需要将隐藏的图标配对。

匹配游戏程序需要跟踪玩家选择了哪些标签控件。 玩家选择第一个标签后,程序应显示图标。 选择第二个标签后,程序应短暂地显示这两个图标。 然后,它隐藏这两个图标。

程序通过引用变量跟踪第一次和第二次分别选择的标签控件。 计时器隐藏图标并控制显示图标的时间

  • 添加标签引用。
  • 添加计时器。

先决条件

本教程基于前面的教程,创建匹配的游戏应用程序将图标添加到匹配的游戏。 首先完成这些教程。

添加标签引用

在本部分中,你将向代码添加两个 引用变量。 它们跟踪或引用标签对象。

  1. 使用以下代码在 Form1.csForm1.vb中添加表单标签引用。

    public partial class Form1 : Form
    {
        // firstClicked points to the first Label control 
        // that the player clicks, but it will be null 
        // if the player hasn't clicked a label yet
        Label firstClicked = null;
    
        // secondClicked points to the second Label control 
        // that the player clicks
        Label secondClicked = null;
    

如果使用 C# ,请将代码放在左大括号之后,然后在类声明(public partial class Form1 : Form)之后。 如果使用 Visual Basic,请将代码放在类声明(Public Class Form1)之后。

这些语句不会导致标签控件显示在窗体上,因为没有 new 关键字。 程序启动时,firstClickedsecondClicked 都设置为 C# null 或 Visual Basic Nothing

  1. Form1.csForm1.vb 中修改 Click 事件处理程序,以使用新的 firstClicked 引用变量。 删除 label1_Click() 事件处理程序方法(clickedLabel.ForeColor = Color.Black;)中的最后一个语句,并将其替换为 if 语句,如下所示。

    /// <summary>
    /// Every label's Click event is handled by this event handler
    /// </summary>
    /// <param name="sender">The label that was clicked</param>
    /// <param name="e"></param>
    private void label1_Click(object sender, EventArgs e)
    {
        Label clickedLabel = sender as Label;
    
        if (clickedLabel != null)
        {
            // If the clicked label is black, the player clicked
            // an icon that's already been revealed --
            // ignore the click
            if (clickedLabel.ForeColor == Color.Black)
                return;
    
            // If firstClicked is null, this is the first icon 
            // in the pair that the player clicked,
            // so set firstClicked to the label that the player 
            // clicked, change its color to black, and return
            if (firstClicked == null)
            {
                firstClicked = clickedLabel;
                firstClicked.ForeColor = Color.Black;
    
                return;
            }
        }
    }
    

  1. 保存并运行程序。 选择其中一个标签控件,并显示其图标。 选择下一个标签控件,并注意到什么都没有发生。

    屏幕截图显示具有一个图标的匹配游戏。

    仅显示所选的第一个图标。 其他图标不可见。

该程序已经跟踪玩家选择的第一个标签。 引用 firstClicked 不是 C# 中的 null,也不是 Visual Basic 中的 Nothing。 当 if 语句发现 firstClicked 不等于 nullNothing时,它将执行这些语句。

添加计时器

匹配游戏应用使用 Timer 控件。 计时器等待后,将触发一个事件,称为一个“时钟周期”。 计时器可以启动操作或定期重复操作。

在程序中,计时器使玩家能够选择两个图标。 如果图标不匹配,它将在短时间内再次隐藏这两个图标。

  1. 选择“工具箱”选项卡,在“组件”类别中,双击“计时器”组件或将其拖到窗体中。 计时器图标(称为 timer1)显示在窗体下方的空间中。

    屏幕截图显示窗体下方的计时器图标。

  2. 选择 Timer1 图标以选择计时器。 在 属性 窗口中,选择 属性 按钮以查看属性。

  3. Interval 属性设置为 750,即 750 毫秒。

    “间隔”属性将通知计时器两个时钟周期之间的等待时长,或何时触发 Tick 事件。 程序调用 Start() 方法,以在玩家选择第二个标签后启动计时器。

  4. 选择计时器控件图标,然后按 Enter,或双击计时器。 IDE 将空 Tick 事件处理程序添加到 Form1.csForm1.vb。 将代码替换为以下代码。

    /// <summary>
    /// This timer is started when the player clicks 
    /// two icons that don't match,
    /// so it counts three quarters of a second 
    /// and then turns itself off and hides both icons
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void timer1_Tick(object sender, EventArgs e)
    {
        // Stop the timer
        timer1.Stop();
    
        // Hide both icons
        firstClicked.ForeColor = firstClicked.BackColor;
        secondClicked.ForeColor = secondClicked.BackColor;
    
        // Reset firstClicked and secondClicked 
        // so the next time a label is
        // clicked, the program knows it's the first click
        firstClicked = null;
        secondClicked = null;
    }
    

Tick 事件处理程序执行三项操作:

  • 它通过调用 Stop() 方法来确保计时器未运行。
  • 它使用两个引用变量(firstClickedsecondClicked)使玩家再次选择的两个标签的图标变得不可见。
  • 它将 firstClickedsecondClicked 引用变量重置为 C# 中的 null,并在 Visual Basic 中 Nothing
  1. 转到代码编辑器,将代码添加到 Form1.csForm1.vblabel1_Click() 事件处理程序方法的顶部和底部。 此代码将检查计时器是否已启用,设置 secondClicked 引用变量,然后启动计时器。 label1_Click() 事件处理程序方法现在如下所示:

    /// <summary>
    /// Every label's Click event is handled by this event handler
    /// </summary>
    /// <param name="sender">The label that was clicked</param>
    /// <param name="e"></param>
    private void label1_Click(object sender, EventArgs e)
    {
        // The timer is only on after two non-matching 
        // icons have been shown to the player, 
        // so ignore any clicks if the timer is running
        if (timer1.Enabled == true)
            return;
    
        Label clickedLabel = sender as Label;
    
        if (clickedLabel != null)
        {
            // If the clicked label is black, the player clicked
            // an icon that's already been revealed --
            // ignore the click
            if (clickedLabel.ForeColor == Color.Black)
                return;
    
            // If firstClicked is null, this is the first icon
            // in the pair that the player clicked, 
            // so set firstClicked to the label that the player 
            // clicked, change its color to black, and return
            if (firstClicked == null)
            {
                firstClicked = clickedLabel;
                firstClicked.ForeColor = Color.Black;
                return;
            }
    
            // 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 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();
        }
    }
    

  • 该方法顶部的代码通过检查 Enabled 属性的值来检查计时器是否已启动。 如果玩家选择第一个和第二个标签控件,并且计时器启动,则选择第三个标签不会执行任何操作。
  • 方法底部的代码将 secondClicked 引用变量设置为跟踪第二个 Label 控件。 然后,它将标签图标颜色设置为黑色,使其可见。 然后,它在单触发模式下启动计时器,以便在等待 750 毫秒后触发单个 Tick。 计时器的 Tick 事件处理程序隐藏两个图标,并重置 firstClickedsecondClicked 引用变量。 表单已准备好让玩家选择另一对图标。

注意

如果复制并粘贴 label1_Click() 代码块,而不是手动输入代码,请确保替换现有的 label1_Click() 代码。 否则,你将得到重复的代码块。

  1. 保存并运行程序。 选择一个正方形,图标变为可见。 选择另一个正方形。 图标会短暂显示,然后两个图标都消失。

你的程序现在跟踪你选择的第一个和第二个图标。 它使用计时器在使图标消失之前暂停。

后续步骤

转到下一教程,了解如何完成匹配游戏。