简介 INTRODUCTION时钟控件+控件数组的典型案例
课题9 时钟控件的灵活使用
授课:刘金玉
打字高手小游戏开发:
知识要点:
1.随机数知识
2.timer控件的使用
3.窗体启动加载事件form_load
4.键盘事件的使用
5.ascii码的转换
6.相关逻辑:if语句大小判断
7.随机颜色的使用
实现步骤:
1.载入几个控件,用来存储随机产生的字符
2.用timer控件来控制字符的下降
3.逻辑判断是否打中相应的字符,打中字符后重置随机字符的位置
4.记录得分分值
软件设计界面:
form源代码:
VERSION 5.00
Begin VB.Form Form1
Caption = "打字游戏--by ljy"
ClientHeight = 5955
ClientLeft = 120
ClientTop = 450
ClientWidth = 9135
LinkTopic = "Form1"
ScaleHeight = 5955
ScaleWidth = 9135
StartUpPosition = 3 '窗口缺省
Begin VB.Timer Timer1
Interval = 500
Left = 960
Top = 4320
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "y"
BeginProperty Font
Name = "微软雅黑"
Size = 18
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 465
Index = 2
Left = 4920
TabIndex = 2
Top = 600
Width = 210
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "y"
BeginProperty Font
Name = "微软雅黑"
Size = 18
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 465
Index = 1
Left = 3000
TabIndex = 1
Top = 600
Width = 210
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "y"
BeginProperty Font
Name = "微软雅黑"
Size = 18
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 465
Index = 0
Left = 1560
TabIndex = 0
Top = 600
Width = 210
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim a(0 To 2) As Long
Dim s(0 To 2) As Long
Private Sub Command1_Click()
Form1.SetFocus
If Command1.Caption = "开始游戏" Then
Timer1.Enabled = True
Command1.Caption = "停止游戏"
Else
Timer1.Enabled = False
Command1.Caption = "开始游戏"
End If
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim i As Integer
For i = 0 To 2 Step 1
If s(i) = KeyCode Then
a(i) = 0
Label1(i).Top = a(i)
Randomize
s(i) = Int(Rnd * 26) + 65
Label1(i).Caption = Chr(s(i))
End If
Next i
End Sub
Private Sub Form_Load()
Form1.Show
Dim i As Integer
For i = 0 To 2 Step 1
s(i) = Int(Rnd * 26) + 65
Label1(i) = Chr(s(i))
Next i
End Sub
Private Sub Timer1_Timer()
Randomize
'颜色
r = Rnd * 256
g = Rnd * 256
b = Rnd * 256
Dim i As Integer
For i = 0 To 2 Step 1
Label1(i).ForeColor = RGB(r, g, b)
a(i) = a(i) + Rnd * 500 + 1
Label1(i).Top = a(i)
'颜色
r = Rnd * 256
g = Rnd * 256
b = Rnd * 256
Next i
End Sub
联系作者:刘金玉
长按关注公众号
编程创造城市官网:https://bcczcs.com/