在這四個系列教學課程中,您會製作一個配對遊戲,讓玩家配對隱藏的圖示對。
您的比對遊戲程式需要追蹤玩家選擇的標籤控制項。 玩家選擇第一個標籤之後,程式應該會顯示圖示。 選擇第二個標籤之後,程式應該會短暫地顯示這兩個圖示。 然後它會隱藏這兩個圖示。
您的程式使用 參考變數,記錄您首先和其次選擇的標籤。 定時器會隱藏圖示,並控制顯示圖示的時間長度
- 新增標籤參考。
- 新增定時器。
先決條件
本教學課程是以先前的教學課程為基礎,建立比對遊戲應用程式,將圖示新增至比對遊戲。 請先完成這些教學課程。
新增標籤參考
在本節中,您會將兩個 參考變數新增至程式代碼。 它們會追蹤或參考 Label 物件。
在
Form1.cs或Form1.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 關鍵詞。
程序啟動時,如果是 C#,則 firstClicked 和 secondClicked 皆設定為 null;如果是 Visual Basic,則設定為 Nothing。
在 Click 或
Form1.cs中修改Form1.vb事件處理程式,以使用新的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; } } }
儲存並執行您的程式。 選擇其中一個標籤控件,其圖示隨即出現。 選擇下一個標籤控件,並注意不會發生任何動作。
只會顯示所選的第一個圖示。 其他圖示是看不見的。
程式已經在追蹤玩家選擇的第一個標籤。
參考 firstClicked 不是 C# 中的 null,也不是 Visual Basic 中的 Nothing。
當您的 if 語句發現 firstClicked 不等於 null 且也不等於 Nothing時,它會執行這些語句。
新增定時器
比對遊戲應用程式會使用 Timer 控件。 定時器會等候,然後引發事件,這被稱為 刻度。 定時器可以啟動動作或定期重複動作。
在您的程式中,定時器可讓玩家選擇兩個圖示。 如果圖示不相符,它會在短時間內再次隱藏兩個圖示。
選取 [工具箱] 索引標籤,在 [元件] 類別中,按兩下或拖曳 [定時器] 元件至表單。 名為 timer1的定時器圖示會出現在表單下方的空間中。
選取 Timer1 圖示以選取定時器。 在 [屬性] 視窗中,選取 [屬性] 按鈕以檢視屬性。
將 Interval 屬性設定為 750,也就是 750 毫秒。
Interval 屬性會告訴定時器在 刻度之間的等待時間長度,當觸發其 Tick 事件時。 您的程式會呼叫 Start() 方法來在玩家選擇第二個標籤之後啟動定時器。
選擇定時器控件圖示,然後按 Enter,或按兩下定時器。 IDE 會將空的 Tick 事件處理程式新增至
Form1.cs或Form1.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() 方法來確保定時器未執行。
- 它使用兩個參考變數,
firstClicked和secondClicked,使玩家選擇的兩個標籤圖示再次變得不可見。 - 它會將
firstClicked和secondClicked參考變數重設為 C# 中的null,並在 Visual Basic 中Nothing。
移至程式代碼編輯器,並將程式代碼新增至
label1_Click()或Form1.cs中Form1.vb事件處理程式方法的頂端和底部。 此程式代碼會檢查定時器是否已啟用、設定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 事件處理程式會隱藏兩個圖示,並重設firstClicked和secondClicked參考變數。 表單已準備好讓玩家選擇另一組圖示。
注意
如果您複製並貼上 label1_Click() 程式代碼區塊,而不是手動輸入程式碼,請務必取代現有的 label1_Click() 程式代碼。
否則,您最後會有重複的程式碼區塊。
- 儲存並執行您的程式。 選取正方形並顯示圖示。 選擇另一個方形。 圖示會短暫出現,然後兩個圖示都會消失。
您的程式現在會追蹤您選擇的第一個和第二個圖示。 它會使用定時器暫停,再讓圖示消失。
後續步驟
前進到下一個教學課程,以瞭解如何完成比對遊戲。