步骤 6:添加计时器

接下来,您要向匹配游戏中添加计时器。

添加计时器

  1. 转到 Windows 窗体设计器中的工具箱。双击**“Timer”(在“组件”**类别中)并向窗体添加计时器,其图标出现在窗体下方的灰框中,如下图所示。

    Timer

    计时器

  2. 单击**“timer1”图标以选择计时器。将“Interval”属性设置为 750,但将“Enabled”属性保留设置为“False”“Interval”**属性通知计时器在计时周期之间等待的时间长度,因此,该属性通知计时器在触发第一个 Tick 事件之前等待四分之三秒(750 毫秒)。您不希望计时器在程序启动时启动。而是在玩家单击第二个标签时使用 Start() 方法启动计时器。

  3. 双击 Windows 窗体设计器中的 Timer 控件图标以添加 Tick 事件处理程序,如下面的代码所示。

    ''' <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>
    ''' <remarks></remarks>
    Private Sub Timer1_Tick() Handles Timer1.Tick
    
        ' 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 = Nothing
        secondClicked = Nothing
    
    End Sub
    
    /// <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() 方法停止计时器。然后,它使用两个引用变量 firstClicked 和 secondClicked 来捕获玩家单击的两个标签,并再次使其图标不可见。最后,它将 firstClicked 和 secondClicked 引用变量重置为 null(Visual C# 中)或 Nothing(Visual Basic 中)。这一点很重要,因为这是该程序重置本身的方式。现在,它不跟踪任何 Label 控件,并已准备好再次捕获玩家的第一次单击。

    说明说明

    Timer 对象具有 Start() 方法和 Stop() 方法,分别用以启动和停止计时器。如果您在“属性”窗口中将计时器的“Enabled”属性设置为“True”,则只要程序开始运行,计时器就会开始计时。但是,如果将该属性保留设置为“False”,则在调用计时器的 Start() 方法之前,计时器不会开始计时。

    说明说明

    通常,计时器会使用“Interval”属性确定要在计时周期之间等待的毫秒数,从而反复触发其 Tick 事件。您可能已经注意到在 Tick 事件中调用计时器的 Stop() 方法的方式。它会将计时器置于“单触发模式”,这样,当调用 Start() 方法时,它会等待间隔的时间长度并触发单一 Tick 事件。

  4. 若要查看正在使用的新计时器,请转至代码编辑器,将以下代码添加到 label_Click() 事件处理程序方法的顶部和底部。(您要将 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>
    ''' <remarks></remarks>
    Private Sub label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click, Label1.Click
    
        ' 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) Then
            Return
        End If
    
        Dim clickedLabel As Label = TryCast(sender, Label)
    
        If clickedLabel IsNot Nothing Then
            ' If the clicked label is black, the player clicked
            ' an icon that's already been revealed --
            ' ignore the click
            If (clickedLabel.ForeColor = Color.Black) Then
                Return
            End If
    
            ' If firstClicked is Nothing, 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 Is Nothing) Then
                firstClicked = clickedLabel
                firstClicked.ForeColor = Color.Black
                Return
            End If
    
            ' 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
    
            ' 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()
        End If
    
    End Sub
    
    /// <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 label_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”**属性来检查计时器是否已启动。这样,如果玩家单击第一个和第二个 Label 控件,且计时器启动,则单击第三个控件将不会执行任何操作。

    该方法底部的代码设置 secondClicked 引用变量,以便它跟踪玩家单击的第二个 Label 控件,它将该标签的图标颜色设置为黑色以使其可见。然后,它在单触发模式下启动计时器,以便它等待 750 毫秒,然后触发 Tick 事件。然后,计时器的 Tick 事件处理程序会隐藏这两个图标,并重置 firstClicked 和 secondClicked 引用变量,以便窗体准备就绪以供玩家单击另一个图标。

  5. 保存并运行程序。单击一个图标,它将变得可见。

  6. 单击另一个图标。它会短暂显示,然后两个图标都消失。多次重复此操作。窗体现在跟踪您单击的第一个和第二个图标,并在使图标消失之前使用计时器暂停。

继续或查看