Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.net计时器滴答声事件问题_Vb.net_Loops_Timer - Fatal编程技术网

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

VB.net计时器滴答声事件问题,vb.net,loops,timer,Vb.net,Loops,Timer,我编写了以下代码来构建一个基本游戏,让两辆车比赛(randomgenerator),并让用户在最终结果上选择赌注。我在某个地方犯了严重的错误,我的赛车只跑了一半,然后被卡在起跑线后面,程序不断重复嘟嘟声和MessageBox.show,并将结果添加到“不正确”框中。在过去的三天里,我一直在努力找出我的错误所在,但我只是错过了。谢谢你花时间看这个。我真的很感激你的建议 终止应用程序的唯一方法是点击“停止调试” Option Explicit On Option Strict On Public

我编写了以下代码来构建一个基本游戏,让两辆车比赛(randomgenerator),并让用户在最终结果上选择赌注。我在某个地方犯了严重的错误,我的赛车只跑了一半,然后被卡在起跑线后面,程序不断重复嘟嘟声和MessageBox.show,并将结果添加到“不正确”框中。在过去的三天里,我一直在努力找出我的错误所在,但我只是错过了。谢谢你花时间看这个。我真的很感激你的建议

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

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

选项显式打开
选项严格限制在
公共类主窗体
私有子exitButton_Click(ByVal sender作为对象,ByVal e作为System.EventArgs)处理exitButton。单击
我
端接头
Private Sub main form_Load(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
'用项目填充列表框,然后选择第一个项目
resultListBox.Items.Add(“无猜测”)
resultListBox.Items.Add(“领带”)
resultListBox.Items.Add(“红色汽车获胜”)
resultListBox.Items.Add(“白车赢”)
resultListBox.SelectedIndex=0
端接头
私有子开始按钮\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理开始按钮。单击
raceTimer.Enabled=True
端接头
私有子raceTimer_Tick(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理raceTimer.Tick
'将两辆车从表格上的起跑线移动到终点线
'显示一条指示比赛结果的消息
'计算并显示用户选择
'结果列表框中的正确比赛结果,以及号码
'用户选择错误比赛结果的次数
Dim随机发生器作为一种新的随机发生器
Dim whitenewlocation作为整数
Dim rednewlocation作为整数
结果显示为字符串
Dim userchoice作为字符串
Dim finishline作为整数=finishTextBox.Left+1
静态数字更正为整数
静态数字正确为整数
'保存用户的列表框选择,然后禁用列表框
userchoice=resultListBox.SelectedItem.ToString
resultListBox.Enabled=False
'计算每个图片框右边框的新位置
“不要让右边界超出终点线
whitenewlocation=whitePictureBox.Right+randomgenerator.Next(0,11)
如果whitenewlocation>finishline,则
whitenewlocation=finishline
如果结束
rednewlocation=redPictureBox.Right+randomgenerator.Next(0,11)
如果rednewlocation>finishline,则
rednewlocation=finishline
如果结束
'将每个图片框移向终点线
whitePictureBox.SetBounds(whitenewlocation-whitePictureBox.Width,0,0,BoundsSpecified.X)
redPictureBox.SetBounds(rednewlocation-redPictureBox.Width,0,0,BoundsSpecified.X)
'仅当至少
其中一个图片框在终点线
如果whitePictureBox.Right=finishline_
OrElse redPictureBox.Right=完成行
'禁用计时器
raceTimer.Enabled=False
如果结束
'发出嘟嘟声表示比赛结束
对于x作为整数=1到5
控制台。嘟嘟声(100100)
下一个x
'将比赛结果存储在变量中
如果whitenewlocation=rednewlocation,则
racesult=“领带”
ElseIf whitenewlocation>rednewlocation然后
raceresult=“白色汽车获胜”
其他的
raceresult=“红色赛车获胜”
如果结束
'显示比赛结果
MessageBox.Show(“比赛结束!”&ControlChars.NewLine&racesult_
“赛车”,MessageBoxButtons.OK,MessageBoxIcon.Information)
'将图片框移回起始线
whitePictureBox.SetBounds(12,0,0,0,BoundsSpecified.X)
redPictureBox.SetBounds(12,0,0,0,BoundsSpecified.X)
'如果用户不想猜测比赛结果,则
'不更新或显示计数器值;否则,,
'将比赛结果与用户的选择进行比较并更新
'选择适当的计数器,然后显示两个计数器值
如果用户选择“没有猜测”,那么
如果racesult=userchoice,则
numbercorrect=numbercorrect+1
其他的
numberincorrect=numberincorrect+1
如果结束
correctLabel.Text=Convert.ToString(numbercorrect)
incorrectLabel.Text=Convert.ToString(numberincorrect)
如果结束
'启用列表框
resultListBox.Enabled=True
端接头
末级

您的计时器在每一个滴答声中都在执行所有操作。处理比赛结束的代码将在计时器的每个滴答声中处理。它应该包含在处理比赛结束的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
Private Sub raceTimer_Tick(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理raceTimer.Tick
'将两辆车从表格上的起跑线移动到终点线
'显示一条指示比赛结果的消息
'计算并显示用户选择
'结果列表框中的正确比赛结果,以及号码
'用户选择错误比赛结果的次数
Dim随机发生器作为一种新的随机发生器
Dim whitenewlocation作为整数
Dim rednewlocation作为整数
结果显示为字符串
Dim userchoice作为字符串
暗淡的终点线A