步骤 3:添加一个倒计时计时器

因为这是一个计时测验,所以您需要添加一个倒计时计时器。您的程序需要随着游戏的进行跟踪剩余的秒数。

添加倒计时计时器

  1. 添加一个名为**“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;
    
  2. 现在,您需要一个实际执行计数的对象,例如计时器。转到 Windows 窗体设计器,并将 Timer 控件从工具箱(位于**“组件”**类别中)拖动到窗体上。它将显示在 Windows 窗体设计器底部的灰色区域内。

  3. 单击刚刚添加的**“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。)

  4. 将三行代码添加到 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() 方法来开始倒计时。它现在还不会检查答案,那是接下来的任务。

  5. 保存并运行程序。当单击**“开始”**按钮时,计时器应开始倒计时。当时间用完时,测验将结束,并显示答案。下图显示正在进行中的测验。

    正在进行中的数学测验

    数学测验正在进行中

继续或查看