视频加载中...
今天是我学习VB编程的第24天,今天学习了刘金玉老师的零基础VB教程第31期,内容是碰撞模型。
补充知识点:
1.RGB函数:颜色函数,表达为RGB(参数1,参数2,参数3),三个参数都在0-255内取值。
2.自定义函数:
Function crash(A As Shape, B As Shape) As Boolean
Dim F As Boolean
F = False
If....Then
F = True
End If
End Function
案例:做一个碰撞小游戏
1.如图新建三个控件
2.输入代码:
Dim currentdirection As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then
currentdirection = 3
ElseIf KeyCode = vbKeyLeft Then
currentdirection = 2
ElseIf KeyCode = vbKeyDown Then
currentdirection = 1
ElseIf KeyCode = vbKeyRight Then
currentdirection = 0
End If
End Sub
Function crash(A As Shape, B As Shape) As Boolean
Dim F As Boolean
F = False
If A.Left + A.Width >= B.Left And A.Left <= B.Left + B.Width And A.Top + A.Height >= B.Top And A.Top <= B.Top + B.Height Then
F = True
End If
crash = F
End Function
Private Sub Timer1_Timer()
Dim speed As Integer
speed = 100
If currentdirection = 0 Then
Shapeball.Left = Shapeball.Left + speed
ElseIf currentdirection = 1 Then
Shapeball.Top = Shapeball.Top + speed
ElseIf currentdirection = 2 Then
Shapeball.Left = Shapeball.Left - speed
ElseIf currentdirection = 3 Then
Shapeball.Top = Shapeball.Top - speed
End If
If crash(Shapeball, Shape1) Then
Shapeball.FillColor = Shape1.FillColor
Shapeball.Width = Shapeball.Width + 200
Shapeball.Height = Shapeball.Height + 200
Randomize
Shape1.FillColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)
Shape1.Left = Rnd * Form1.Width - 20
Shape1.Top = Rnd * Form1.Height - 20
Shape1.Shape = Int(Rnd * 5) + 1
End If
End Sub
3.生成程序运行
页面更新:2024-05-28
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号