次の方法で共有


手順 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 プロパティを更新します。

    残り時間がなくなると、タイマーは停止し、"Time's up!" と表示されるように timeLabel コントロールのテキストを変更します。クイズが終了したことをユーザーに知らせるメッセージ ボックスが表示されます。答えが (ここでは 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 は 2 つの値を合計します。最初の部分 (sum.Value) は、NumericUpDown コントロールの Value プロパティを使用して正しい答えを表示します。Value プロパティは、この後、クイズの答えを確認するときにも使用します。

    NumericUpDown コントロールを使用するとユーザーが簡単に数値を入力できるので、このコントロールを計算問題の答えに使用します。答えはすべて 0 ~ 100 の数値であるため、Minimum プロパティと Maximum プロパティは既定の設定値の 0 と 100 のままにします。これにより、ユーザーがこのコントロールに入力できるのは 0 ~ 100 の数値のみになります。また、答えは整数にしかならないため、DecimalPlaces プロパティは 0 のままにします。これにより、ユーザーが小数を入力できなくなります (ユーザーが 3.141 は入力でき、3.1415 は入力できないようにする場合は、DecimalPlaces プロパティを 3 に設定します)。

  4. StartTheQuiz() メソッドの最後に 3 行のコードを追加します。コードは次のようになります。

    ''' <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 seconds" に変更されます。また、Timer コントロールの Start() メソッドが呼び出され、カウントダウンが始まります (答えの確認は次の手順で設定するため、まだ行われません)。

  5. プログラムを保存し、実行します。[Start] ボタンをクリックすると、タイマーのカウントダウンが始まります。残り時間がなくなると、クイズが終了し、答えが表示されます。次の図は、実行中のクイズを示しています。

    実行中の計算クイズ

    実行中の計算クイズ

続行または確認するには