WEB开发网
开发学院WEB开发综合 基于VB6.0射击游戏的实现 阅读

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

 2006-02-27 11:39:56 来源:WEB开发网   
核心提示:基于VB6.0射击游戏的实现随着计算机技术的进步,计算机游戏也越来越普及,基于VB6.0射击游戏的实现,很多喜爱程序开发的读者朋友都向往游戏编程,但往往又觉得游戏编程序很复杂,读者朋友可以根据文章中的程序代码自己动手实验一下,本程序在Windows2000、VisualBasic6.0环境下编译通过,高深莫测,诚然

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

随着计算机技术的进步,计算机游戏也越来越普及,很多喜爱程序开发的读者朋友都向往游戏编程,但往往又觉得游戏编程序很复杂,高深莫测。诚然,象"帝国时代"、"反恐精英"这样的大型游戏需要写很复杂的程序来实现一些令人"头昏"的算法,需要熟悉DirectX开发知识。但是,对于一般的开发人员,完全可以利用所学的知识开发一些小的游戏,达到自娱自乐的目的。


  本文介绍如何在VisualBasic6.0环境下开发射击小游戏,通过实现该小游戏可以帮助一些VisualBasic初学者加深对VisualBasic编程知识的理解,同时它也可以开拓初、中级开发爱好者的编程思路。该射击小游戏程序编译运行后的界面效果如图一所示:


图一、射击游戏界面图

在射击游戏中,安排了两个角色,相互之间可以开枪进行对射,同时为了丰富游戏的功能,游戏的场景中添加定时移动的仙人掌,为射击的双方角色提供保护功能。当其中一个角色中弹后,游戏终止,同时游戏人物角色的图标变更,表示角色死亡。为了实现上述的游戏,最初要作的是设计程序界面,按照游戏的需求,首先生成一个VB应用程序,在Form1上添加一个开始按钮btnStart,一个名为picDesert的Picture控件,该控件用来做为游戏的场景和其它控件的容器,在该控件上添加六个Image控件,分别用来显示游戏的角色、两个移动的仙人掌、分别向右、向左呼啸射击的子弹以及标志角色死亡的图标,它们的图象分别如下:


游戏角色

仙人掌

呼啸的子弹

角色击中标志

为了使程序中的仙人掌、游戏角色和射击时发射的子弹可以移动,需要向项目中添加定时器tmrMouseCnt和Timer1,在这两个定时响应函数中完成不同对象的移动功能。在游戏运行后,为了使用户可以通过键盘和鼠标来操作游戏的角色,实现射击的功能,需要添加鼠标消息和键盘消息处理函数。例如,对于角色1来说,可以通过上下键来移动,空格键来射击,对于角色2来说,鼠标左右键控制移动,双击实现射击。在射击过程中,要处理两个细节,一个细节是子弹与仙人掌及角色的区域重叠问题,当子弹与仙人掌重叠时让子弹隐藏起来,与角色重叠时表示击中目标,游戏结束。这里需要判断何时两个区域有重叠,解决这个问题的方法是使用API函数IntersectRect,用它来判断两个区域是否有重叠。另一个细节是子弹射击过程中需要添加"呼啸"的声音和击中目标时添加人物惨叫的声音,来达到逼真的效果,为了实现这个功能,需要向程序中添加语音文件(程序中的语音文件分别为:BANG.WAV和OH!!.WAV),然后通过API函数sndPlaySound来实现。另外,在对象移动的过程中,需要注意移动到边缘位置的情况处理。
程序的具体实现代码如下:
SHOOTOUT.BAS
OptionExplicit
'DatatyperequiredbytheIntersectRectfunction
TypetRect
LeftAsLong
TopAsLong
RightAsLong
BottomAsLong
EndType
'WindowsAPIrectanglefunctions
DeclareFunctionIntersectRectLib"user32"(lpDestRectAstRect,lpSrc1RectAstRect,lpSrc2RectAstRect)AsLong
'Functionsandconstantsusedtoplaysounds.
DeclareFunctionsndPlaySoundLib"winmm.dll"Alias"sndPlaySoundA"(ByVallpszSoundNameAsString,ByValuFlagsAsLong)AsLong
'ConstantusedwithsndPlaySoundfunction
GlobalConstSND_ASYNC=&H1
'----------------------------------------------------------
'SHOOTOUT.FRM
OptionExplicit
'KeyCodesforkeyboardaction.
ConstKEY_SPACE=&H20
ConstKEY_UP=&H26
ConstKEY_DOWN=&H28
'NumberofTwipstomoveplayeroneachkeyormouseevent.
ConstPlayerIncrement=45
'Constantsformouseaction.
ConstNO_BUTTON=0
ConstLBUTTON=1
ConstRBUTTON=2
'BooleanthatindicatesifmousebuttonhasbeenPResseddown.
DimMouseButtonDownAsInteger
'Numberofbulletseitherplayercanhaveinuseatonetime.
ConstNUM_BULLETS=6
'Booleansindicatingifplayer0orplayer1havejustfired.
DimGunFired(0To1)AsInteger

'Startthegamebyenablingthemaintimerandhidingthestartbutton.
PrivateSubbtnStart_Click()
Timer1.Enabled=True
btnStart.Visible=False
EndSub

'CheckifthetwoImagesintersect,usingtheIntersectRectAPIcall.
PrivateFunctionCollided(imgAAsImage,imgBAsImage)AsInteger
DimAAstRect
DimBAstRect
DimResultRectAstRect
'CopyinformationintotRectstructure
A.Left=imgA.Left
A.Top=imgA.Top
B.Left=imgB.Left
B.Top=imgB.Top
'CalculatetherightandbottomsofrectanglesneededbytheAPIcall.
A.Right=A.Left imgA.Width-1
A.Bottom=A.Top imgA.Height-1
B.Right=B.Left imgB.Width-1
B.Bottom=B.Top imgB.Height-1
'IntersectRectwillonlyreturn0(false)ifthe
'tworectanglesdoNOTintersect.
Collided=IntersectRect(ResultRect,A,B)
EndFunction

'Double-clickingthemousefiresPlayer1'sgun.
PrivateSubForm_DblClick()
DimrcAsInteger
IfNotTimer1.EnabledThenExitSub
GunFired(1)=True
rc=sndPlaySound(App.Path&"\BANG.WAV",SND_ASYNC)
EndSub

'ThiseventhandlesPlayer0'sgameactionviathekeyboard.
PrivateSubForm_KeyDown(KeyCodeAsInteger,ShiftAsInteger)
DimrcAsInteger
StaticInKeyDownAsInteger
IfNotTimer1.EnabledThenExitSub
IfInKeyDownThenExitSub
InKeyDown=True
DoEvents
SelectCaseKeyCode
CaseKEY_UP
imgPlayer(0).Top=imgPlayer(0).Top-PlayerIncrement
IfimgPlayer(0).Top<0ThenimgPlayer(0).Top=0
CaseKEY_SPACE
GunFired(0)=True
rc=sndPlaySound(App.Path&"\BANG.WAV",SND_ASYNC)
CaseKEY_DOWN
imgPlayer(0).Top=imgPlayer(0).Top PlayerIncrement
IfimgPlayer(0).Top>(picDesert.ScaleHeight-
imgPlayer(0).Height)Then
imgPlayer(0).Top=picDesert.ScaleHeight-
imgPlayer(0).Height
EndIf
EndSelect
InKeyDown=False
EndSub

PrivateSubForm_Load()
DimiAsInteger
Timer1.Interval=22
Timer1.Enabled=False
MouseButtonDown=NO_BUTTON
Fori=1ToNUM_BULLETS-1
LoadimgLBullet(i)
LoadimgRBullet(i)
Next
EndSub

PrivateSubForm_MouseDown(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
MouseButtonDown=Button
EndSub

PrivateSubForm_MouseUp(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
MouseButtonDown=NO_BUTTON
EndSub

'Themaingametimer.
PrivateSubTimer1_Timer()
ConstCactusIncrement=30
ConstBulletIncrement=300
ConstNumCacti=2

DimiAsInteger
DimrcAsInteger
'Movetherovingcacti.
Fori=0ToNumCacti-1
imgCactus(i).Top=imgCactus(i).Top-CactusIncrement
IfimgCactus(i).Top<-imgCactus(i).HeightThen
imgCactus(i).Top=picDesert.Height
EndIf
Next
'Didplayer0fireabullet?
IfGunFired(0)Then
GunFired(0)=False
'Findaspare(invisible)bullet.
Fori=0ToNUM_BULLETS-1
IfNotimgLBullet(i).VisibleThen
imgLBullet(i).Top=imgPlayer(0).Top
imgLBullet(i).Left=imgPlayer(0).Left
(imgPlayer(0).Width/2)
imgLBullet(i).Visible=True
ExitFor
EndIf
Next
EndIf
'Didplayer1fireabullet?
IfGunFired(1)Then
GunFired(1)=False
'Findaspare(invisible)bullet.
Fori=0ToNUM_BULLETS-1
IfNotimgRBullet(i).VisibleThen
imgRBullet(i).Top=imgPlayer(1).Top
imgRBullet(i).Left=imgPlayer(1).Left-
(imgPlayer(1).Width/2)
imgRBullet(i).Visible=True
ExitFor
EndIf
Next
EndIf
'MoveVisibleBullets
Fori=0ToNUM_BULLETS-1
'Moveplayer0'sbullets.
IfimgLBullet(i).VisibleThen
imgLBullet(i).Left=imgLBullet(i).Left BulletIncrement
IfCollided(imgLBullet(i),imgCactus(0))Then
imgLBullet(i).Visible=False
ElseIfCollided(imgLBullet(i),imgCactus(1))Then
imgLBullet(i).Visible=False
ElseIfimgLBullet(i).Left>picDesert.ScaleWidthThen
imgLBullet(i).Visible=False
ElseIfCollided(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)
EndIf
EndIf
'Moveplayer1'sbullets.
IfimgRBullet(i).VisibleThen
imgRBullet(i).Left=imgRBullet(i).Left-BulletIncrement
IfCollided(imgRBullet(i),imgCactus(0))Then
imgRBullet(i).Visible=False
ElseIfCollided(imgRBullet(i),imgCactus(1))Then
imgRBullet(i).Visible=False
ElseIfimgRBullet(i).Left<-imgRBullet(i).WidthThen
imgRBullet(i).Visible=False
ElseIfCollided(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)
EndIf
EndIf
Next
EndSub

'HandlePlayer1'smovement(upanddown).
PrivateSubtmrMouseCntl_Timer()
IfNotTimer1.EnabledThenExitSub
SelectCaseMouseButtonDown
CaseRBUTTON
imgPlayer(1).Top=imgPlayer(1).Top-PlayerIncrement
IfimgPlayer(1).Top<0ThenimgPlayer(1).Top=0
CaseLBUTTON
imgPlayer(1).Top=imgPlayer(1).Top PlayerIncrement
IfimgPlayer(1).Top>(picDesert.ScaleHeight-
imgPlayer(1).Height)Then
imgPlayer(1).Top=picDesert.ScaleHeight-
imgPlayer(1).Height
EndIf
EndSelect
EndSub


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

->

Tags:基于 VB 射击游戏

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接