📄 基于vb6_0射击游戏的实现.htm
字号:
<P> 随着计算机技术的进步,计算机游戏也越来越普及,很多喜爱程序开发的读者朋友都向往游戏编程,但往往又觉得游戏编程序很复杂,高深莫测。诚然,象"帝国时代"、"反恐精英"这样的大型游戏需要写很复杂的程序来实现一些令人"头昏"的算法,需要熟悉DirectX开发知识。但是,对于一般的开发人员,完全可以利用所学的知识开发一些小的游戏,达到自娱自乐的目的。 </P>
<P><BR> 本文介绍如何在Visual Basic6.0环境下开发射击小游戏,通过实现该小游戏可以帮助一些Visual Basic初学者加深对Visual Basic编程知识的理解,同时它也可以开拓初、中级开发爱好者的编程思路。该射击小游戏程序编译运行后的界面效果如图一所示: </P>
<P align=center><IMG src="/upload/20041221081125147.bmp"></P>
<P align=center><BR>图一、射击游戏界面图</P>
<P> 在射击游戏中,安排了两个角色,相互之间可以开枪进行对射,同时为了丰富游戏的功能,游戏的场景中添加定时移动的仙人掌,为射击的双方角色提供保护功能。当其中一个角色中弹后,游戏终止,同时游戏人物角色的图标变更,表示角色死亡。为了实现上述的游戏,最初要作的是设计程序界面,按照游戏的需求,首先生成一个VB应用程序,在Form1上添加一个开始按钮btnStart,一个名为picDesert的Picture控件,该控件用来做为游戏的场景和其它控件的容器,在该控件上添加六个Image控件,分别用来显示游戏的角色、两个移动的仙人掌、分别向右、向左呼啸射击的子弹以及标志角色死亡的图标,它们的图象分别如下:</P>
<P><IMG src="/upload/20041221081145379.bmp"></P>
<P><IMG src="/upload/20041221081210386.bmp"><BR>游戏角色</P>
<P><IMG src="/upload/20041221081222724.bmp"></P>
<P>仙人掌</P>
<P><IMG src="/upload/20041221081236241.bmp"></P>
<P>呼啸的子弹</P>
<P>角色击中标志</P>
<P> 为了使程序中的仙人掌、游戏角色和射击时发射的子弹可以移动,需要向项目中添加定时器tmrMouseCnt和Timer1,在这两个定时响应函数中完成不同对象的移动功能。在游戏运行后,为了使用户可以通过键盘和鼠标来操作游戏的角色,实现射击的功能,需要添加鼠标消息和键盘消息处理函数。例如,对于角色1来说,可以通过上下键来移动,空格键来射击,对于角色2来说,鼠标左右键控制移动,双击实现射击。在射击过程中,要处理两个细节,一个细节是子弹与仙人掌及角色的区域重叠问题,当子弹与仙人掌重叠时让子弹隐藏起来,与角色重叠时表示击中目标,游戏结束。这里需要判断何时两个区域有重叠,解决这个问题的方法是使用API函数IntersectRect,用它来判断两个区域是否有重叠。另一个细节是子弹射击过程中需要添加"呼啸"的声音和击中目标时添加人物惨叫的声音,来达到逼真的效果,为了实现这个功能,需要向程序中添加语音文件(程序中的语音文件分别为:BANG.WAV和OH!!.WAV),然后通过API函数sndPlaySound来实现。另外,在对象移动的过程中,需要注意移动到边缘位置的情况处理。<BR>程序的具体实现代码如下:<BR>SHOOTOUT.BAS<BR>Option Explicit<BR>' Data type required by the IntersectRect function<BR>Type tRect<BR>Left As Long<BR>Top As Long<BR>Right As Long<BR>Bottom As Long<BR>End Type<BR>' Windows API rectangle functions<BR>Declare Function IntersectRect Lib "user32" (lpDestRect As tRect, lpSrc1Rect As tRect, lpSrc2Rect As tRect) As Long<BR>' Functions and constants used to play sounds.<BR>Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long<BR>' Constant used with sndPlaySound function<BR>Global Const SND_ASYNC = &H1<BR>'----------------------------------------------------------<BR>' SHOOTOUT.FRM<BR>Option Explicit<BR>' KeyCodes for keyboard action.<BR>Const KEY_SPACE = &H20<BR>Const KEY_UP = &H26<BR>Const KEY_DOWN = &H28<BR>' Number of Twips to move player on each key or mouse event.<BR>Const PlayerIncrement = 45<BR>' Constants for mouse action.<BR>Const NO_BUTTON = 0<BR>Const LBUTTON = 1<BR>Const RBUTTON = 2<BR>' Boolean that indicates if mouse button has been pressed down.<BR>Dim MouseButtonDown As Integer<BR>' Number of bullets either player can have in use at one time.<BR>Const NUM_BULLETS = 6<BR>' Booleans indicating if player 0 or player 1 have just fired.<BR>Dim GunFired(0 To 1) As Integer</P>
<P>' Start the game by enabling the main timer and hiding the start button.<BR>Private Sub btnStart_Click()<BR>Timer1.Enabled = True<BR>btnStart.Visible = False<BR>End Sub</P>
<P>' Check if the two Images intersect, using the IntersectRect API call.<BR>Private Function Collided(imgA As Image, imgB As Image) As Integer<BR>Dim A As tRect<BR>Dim B As tRect<BR>Dim ResultRect As tRect<BR>' Copy information into tRect structure<BR>A.Left = imgA.Left<BR>A.Top = imgA.Top<BR>B.Left = imgB.Left<BR>B.Top = imgB.Top<BR>' Calculate the right and bottoms of rectangles needed by the API call.<BR>A.Right = A.Left + imgA.Width - 1<BR>A.Bottom = A.Top + imgA.Height - 1<BR>B.Right = B.Left + imgB.Width - 1<BR>B.Bottom = B.Top + imgB.Height - 1<BR>' IntersectRect will only return 0 (false) if the<BR>' two rectangles do NOT intersect.<BR>Collided = IntersectRect(ResultRect, A, B)<BR>End Function</P>
<P>' Double-clicking the mouse fires Player 1's gun.<BR>Private Sub Form_DblClick()<BR>Dim rc As Integer<BR>If Not Timer1.Enabled Then Exit Sub<BR>GunFired(1) = True<BR>rc = sndPlaySound(App.Path & "\BANG.WAV", SND_ASYNC)<BR>End Sub</P>
<P>' This event handles Player 0's game action via the keyboard.<BR>Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)<BR>Dim rc As Integer<BR>Static InKeyDown As Integer<BR>If Not Timer1.Enabled Then Exit Sub<BR>If InKeyDown Then Exit Sub<BR>InKeyDown = True<BR>DoEvents<BR>Select Case KeyCode<BR>Case KEY_UP<BR>imgPlayer(0).Top = imgPlayer(0).Top - PlayerIncrement<BR>If imgPlayer(0).Top < 0 Then imgPlayer(0).Top = 0<BR>Case KEY_SPACE<BR>GunFired(0) = True<BR>rc = sndPlaySound(App.Path & "\BANG.WAV", SND_ASYNC)<BR>Case KEY_DOWN<BR>imgPlayer(0).Top = imgPlayer(0).Top + PlayerIncrement<BR>If imgPlayer(0).Top > (picDesert.ScaleHeight -<BR>imgPlayer(0).Height) Then<BR>imgPlayer(0).Top = picDesert.ScaleHeight - <BR>imgPlayer(0).Height<BR>End If<BR>End Select<BR>InKeyDown = False<BR>End Sub</P>
<P>Private Sub Form_Load()<BR>Dim i As Integer<BR>Timer1.Interval = 22<BR>Timer1.Enabled = False<BR>MouseButtonDown = NO_BUTTON<BR>For i = 1 To NUM_BULLETS - 1<BR>Load imgLBullet(i)<BR>Load imgRBullet(i)<BR>Next<BR>End Sub</P>
<P>Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR>MouseButtonDown = Button<BR>End Sub</P>
<P>Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)<BR>MouseButtonDown = NO_BUTTON<BR>End Sub</P>
<P>' The main game timer.<BR>Private Sub Timer1_Timer()<BR>Const CactusIncrement = 30<BR>Const BulletIncrement = 300<BR>Const NumCacti = 2</P>
<P>Dim i As Integer<BR>Dim rc As Integer<BR>' Move the roving cacti.<BR>For i = 0 To NumCacti - 1<BR>imgCactus(i).Top = imgCactus(i).Top - CactusIncrement<BR>If imgCactus(i).Top < -imgCactus(i).Height Then<BR>imgCactus(i).Top = picDesert.Height<BR>End If<BR>Next<BR>' Did player 0 fire a bullet?<BR>If GunFired(0) Then<BR>GunFired(0) = False<BR>' Find a spare (invisible) bullet.<BR>For i = 0 To NUM_BULLETS - 1<BR>If Not imgLBullet(i).Visible Then<BR>imgLBullet(i).Top = imgPlayer(0).Top<BR>imgLBullet(i).Left = imgPlayer(0).Left + <BR>(imgPlayer(0).Width / 2)<BR>imgLBullet(i).Visible = True<BR>Exit For<BR>End If<BR>Next<BR>End If<BR>' Did player 1 fire a bullet?<BR>If GunFired(1) Then<BR>GunFired(1) = False<BR>' Find a spare (invisible) bullet.<BR>For i = 0 To NUM_BULLETS - 1<BR>If Not imgRBullet(i).Visible Then<BR>imgRBullet(i).Top = imgPlayer(1).Top<BR>imgRBullet(i).Left = imgPlayer(1).Left -<BR>(imgPlayer(1).Width / 2)<BR>imgRBullet(i).Visible = True<BR>Exit For<BR>End If<BR>Next<BR>End If<BR>' Move Visible Bullets<BR>For i = 0 To NUM_BULLETS - 1<BR>' Move player 0's bullets.<BR>If imgLBullet(i).Visible Then<BR>imgLBullet(i).Left = imgLBullet(i).Left + BulletIncrement<BR>If Collided(imgLBullet(i), imgCactus(0)) Then<BR>imgLBullet(i).Visible = False<BR>ElseIf Collided(imgLBullet(i), imgCactus(1)) Then<BR>imgLBullet(i).Visible = False<BR>ElseIf imgLBullet(i).Left > picDesert.ScaleWidth Then<BR>imgLBullet(i).Visible = False<BR>ElseIf Collided(imgLBullet(i), imgPlayer(1)) Then<BR>imgLBullet(i).Visible = False<BR>imgPlayer(1).Picture = imgRIP.Picture<BR>Timer1.Enabled = False<BR>rc = sndPlaySound(App.Path & "\OH!!.WAV", SND_ASYNC)<BR>End If<BR>End If<BR>' Move player 1's bullets.<BR>If imgRBullet(i).Visible Then<BR>imgRBullet(i).Left = imgRBullet(i).Left - BulletIncrement<BR>If Collided(imgRBullet(i), imgCactus(0)) Then<BR>imgRBullet(i).Visible = False<BR>ElseIf Collided(imgRBullet(i), imgCactus(1)) Then<BR>imgRBullet(i).Visible = False<BR>ElseIf imgRBullet(i).Left < -imgRBullet(i).Width Then<BR>imgRBullet(i).Visible = False<BR>ElseIf Collided(imgRBullet(i), imgPlayer(0)) Then<BR>imgRBullet(i).Visible = False<BR>imgPlayer(0).Picture = imgRIP.Picture<BR>Timer1.Enabled = False<BR>rc = sndPlaySound(App.Path & "\OH!!.WAV", SND_ASYNC)<BR>End If<BR>End If<BR>Next<BR>End Sub</P>
<P>' Handle Player 1's movement (up and down).<BR>Private Sub tmrMouseCntl_Timer()<BR>If Not Timer1.Enabled Then Exit Sub<BR>Select Case MouseButtonDown<BR>Case RBUTTON<BR>imgPlayer(1).Top = imgPlayer(1).Top - PlayerIncrement<BR>If imgPlayer(1).Top < 0 Then imgPlayer(1).Top = 0<BR>Case LBUTTON<BR>imgPlayer(1).Top = imgPlayer(1).Top + PlayerIncrement<BR>If imgPlayer(1).Top > (picDesert.ScaleHeight -<BR>imgPlayer(1).Height) Then<BR>imgPlayer(1).Top = picDesert.ScaleHeight -<BR>imgPlayer(1).Height<BR>End If<BR>End Select<BR>End Sub</P>
<P><BR> 文章的上述内容对射击游戏中的各个实现功能进行了详细的介绍,读者朋友可以根据文章中的程序代码自己动手实验一下。本程序在Windows2000、Visual Basic6.0环境下编译通过,运行正常。</P></td><td width="30"> </td></tr></table><table width="577" border="0" cellspacing="0" cellpadding="0"><tr><td width="30" height="50"> </td><td width="547" valign="bottom" class="normal_f">出处:www.vbzx.net 日期:2005-04-14</td></tr></table></div><table width="577" border="0" cellspacing="0" cellpadding="0"><tr><td bgcolor="#C4C4C4"><img src="/images/x.gif" width="1" height="1"></td></tr></table><table width="577" border="0" cellspacing="0" cellpadding="0"><tr><td width="100%" bgcolor="#C4C4C4"><script language="JavaScript">document.write("<script src=\"/inc/comment.jsp?channel=news&infoId=3078\"><\/script>");</script></td></tr></table><table width="577" border="0" cellspacing="0" cellpadding="0"><tr><td bgcolor="#C4C4C4"><img src="/images/x.gif" width="1" height="1"></td></tr></table><table width="577" border="0" cellspacing="0" cellpadding="0"><tr><td width="50%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="20" rowspan="7"> </td><td height="30" class="t_font">相关文章:</td></tr><tr><td colspan="2" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" valign="top"><script language="JavaScript">document.write("<script src=\"/inc/relation.jsp?newskeys=VB,编程,游戏&id=3078\"><\/script>");</script></table></td></tr><tr><td colspan="2"> </td></tr></table></td><td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="20" rowspan="7"> </td><td height="30" class="t_font">相关软件:</td></tr><tr><td colspan="2" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" valign="top"><script language="JavaScript">document.write("<script src=\"/inc/relation_soft.jsp?softkeys=VB,编程,游戏&id=3078\"><\/script>");</script></table></td></tr><tr><td colspan="2"> </td></tr></table></td></tr></table></td><td width="1" valign="top" bgcolor="#C4C4C4"><img src="/images/x.gif" width="1" height="1"></td></tr></table><table width="770" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#C4C4C4"><tr><td width="770" height="1"><img src="/images/x.gif" width="1" height="1"></td></tr></table><table width="770" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td><img src="/images/last2.gif" width="770" height="18"></td></tr></table><Script language="JavaScript" src="/foot.js"> // </Script></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -