步驟 3:加入倒數計時器
因為這是計時測驗,您將加入倒數計時器。您的程式需要在遊戲進行中追蹤剩餘秒數。
若要加入倒數計時器
加入稱為 timeLeft 的 int (Integer),就像您先前的做法一樣。您的程式碼應該看起來與下列範例相同。
Public Class Form1 ' Create a Random object to generate random numbers. Dim randomizer As New Random ' These Integers will store the numbers ' for the addition problem. Dim addend1 As Integer Dim addend2 As Integer ' This Integer will keep track of the time left. Dim timeLeft As Integer
public partial class Form1 : Form { // Create a Random object to generate random numbers. Random randomizer = new Random(); // These ints will store the numbers // for the addition problem. int addend1; int addend2; // This int will keep track of the time left. int timeLeft;
現在,您需要實際執行計算的機制,例如計時器。移至 [Windows Form 設計工具],並自 [工具箱] (從 [元件] 類別) 將 Timer 控制項拖曳至表單。它會出現在 [Windows Form 設計工具] 底端的灰色區域中。
按一下您剛加入的 [timer1] 圖示,將 [Interval] 屬性設定為 [1000]。這樣會導致每秒引發一次 Tick 事件。然後,按兩下圖示加入 Tick 事件處理常式。IDE 會切換至程式碼編輯器並跳至新的事件處理常式方法。加入下列陳述式。
Private Sub Timer1_Tick() Handles Timer1.Tick If (timeLeft > 0) Then ' Display the new time left ' by updating the Time Left label. timeLeft = timeLeft - 1 timeLabel.Text = timeLeft & " seconds" Else ' If the user ran out of time, stop the timer, show ' a MessageBox, and fill in the answers. Timer1.Stop() timeLabel.Text = "Time's up!" MessageBox.Show("You didn't finish in time.", "Sorry") sum.Value = addend1 + addend2 startButton.Enabled = True End If End Sub
private void timer1_Tick(object sender, EventArgs e) { if (timeLeft > 0) { // Display the new time left // by updating the Time Left label. timeLeft = timeLeft - 1; timeLabel.Text = timeLeft + " seconds"; } else { // If the user ran out of time, stop the timer, show // a MessageBox, and fill in the answers. timer1.Stop(); timeLabel.Text = "Time's up!"; MessageBox.Show("You didn't finish in time.", "Sorry"); sum.Value = addend1 + addend2; startButton.Enabled = true; } }
根據您已加入的值,計時器會藉由每秒檢查一次 timeLeft int (Integer) 是否大於 0,檢查時間是否結束。如果大於 0,表示還有剩餘時間。首先,計時器會將 timeLeft 減 1,然後更新 timeLabel 控制項的 [Text] 屬性,讓使用者看到還剩多少時間。
如果沒有剩餘時間,計時器會停止並將 timeLabel 控制項文字變更為顯示 [Time's up!]。訊息方塊隨即出現,告訴使用者測驗已結束。答案隨即出現,在這個案例中是將 addend1 和 addend2 相加。startButton 控制項的 [Enabled] 屬性會設定為 true,以再次啟用按鈕。如此一來,使用者就能再次開始測驗。
您已加入 if else 陳述式,讓程式知道如何做判斷。if else 陳述式看起來如下。
If (something your program will check) Then ' statements that will get executed ' if the thing that the program checked is true Else ' statements that will get executed ' if the thing that the program checked is NOT true End If
if (something your program will check) { // statements that will get executed // if the thing that the program checked is true } else { // statements that will get executed // if the thing that the program checked is NOT true }
仔細查看您在 else 區塊中加入用來顯示加法問題答案的陳述式。
sum.Value = addend1 + addend2
sum.Value = addend1 + addend2;
您可能已看出,addend1 + addend2 會將兩個值相加。第一個部分 (sum.Value) 使用 NumericUpDown 控制項的 [Value] 屬性來顯示正確答案。稍後,當您想要檢查測驗的答案時,也會用到 [Value] 屬性。
NumericUpDown 控制項可讓使用者輕鬆輸入數字,這就是為何使用控制項來顯示數學問題答案的原因。因為所有答案都是從 0 至 100 的數字,所以您將預設的 [Minimum] 和 [Maximum] 屬性保持設定為 0 和 100。這樣會使得控制項只允許使用者輸入從 0 至 100 的數字。因為答案只能為整數,您將 [DecimalPlaces] 屬性設定為 0,這表示使用者不能輸入小數 (如果您要允許使用者輸入 3.141 而不是 3.1415,您可以將 [DecimalPlaces] 屬性設定為 3)。
將三行程式碼加入至 StartTheQuiz() 方法的結尾,使程式碼看起來如下。
''' <summary> ''' Start the quiz by filling in all of the problems ''' and starting the timer. ''' </summary> ''' <remarks></remarks> Public Sub StartTheQuiz() ' Fill in the addition problem. addend1 = randomizer.Next(51) addend2 = randomizer.Next(51) plusLeftLabel.Text = addend1.ToString plusRightLabel.Text = addend2.ToString sum.Value = 0 ' Start the timer. timeLeft = 30 timeLabel.Text = "30 seconds" Timer1.Start() End Sub
/// <summary> /// Start the quiz by filling in all of the problems /// and starting the timer. /// </summary> public void StartTheQuiz() { // Fill in the addition problem. addend1 = randomizer.Next(51); addend2 = randomizer.Next(51); plusLeftLabel.Text = addend1.ToString(); plusRightLabel.Text = addend2.ToString(); sum.Value = 0; // Start the timer. timeLeft = 30; timeLabel.Text = "30 seconds"; timer1.Start(); }
現在,當測驗開始時,它會將 timeLeft int (Integer) 設定為 30,並將 timeLabel 控制項的 [Text] 屬性變更為 30 秒。然後,它會呼叫 Timer 控制項的 Start() 方法來開始倒數計時 (它還不會檢查答案,這是接下來要討論的部分)。
儲存並執行您的程式。當您按一下 [開始] 按鈕時,計時器應該會開始倒數計時。當時間結束時,測驗就會結束,答案也會出現。下列圖片顯示正在進行的測驗。
進行中的數學測驗
若要繼續或檢視
若要移到下一個教學課程步驟,請參閱步驟 4:加入 CheckTheAnswer() 方法。
若要回到上一個教學課程步驟,請參閱步驟 2:建立隨機加法問題。