共用方式為


教學課程:將參考變數和計時器控制項新增至您的配對遊戲 WinForms 應用程式

在此系列四個教學課程中,您會建置配對遊戲,讓玩家在其中配對隱藏圖示。

您的配對遊戲程式需要追蹤玩家所選擇的 Label 控制項。 在玩家選擇第一個標籤之後,程式應該會顯示圖示。 在選擇第二個標籤之後,程式應該會短暫地顯示這兩個圖示。 然後,其會隱藏這兩個圖示。

您的程式會使用「參考變數」,追蹤您選擇的第一個和第二個標籤。 計時器會隱藏圖示,並控制顯示圖示多久時間

  • 新增標籤參考。
  • 新增計時器。

必要條件

本教學課程以先前的教學課程 (建立配對遊戲應用程式將圖示新增至您的配對遊戲) 為基礎。 請先完成這些教學課程。

新增標籤參考

在本節中,您會將兩個「參考變數」新增至您的程式碼。 其會追蹤或參考 Label 物件。

  1. 使用下列程式碼,將標籤參考加入至您的表單。

    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# 程式碼片段或 Visual Basic 程式碼片段。

Programming language control for Microsoft Learn

這些陳述式不會導致 Label 控制項出現在表單上,因為沒有 new 關鍵字。 當程式啟動時,firstClickedsecondClicked 都會設定為 null (適用於 C#) 或設定為 Nothing (適用於 Visual Basic)。

  1. 修改 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. 儲存並執行您的程式。 選擇其中一個 Label 控制項,其圖示就會出現。 選擇一個 Label 控制項,並注意什麼事也沒發生。

    Screenshot shows the Matching Game showing one icon.

    只有第一個選擇的圖示才會出現。 其他圖示會看不見。

程式已追蹤玩家選擇的第一個標籤。 參考 firstClicked 不是 C# 中的 null 或 Visual Basic 中的 Nothing。 當您的 if 陳述式發現 firstClicked 不等於 nullNothing 時,其會執行陳述式。

新增計時器

配對遊戲應用程式會使用 Timer 控制項。 計時器會等候,然後引發事件,稱為「刻度」。 計時器可以啟動動作或定期重複動作。

在您的程式中,計時器可讓玩家選擇兩個圖示。 如果圖示不符,其會在短時間內再次隱藏這兩個圖示。

  1. 選取 [工具箱] 索引標籤,然後在 [元件] 類別中,按兩下 [計時器] 元件或將其拖曳至表單。 稱為 timer1 的計時器圖示會出現在表單下方的空白處。

    Screenshot shows the timer icon below the form.

  2. 選取 Timer1 圖示以選取計時器。 在 [屬性] 視窗中,選取 [屬性] 按鈕來檢視屬性。

  3. Interval 屬性設定為 750,也就是 750 毫秒。

    Interval 屬性會告知計時器,當其觸發其 Tick 事件時,在「刻度」之間要等候多久的時間。 在玩家選擇第二個標籤之後,您的程式會呼叫 Start() 方法,以啟動計時器。

  4. 選擇計時器控制項圖示,然後按 Enter 鍵或按兩下計時器。 IDE 會新增空的刻度事件處理常式。 使用下列程式碼取代程式碼。

    /// <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;
    }
    

刻度事件處理常式會執行下列三件事情:

  • 其會呼叫 Stop() 方法來確定計時器並未執行。
  • 其會使用兩個參考變數 firstClickedsecondClicked,確定玩家所選兩個標籤的圖示再次看不見。
  • 其會將 firstClickedsecondClicked 參考變數重設為 Visual C# 中的 null 和 Visual Basic 中的 Nothing
  1. 移至程式碼編輯器,並將程式碼新增至 label1_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] 屬性的值來啟動。 如果玩家選擇第一個和第二個 Label 控制項,且計時器啟動了,則選擇第三個標籤後並不會執行任何動作。
  • 方法底部的程式碼會設定 secondClicked 參考變數,來追蹤第二個 Label 控制項。 然後,其會將該標籤圖示顏色設定為黑色,使其看得見。 然後,其會以一次性模式啟動計時器,因此會等待 750 毫秒,而後引發單一刻度。 計時器的刻度事件處理常式會隱藏兩個圖示,並重設 firstClickedsecondClicked 參考變數。 表單已準備好讓玩家選擇另一對圖示。

注意

如果您是透過複製並貼上 label1_Click() 程式碼區塊而非手動輸入程式碼,請務必取代現有的 label1_Click() 程式碼。 否則,您將會產生重複的程式碼區塊。

  1. 儲存並執行您的程式。 選取正方形,圖示就會看得見。 選擇另一個正方形。 圖示會短暫出現,然後這兩個圖示會消失。

您的程式現在會追蹤您選擇的第一個和第二個圖示。 其會使用計時器暫停,再讓圖示消失。

下一步

前進到下一個教學課程,以了解如何完成您的配對遊戲。