我在Visual Basic 2010中有一个模拟老虎机的程序。
首先,我从1到9生成3个随机数,因为我想模拟“旋转”,我决定经历一个循环,其中老虎机的图像水果和事物出现在屏幕上。在此循环结束后,用户应获得生成的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或其他东西
答案 0 :(得分: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循环会耗尽CPU内部没有Sleep - pinkfloydx33
答案 1 :(得分: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