0

我在 Visual Basic 2010 中有一个模拟老虎机的程序。

首先,我生成 3 个从 1 到 9 的随机数,当我想模拟“旋转”时,我决定通过一个循环,其中老虎机水果和事物的图像出现在屏幕上。在这个循环结束后,用户应该得到生成的 1 到 9 个数字的对应图片。

所以我看到最好的想法是设置一个计时器,例如设置一个 100 的间隔并启动它,并在每个滴答声中在屏幕上显示一些随机图像。

但是,当我启动计时器时,它似乎与调用它的主函数平行。我不知道我是否在这里提供信息XD。最好自己找:

'CALCULATE WINNING RESULT
valor1 = GeneraAleatorio(1, 9)   -> This custom function returns a random number
valor2 = GeneraAleatorio(1, 9)
valor3 = GeneraAleatorio(1, 9)

Timer1.Enabled = True

'NOW I PUT THE WINNING PICTURES THAT CORRESPOND WITH THE NUMBERS
ColocaImagen(1, valor1)  -> Another custom made function, takes the position (1 to 3) and an image (1 to 9)
ColocaImagen(2, valor2)
ColocaImagen(3, valor3)

'END GAME
End()          -> or whatever

我的 timer_tick 函数是这样的:

    If tiempo >= 4000 Then
        Timer1.Enabled = False        ' -> To make it stop when it reaches 4000 (4 seconds)

    ElseIf tiempo <= 3900 Then

        ColocaImagen(1, GeneraAleatorio(1, 9))
        ColocaImagen(2, GeneraAleatorio(1, 9))
        ColocaImagen(3, GeneraAleatorio(1, 9))

        If tiempo >= ProgressBar.Minimum & tiempo <= ProgressBar.Maximum Then
            ProgressBar.Value = tiempo         
        End If

        tiempo = tiempo + 100                  'Tiempo is "time" in Spanish, it increases 100 every 100ms
    End If

似乎当我调用 timer1.enabled = true 时,它​​会以两种方式继续:程序进入刻度功能,但也不会等待计时器停止而走向游戏结束。我想要 4 秒过去,然后显示正确的图片,并执行我想要的任何操作,或显示 msgbox 或其他东西

4

2 回答 2

0

试试这个:

While (Timer1.Enabled) {} // 会一直阻塞直到定时器结束

'CALCULATE WINNING RESULT
valor1 = GeneraAleatorio(1, 9)   -> This custom function returns a random number
valor2 = GeneraAleatorio(1, 9)
valor3 = GeneraAleatorio(1, 9)

Timer1.Enabled = True

While (Timer1.Enabled) {} // Will keep blocking until the timer sets Enabled to false

'NOW I PUT THE WINNING PICTURES THAT CORRESPOND WITH THE NUMBERS
ColocaImagen(1, valor1)  -> Another custom made function, takes the position (1 to 3) and an image (1 to 9)
ColocaImagen(2, valor2)
ColocaImagen(3, valor3)

'END GAME
End()          -> or whatever

启动计时器不会导致程序停止并等待。

还:

while 循环会在没有 Sleep 的情况下占用 CPU – pinkfloydx33

于 2013-02-24T00:14:43.130 回答
0

只需创建一个 SubRoutine 并在您的 Timer 达到 4000 时调用它。看看这样的事情是否适合您。

Public Class Form1

    Dim valor1 As Integer
    Dim valor2 As Integer
    Dim valor3 As Integer


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If Timer1.Enabled Then Exit Sub
        'CALCULATE WINNING RESULT
        valor1 = GeneraAleatorio(1, 9)
        valor2 = GeneraAleatorio(1, 9)
        valor3 = GeneraAleatorio(1, 9)
        Timer1.Enabled = True

    End Sub

    Public Sub ShowFinalResult()
        'NOW I PUT THE WINNING PICTURES THAT CORRESPOND WITH THE NUMBERS
        ColocaImagen(1, valor1)
        ColocaImagen(2, valor2)
        ColocaImagen(3, valor3)
    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        Static tiempo As Integer
        Timer1.Enabled = False
        If tiempo >= 4000 Then
             tiempo = 0
             ShowFinalResult()

        ElseIf tiempo <= 3900 Then

            ColocaImagen(1, GeneraAleatorio(1, 9))
            ColocaImagen(2, GeneraAleatorio(1, 9))
            ColocaImagen(3, GeneraAleatorio(1, 9))

            tiempo = tiempo + 100 

            If tiempo >= ProgressBar1.Minimum And tiempo <= ProgressBar1.Maximum Then ProgressBar1.Value = tiempo

            Timer1.Enabled = True
        End If

    End Sub

End Class
于 2013-02-24T05:29:58.837 回答