手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>VB>列表

基于VB6.0射击游戏的实现

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

GunFired(1) = True
rc = sndPlaySound(App.Path & "\BANG.WAV", SND_ASYNC)
End Sub

' This event handles Player 0's game action via the keyboard.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim rc As Integer
Static InKeyDown As Integer
If Not Timer1.Enabled Then Exit Sub
If InKeyDown Then Exit Sub
InKeyDown = True
DoEvents
Select Case KeyCode
Case KEY_UP
imgPlayer(0).Top = imgPlayer(0).Top - PlayerIncrement
If imgPlayer(0).Top < 0 Then imgPlayer(0).Top = 0
Case KEY_SPACE
GunFired(0) = True
rc = sndPlaySound(App.Path & "\BANG.WAV", SND_ASYNC)
Case KEY_DOWN
imgPlayer(0).Top = imgPlayer(0).Top PlayerIncrement
If imgPlayer(0).Top > (picDesert.ScaleHeight -
imgPlayer(0).Height) Then
imgPlayer(0).Top = picDesert.ScaleHeight -
imgPlayer(0).Height
End If
End Select
InKeyDown = False
End Sub

Private Sub Form_Load()
Dim i As Integer
Timer1.Interval = 22
Timer1.Enabled = False
MouseButtonDown = NO_BUTTON
For i = 1 To NUM_BULLETS - 1
Load imgLBullet(i)
Load imgRBullet(i)
Next
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
MouseButtonDown = Button
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MouseButtonDown = NO_BUTTON
End Sub

' The main game timer.
Private Sub Timer1_Timer()
Const CactusIncrement = 30
Const BulletIncrement = 300
Const NumCacti = 2

Dim i As Integer
Dim rc As Integer
' Move the roving cacti.
For i = 0 To NumCacti - 1
imgCactus(i).Top = imgCactus(i).Top - CactusIncrement
If imgCactus(i).Top < -imgCactus(i).Height Then
imgCactus(i).Top = picDesert.Height
End If
Next
' Did player 0 fire a bullet?
If GunFired(0) Then
GunFired(0) = False
' Find a spare (invisible) bullet.
For i = 0 To NUM_BULLETS - 1
If Not imgLBullet(i).Visible Then
imgLBullet(i).Top = imgPlayer(0).Top
imgLBullet(i).Left = imgPlayer(0).Left
(imgPlayer(0).Width / 2)
imgLBullet(i).Visible = True
Exit For
End If
Next
End If
' Did player 1 fire a bullet?
If GunFired(1) Then
GunFired(1) = False
' Find a spare (invisible) bullet.
For i = 0 To NUM_BULLETS - 1
If Not imgRBullet(i).Visible Then
imgRBullet(i).Top = imgPlayer(1).Top
imgRBullet(i).Left = imgPlayer(1).Left -
(imgPlayer(1).Width / 2)
imgRBullet(i).Visible = True
Exit For
End If
Next
End If
' Move Visible Bullets
For i = 0 To NUM_BULLETS - 1
' Move player 0's bullets.
If imgLBullet(i).Visible Then
imgLBullet(i).Left = imgLBullet(i).Left BulletIncrement
If Collided(imgLBullet(i), imgCactus(0)) Then
imgLBullet(i).Visible = False
ElseIf Collided(imgLBullet(i), imgCactus(1)) Then
imgLBullet(i).Visible = False
ElseIf imgLBullet(i).Left > picDesert.ScaleWidth Then
imgLBullet(i).Visible = False
ElseIf Collided(imgLBullet(i), imgPlayer(1)) Then
imgLBullet(i).Visible = False
imgPlayer(1).Picture = imgRIP.Picture
Timer1.Enabled = False
rc = sndPlaySound(App.Path & "\OH!!.WAV", SND_ASYNC)
End If
End If
' Move player 1's bullets.
If imgRBullet(i).Visible Then
imgRBullet(i).Left = imgRBullet(i).Left - BulletIncrement
If Collided(imgRBullet(i), imgCactus(0)) Then
imgRBullet(i).Visible = False
ElseIf Collided(imgRBullet(i), imgCactus(1)) Then
imgRBullet(i).Visible = False
ElseIf imgRBullet(i).Left < -imgRBullet(i).Width Then
imgRBullet(i).Visible = False
ElseIf Collided(imgRBullet(i), imgPlayer(0)) Then
imgRBullet(i).Visible = False
imgPlayer(0).Picture = imgRIP.Picture
Timer1.Enabled = False
rc = sndPlaySound(App.Path & "\OH!!.WAV", SND_ASYNC)
End If
End If
Next
End Sub

' Handle Player 1's movement (up and down).
Private Sub tmrMouseCntl_Timer()
If Not Timer1.Enabled Then Exit Sub
Select Case MouseButtonDown
Case RBUTTON
imgPlayer(1).Top = imgPlayer(1).Top - PlayerIncrement
If imgPlayer(1).Top < 0 Then imgPlayer(1).Top = 0
Case LBUTTON
imgPlayer(1).Top = imgPlayer(1).Top PlayerIncrement
If imgPlayer(1).Top > (picDesert.ScaleHeight -
imgPlayer(1).Height) Then
imgPlayer(1).Top = picDesert.ScaleHeight -
imgPlayer(1).Height
End If
End Select
End Sub


   文章的上述内容对射击游戏中的各个实现功能进行了详细的介绍,读者朋友可以根据文章中的程序代码自己动手实验一下。本程序在Windows2000、Visual Basic6.0环境下编译通过,运行正常。

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!