'窗体添加label1控件,将index属性设置为0,使成为控件数组,再添加timer1控件
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim m As Integer
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim j As Integer
If KeyAscii = 13 Then
Timer1.Enabled = True
ElseIf KeyAscii = 27 Then 'ESC暂停
Timer1.Enabled = False
ElseIf Timer1.Enabled = True Then
For j = 0 To 9
If Label1(j).Caption = UCase(Chr(KeyAscii)) Then
m = m + 1
If m = 30 Then Me.Caption = "第1级"
If m = 60 Then Me.Caption = "第2级"
If m = 90 Then Me.Caption = "第3级"
Label1(j).Top = 0
Exit For
End If
Next
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 9
Load Label1(i)
Label1(i).Visible = True
Next
For i = 0 To 9
Label1(i).Top = 0
Next
Timer1.Enabled = False
Timer1.Interval = 100
Label1(0).Caption = "按回车开始游戏"
Me.Caption = "第0级"
End Sub
Private Sub Timer1_Timer()
Static n As Integer
Dim i As Integer
If n = 10 Then
MsgBox "游戏结束"
Timer1.Enabled = False
End If
For i = 0 To 9
If Label1(i).Top > Form1.Height Then
Label1(i).Top = 0
Randomize
Label1(i).Caption = Chr(Int(Rnd * 26 + 65))
n = n + 1
ElseIf Label1(i).Top = 0 Then
Randomize
Label1(i).Caption = Chr(Int(Rnd * 26 + 65))
Label1(i).Top = Label1(i).Top + 100
Else
Label1(i).Top = Label1(i).Top + 100
End If
Next
End Sub