VB.net计时器滴答事件问题

时间:2011-03-17 09:21:27

标签: vb.net loops timer

我编写了以下代码来构建一个基本游戏,让两辆赛车(randomgenerator)让用户在最终结果上下注。我的某些地方出现了严重的错误,因为我的赛车只有比赛中途然后被卡在起跑线后面并且程序不断重复哔哔声和messsagebox.show并将结果添加到“不正确的”框中。我花了最后三天试图弄清楚我哪里出错了,我只是错过了它。感谢您抽出宝贵时间来看看这个。我真的很感激任何建议。

终止应用程序的唯一方法是点击“停止调试”。

Screen shot of application

Option Explicit On
Option Strict On

Public Class MainForm

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub


    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'fills the list box with items, then selects the first item
        resultListBox.Items.Add("No guess")
        resultListBox.Items.Add("Tie")
        resultListBox.Items.Add("Red Car wins")
        resultListBox.Items.Add("White car wins")
        resultListBox.SelectedIndex = 0
    End Sub

    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
        raceTimer.Enabled = True

    End Sub

    Private Sub raceTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles raceTimer.Tick
        ' moves two cars from a starting line to a finish line on the form
        ' displays a message indicating the race results
        ' calculates and displays the number of times the user selects the
        ' correct race result from the resultlistbox, and the number
        ' of times the user selects an incorrect race result

        Dim randomgenerator As New Random
        Dim whitenewlocation As Integer
        Dim rednewlocation As Integer
        Dim raceresult As String
        Dim userchoice As String
        Dim finishline As Integer = finishTextBox.Left + 1
        Static numbercorrect As Integer
        Static numberincorrect As Integer

        ' save the user's list box selection, then disable the list box
        userchoice = resultListBox.SelectedItem.ToString
        resultListBox.Enabled = False

        'calculate the new location of each picture box's right border
        ' don't allow the right border to go beyond the finish line
        whitenewlocation = whitePictureBox.Right + randomgenerator.Next(0, 11)
        If whitenewlocation > finishline Then
            whitenewlocation = finishline
        End If
        rednewlocation = redPictureBox.Right + randomgenerator.Next(0, 11)
        If rednewlocation > finishline Then
            rednewlocation = finishline
        End If

        'move each picture box toward the finish line
        whitePictureBox.SetBounds(whitenewlocation - whitePictureBox.Width, 0, 0, 0, BoundsSpecified.X)
        redPictureBox.SetBounds(rednewlocation - redPictureBox.Width, 0, 0, 0, BoundsSpecified.X)

        'the following selection structure is processed only when at least
        ' one of the picture boxes is at the finish line
        If whitePictureBox.Right = finishline _
            OrElse redPictureBox.Right = finishline Then
            'disable the timer
            raceTimer.Enabled = False
        End If

        'sound a beep to indicate the end of the race
        For x As Integer = 1 To 5
            Console.Beep(100, 100)
        Next x

        'store the result of the race in a variable
        If whitenewlocation = rednewlocation Then
            raceresult = "Tie"
        ElseIf whitenewlocation > rednewlocation Then
            raceresult = "White car wins"
        Else
            raceresult = "red car wins"
        End If

        'display the race results
        MessageBox.Show("Race Over!" & ControlChars.NewLine & raceresult, _
        "Car Race", MessageBoxButtons.OK, MessageBoxIcon.Information)

        'move the picture boxes back to the starting line
        whitePictureBox.SetBounds(12, 0, 0, 0, BoundsSpecified.X)
        redPictureBox.SetBounds(12, 0, 0, 0, BoundsSpecified.X)


        'if the user did not want to guess the race results, then
        'don't update or display the counter values; otherwise,
        'compare the race results to the user's selection and update
        ' the appropriate counter, then display both counter values
        If userchoice <> "no guess" Then
            If raceresult = userchoice Then
                numbercorrect = numbercorrect + 1
            Else
                numberincorrect = numberincorrect + 1
            End If
            correctLabel.Text = Convert.ToString(numbercorrect)
            incorrectLabel.Text = Convert.ToString(numberincorrect)
        End If

        'enable the list box
        resultListBox.Enabled = True
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

你的计时器正在做每一个滴答的一切。处理比赛结束的代码在计时器的每个滴答处理。它应该包含在处理竞赛结束的if逻辑中。

在评论“发出哔哔声以表明比赛结束后”的所有内容应该在比赛结束时检查:

If whitePictureBox.Right = finishline  OrElse redPictureBox.Right = finishline Then
    'put all logic of the "ending" of the race here
end if

因此,此代码将按预期工作:

Private Sub raceTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RaceTimer.Tick
    ' moves two cars from a starting line to a finish line on the form
    ' displays a message indicating the race results
    ' calculates and displays the number of times the user selects the
    ' correct race result from the resultlistbox, and the number
    ' of times the user selects an incorrect race result

    Dim randomgenerator As New Random
    Dim whitenewlocation As Integer
    Dim rednewlocation As Integer
    Dim raceresult As String
    Dim userchoice As String
    Dim finishline As Integer = finishTextBox.Left + 1
    Static numbercorrect As Integer
    Static numberincorrect As Integer

    ' save the user's list box selection, then disable the list box
    userchoice = resultListBox.SelectedItem.ToString
    resultListBox.Enabled = False

    'calculate the new location of each picture box's right border
    ' don't allow the right border to go beyond the finish line
    whitenewlocation = whitePictureBox.Right + randomgenerator.Next(0, 11)
    If whitenewlocation > finishline Then
        whitenewlocation = finishline
    End If
    rednewlocation = redPictureBox.Right + randomgenerator.Next(0, 11)
    If rednewlocation > finishline Then
        rednewlocation = finishline
    End If

    'move each picture box toward the finish line
    whitePictureBox.SetBounds(whitenewlocation - whitePictureBox.Width, 0, 0, 0, BoundsSpecified.X)
    redPictureBox.SetBounds(rednewlocation - redPictureBox.Width, 0, 0, 0, BoundsSpecified.X)

    'the following selection structure is processed only when at least
    ' one of the picture boxes is at the finish line
    If whitePictureBox.Right = finishline _
        OrElse redPictureBox.Right = finishline Then
        'disable the timer
        RaceTimer.Enabled = False
        'sound a beep to indicate the end of the race
        For x As Integer = 1 To 5
            Console.Beep(100, 100)
        Next x

        'store the result of the race in a variable
        If whitenewlocation = rednewlocation Then
            raceresult = "Tie"
        ElseIf whitenewlocation > rednewlocation Then
            raceresult = "White car wins"
        Else
            raceresult = "red car wins"
        End If

        'display the race results
        MessageBox.Show("Race Over!" & ControlChars.NewLine & raceresult, _
        "Car Race", MessageBoxButtons.OK, MessageBoxIcon.Information)

        'move the picture boxes back to the starting line
        whitepicturebox.SetBounds(12, 0, 0, 0, BoundsSpecified.X)
        redpicturebox.SetBounds(12, 0, 0, 0, BoundsSpecified.X)


        'if the user did not want to guess the race results, then
        'don't update or display the counter values; otherwise,
        'compare the race results to the user's selection and update
        ' the appropriate counter, then display both counter values
        If userchoice <> "no guess" Then
            If raceresult = userchoice Then
                numbercorrect = numbercorrect + 1
            Else
                numberincorrect = numberincorrect + 1
            End If
            correctlabel.Text = Convert.ToString(numbercorrect)
            incorrectlabel.Text = Convert.ToString(numberincorrect)
        End If

        'enable the list box
        Resultlistbox.Enabled = True
    End If


End Sub