快捷搜索:  汽车  科技

VB初学编程100个代码(做个小游戏-膨胀的星球)

VB初学编程100个代码(做个小游戏-膨胀的星球)If....ThenF = False2.自定义函数:Function crash(A As Shape B As Shape) As BooleanDim F As Boolean

今天是我学习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.如图新建三个控件

VB初学编程100个代码(做个小游戏-膨胀的星球)(1)

2.输入代码:

VB初学编程100个代码(做个小游戏-膨胀的星球)(2)

VB初学编程100个代码(做个小游戏-膨胀的星球)(3)

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.生成程序运行

猜您喜欢: