一、界面设计说明
界面设计和时钟控件说明

时钟控件:
控制植物动画:timer2,时间间隔100,默认启用
控制子弹动画:timer4,时间间隔10,默认关闭
监控子弹与僵尸碰撞:timer5,时间间隔1,默认启用
控制僵尸前进:timer3,时间间隔100,默认启用
控制僵尸动画:timer1,时间间隔100,默认启用
背景音乐:
使用新的部件windows media player

音乐控件名称:WindowsMediaPlayer1
植物:image2,载入第一张植物图片

僵尸:image1,载入第一张僵尸图片

子弹:shape1,通过属性配置可以模拟子弹形状


总体工程生成的保存文件结构:

img文件夹中植物与僵尸的图片与命名

二、游戏思想
游戏人物:植物、僵尸
模拟动画效果:
1.植物动的状态
2.僵尸动的状态
3.子弹动画
通过观察:利用循环的思想实现动画效果,需要使用到时钟控件timer
这个时钟控件里面做什么事情?
举例僵尸:
我们通过不断循环图片来实现。
通过实现的代码:
i = i + 1
If i > 22 Then i = 1
Image1.Picture = LoadPicture(App.Path & "\img\c (" & i & ").jpg")
界面采用image控件和timer控件
在timer控件的的timer事件中实现图片的切换。
图片的加载使用Loadpicture来实现
loadpicture函数用法:
loadpicture(图片路径)
图片路径可以用字符串表示。
图片的切换使用变量的递增来实现。
因为图片的个数是有限的,所以一定要注意在切换完图片最后一定要重置到第一张图片,使得图片能不断循环。
同样的,僵尸和植物都是可以采用以上方法来实现的。
注意:代码中的App.Path代表项目工程文件vbp文件所在的目录。
鼠标在工具栏右键---部件---选择“windows media player”
把调出的音乐控件拖入界面上面
在form_load事件中设置控件的url属性代表音乐路径
i = 1
j = 1
WindowsMediaPlayer1.URL = App.Path & "/" & "a.mp3"使用shape控件来设计子弹,fillestyle填充为solid绿色
通过timer控件来控制子弹的运动
在timer控件中设置shape1.left=shape1.left+100
设置子弹默认情况为visible属性为false代表隐藏
时钟控件enabled属性默认为false
当点击空格键后,打开时钟控件,让子弹运动。
通过使用function函数实现一个通用的碰撞模型。
Function PZ(A As Shape, B As Image)
Dim r As Boolean
r = False '默认为不碰撞,如果碰撞,就置为true
'第一个条件检测左右碰撞情况,第二个条件检测上下碰撞情况
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
r = True
End If
PZ = r
End Function三、整个游戏所有源代码
Dim i As Integer
Dim j As Integer
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then
Shape1.Visible = True
Timer4.Enabled = True
End If
End Sub
Private Sub Form_Load()
i = 1
j = 1
WindowsMediaPlayer1.URL = App.Path & "/" & "a.mp3"
End Sub
Private Sub Timer1_Timer()
i = i + 1
If i > 22 Then i = 1
Image1.Picture = LoadPicture(App.Path & "\img\c (" & i & ").jpg")
End Sub
Private Sub Timer2_Timer()
j = j + 1
If j > 7 Then j = 1
Image2.Picture = LoadPicture(App.Path & "\img\b (" & j & ").jpg")
End Sub
Private Sub Timer3_Timer()
If Image1.Left < Image2.Left + Image2.Width Then
Image1.Left = Image2.Left + Image2.Width
Timer3.Enabled = False
Timer1.Interval = 1
Else
Image1.Left = Image1.Left - 30
End If
End Sub
Private Sub Timer4_Timer()
Shape1.Left = Shape1.Left + 100
End Sub
Function PZ(A As Shape, B As Image)
Dim r As Boolean
r = 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
r = True
End If
PZ = r
End Function
Private Sub Timer5_Timer()
If PZ(Shape1, Image1) Then
Timer4.Enabled = False
Shape1.Left = 1200
Shape1.Visible = False
End If
End Sub