经测试,你的所谓速度搞多了是牺牲其他点不动的情况下实现的。理论上,作为单线程的vb并不会因为你讲任务分配到多个timer就会提高并行能力,你可以通过在一个timer梨执行死循环来判断另一个timer是否正常计时得到答案。你那个出现反复不退出的情况是由于控件数组任务现场还原造成的,控件数组执行任务时会保存原状态,你unload后,会返回到上一个控件数组的执行状态,这时你取消的定时器又处于enable=true的状态了,由于定时器有效,所以又会执行控件显示操作,重新执行form_load。将form2里的代码修改如下(红色部分为修改部分):
Dim Movement As Integer, JValue As Integer, TimerIndex As Integer
Dim Speed As Integer, MouseX As Integer, MouseY As Integer
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Dim Time As Integer
Private Sub Form_Load()
Me.Height = Screen.Height: Me.Width = Screen.Width
If Difficulty = 1 Then
Speed = 100: Me.Caption = Me.Caption & "简单"
ElseIf Difficulty = 2 Then
Speed = 300: Me.Caption = Me.Caption & "普通"
ElseIf Difficulty = 3 Then
Speed = 600: Me.Caption = Me.Caption & "困难"
ElseIf Difficulty = 4 Then
Speed = 1000: Me.Caption = Me.Caption & "爆炸难度"
End If
Timer1(0).Interval = 10
MouseX = Screen.Width / 2
MouseY = Screen.Height
Dim i As Integer
For i = 1 To Speed Step 1 '循环加载部件
sss:
On Error Resume Next
Load Shape(i): Shape(i).Left = 40000 * Rnd: Shape(i).Top = 20000 * Rnd: Shape(i).FillColor = Shape(0).FillColor: Shape(i).BorderColor = Shape(0).BorderColor: Shape(i).Height = 135: Shape(i).Width = 135: Shape(i).Visible = True: Shape(i).Shape = 3
If Shape(i).Left < MouseX + 1000 And Shape(i).Left > MouseX - 1000 Then GoTo sss
If Shape(i).Top < MouseY + 1000 And Shape(i).Top > MouseY - 1000 Then GoTo sss '防止生成在鼠标附近
Next i
Me.WindowState = 2
SetCursorPos ScaleX((Screen.Width / 2), 1, 3), ScaleY((Screen.Height / 2), 1, 3) '移动鼠标位置
因为SetCursorPos使用的坐标是以像素为单位,而Screen的Width和Height属性是以缇为单位,所以必须将Screen的Width和Height属性值转换为像素才行。代码中ScaleX和ScaleY就是完成缇到像素的单位转换的。
JValue = Speed
Time = -1
TimerIndex = 1
For e = 1 To Speed / 50 - 1 '循环加载Timer
Load Timer1(e): Timer1(e).Enabled = True: Timer1(e).Interval = 10 '加载Timer
TimerIndex = TimerIndex + 1
Next e
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
MouseX = x
MouseY = y
Shape1.Left = x - 100
Shape1.Top = y - 100
End Sub
Private Sub Timer1_Timer(Index As Integer)
Dim JValue2 As Integer
, b As Timer
If (Index + 1) * 50 > JValue Then
JValue2 = JValue
Else
JValue2 = (Index + 1) * 50
End If
For i = Index * 50 To JValue2
If Abs(Shape1.Top - Shape(i).Top) < 100 And Abs(Shape1.Left - Shape(i).Left) < 100 Then
Debug.Print Shape(i).Top
Debug.Print Shape(i).Left
'这里删除部分
Open App.Path & "\高分榜\" & Replace(Replace(Replace(Replace(Speed, "1000", "VeryDifficult.sav"), "300", "Middle.sav"), "600", "Difficult.sav"), "100", "Easy.sav") For Input As #1
Input #1, a
Close #1
If a > Time Then
MsgBox "你死了!游戏结束!未破纪录。分数:" & CStr(Time)
'这里删除部分
Else
Open App.Path & "\高分榜\" & Replace(Replace(Replace(Replace(Speed, "1000", "VeryDifficult.sav"), "300", "Middle.sav"), "600", "Difficult.sav"), "100", "Easy.sav") For Output As #1 '输出分数,REPLACE函数负责模式检测
Print #1, Time
Close #1
MsgBox "你死了!游戏结束!你的分数是最高纪录!分数:" & CStr(Time) '这里删除部分
End If
Unload Me
For Each b In Timer1
b.Enabled = False
Next
Timer2.Enabled = False
Timer3.Enabled = False
Form1.Show
Exit Sub
End If
Movement = Abs(Shape1.Left - Shape(i).Left) / Abs(Shape1.Top - Shape(i).Top) '计算移动比例
If Shape1.Left > Shape(i).Left Then
Shape(i).Left = Shape(i).Left + 15
ElseIf Shape1.Left < Shape(i).Left Then
Shape(i).Left = Shape(i).Left - 15
End If
On Error Resume Next
If Movement = 0 Then Movement = 1
If Shape1.Top > Shape(i).Top Then
Shape(i).Top = Shape(i).Top + 15 / Movement
ElseIf Shape1.Top < Shape(i).Top Then
Shape(i).Top = Shape(i).Top - 15 / Movement
End If
DoEvents
Next i
End Sub
Private Sub Timer2_Timer()
For i = JValue To JValue + 30
sss:
On Error Resume Next
Load Shape(i): Shape(i).Left = 40000 * Rnd: Shape(i).Top = 20000 * Rnd: Shape(i).FillColor = Shape(0).FillColor: Shape(i).BorderColor = Shape(0).BorderColor: Shape(i).Height = 135: Shape(i).Width = 135: Shape(i).Visible = True: Shape(i).Shape = 3
If Shape(i).Left < MouseX + 1000 And Shape(i).Left > MouseX - 1000 Then GoTo sss
If Shape(i).Top < MouseY + 1000 And Shape(i).Top > MouseY - 1000 Then GoTo sss '防止生成在鼠标附近
Next i
JValue = JValue + 30
If JValue > TimerIndex * 50 Then Load Timer1(TimerIndex): Timer1(TimerIndex).Enabled = True: Timer1(TimerIndex).Interval = 10 '加载Timer
TimerIndex = TimerIndex + 1
End Sub
Private Sub Timer3_Timer()
Time = Time + 1
Form2.Caption = Replace(Replace(Replace(Replace(("反应速度测试 难度:" & Speed), "1000", "爆炸难度"), "300", "普通"), "600", "困难"), "100", "简单") & " 分数" & CStr(Time)
End Sub