'字母和数字随机产生,可以计算得分与控制时间:
Option Explicit
Dim score As Integer
Dim speed As Integer
Public Sub init()
Label5.Caption = Chr(Int(Rnd * 26) + 49) '设置label5随机显示字母
speed = Int(Rnd * 100 + 100) '设置label5随机显示字母的速度
Label5.Left = Int(Rnd * Frame1.Width)
Label5.Top = Frame1.Top '设置label5代表字母出现的顶部位置。
End Sub
Public Sub init1()
Label6.Caption = Chr(Int(Rnd * 26) + 97) '设置label6随机显示字母
speed = Int(Rnd * 100 + 100) '设置label6随机显示字母的速度
Label6.Left = Int(Rnd * Frame1.Width)
Label6.Top = Frame1.Top '设置label6代表字母出现的顶部位置。
End Sub
Private Sub Command1_Click()
init
init1
Timer1.Enabled = True
Timer2.Enabled = True
Command1.Visible = False
Label4.Caption = 5
Label3.Caption = 0
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) = Label5.Caption Then '校验键盘输入字符和label5显示的字符
init
score = score + 1
Label3.Caption = score
End If
If Chr(KeyAscii) = Label6.Caption Then '校验键盘输入字符和label6显示的字符
init1
score = score + 1
Label3.Caption = score
End If
End Sub
Private Sub Form_Load()
Randomize
Timer1.Enabled = False
Timer2.Enabled = False
End Sub
private Sub Timer1_Timer()
Label5.Top = Label5.Top + speed
If Label5.Top > Frame1.Height Then
'当第一个字母超出屏幕范围的时候调用init子程序重新出现一个字母。
init
End If
Label6.Top = Label6.Top + speed
If Label6.Top > Frame1.Height Then
'当第二个字母超出屏幕范围的时候调用init1子程序重新出现一个字母。
init1
End If
End Sub
Private Sub Timer2_Timer()
Label4.Caption = Val(Label4.Caption) - 1 '扣除剩余个数中的一个。
If Val(Label4.Caption) <= 0 Then
Timer1.Enabled = False
Label5.Caption = ""
Label6.Caption = ""
Select Case score
Case Is <= 80
MsgBox vbCrLf + "别放弃,再来一次!"
Case Is <= 120
MsgBox vbCrLf + "成绩不错,加油!"
Case Is <= 150
MsgBox vbCrLf + "再努力做得更好!"
Case Is > 180
MsgBox vbCrLf + "好厉害,最高分呀!"
End Select
Command1.Visible = True
Label3.Caption = 0
Label4.Caption = 200
Timer1.Enabled = False
Timer2.Enabled = False
End If
End Sub
来源:怖客技术吧 www.bkhack.com,转载请注明来处。
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。