65.9K
CodeProject 正在变化。 阅读更多。
Home

航空战

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (19投票s)

2008年11月17日

CPOL

2分钟阅读

viewsIcon

51177

downloadIcon

2259

这是用 Visual Studio 开发的非常简单的 2D 桌面游戏

Sample Image

引言

这是一个非常简单的 2D 桌面游戏,使用 Visual Studio 2003 开发,适用于 Windows 2000/XP/Vista(不支持 Win 98 和 Millennium)。 在这个游戏中,您必须用有限的火箭摧毁敌人,有多个关卡和不同的难度。

您可以通过拖动鼠标移动您的战斗机,点击战斗机进行射击,您也可以使用键盘(方向键和空格键),请注意您必须射击飞机的中心部分才能摧毁它们。

右键单击战斗机将打开一个菜单,用于更改一些选项或退出游戏。

背景

我于 2006 年 9 月编写了此代码,并将其发布在 PSC 上,我只是决定将其复制到这里。

Using the Code

此程序使用 Form 的透明度和动画图片来显示简单的动画。 所有对象都在运行时创建并添加到集合中,因此我们可以轻松地控制和销毁它们。

有 3 个计时器控制移动和命中;您可以在以下图表中看到程序的不同部分

A. 这是第一部分,当您开始游戏时;它显示一个信息面板并设置屏幕和游戏级别等等 ....

B. 当您发射火箭时,启动以下机制

C. 这是控制命中、剩余敌人和火箭以及游戏结束的主要计时器

完整的图表大概是这样的

现在我尝试解释更多

1.

这些是 mdlMain 模块中的一些常用变量

Friend myMaxWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width
Friend myMaxHeight As Integer = Screen.PrimaryScreen.WorkingArea.Height
Friend myLevel As Integer = 1 ' Current level of game
Friend mySpeed As Integer 'Speed of airplanes
Friend myEnemies As Integer 'Total enemies (airplanes)
Friend myRemainEnemies As Integer ' Enemies that are not on the screen, 
  ' but when one airplane crashes, one appears on screen.
Friend myTotalRockets As Integer
Friend myTotalScore As Integer
Friend myLevelScore As Integer
Friend playSound As Boolean = True

我使用了 3 个 集合 来存储飞机、火箭和爆炸,如下所示

Private myPlanesCollection As New Collection
Private myRocketsCollection As New Collection
Private myExplodesCollection As New Collection

我制作了一些用于游戏不同部分的 Forms,并定义了一些变量在运行时使用

Private myShowLevel As New frmLevel ' show level of game
Private myGameOver As New frmGameOver ' show when game is over
Private myBackground As New frmBackground ' show level,number of enemies,
                                          ' rockets and game score.
Private myRocket As frmRocket ' for making new Rockets
Private myExplode As frmExplode  'for making new explodes
Private WithEvents myPlanes As frmPlanes ' for making new airplanes

2.

Private Sub frmMain_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load
    Randomize()
    myShowLevel.ShowDialog() ' show level of game
    myBackground.Show() ' show information screen
    ' location of your fighter at the first run
    Me.Location = New Point(myMaxWidth / 2, myMaxHeight - Me.Height - 20)     ReStart()
End Sub

这是 myShowLevel.ShowDialog()

以下是 myBackground.Show()

3.

' when click "Restart" or Game over or new level, it reset game.
Private Sub ReStart()
    Call KillAll()
    .
    .
    .
End Sub

' remove all objects from screen and collections 
Private Sub KillAll()
    For Each myPlane As frmPlanes In myPlanesCollection 'remove all airplanes
        myPlanesCollection.Remove(1)
        myPlane.Dispose()
    Next
    For Each myxExplode As frmExplode In myExplodesCollection 'remove all explodes
        myExplodesCollection.Remove(1)
        myxExplode.Dispose()
    Next
    For Each myRocket As frmRocket In myRocketsCollection 'remove all rockets
        myRocketsCollection.Remove(1)
        myRocket.Dispose()
    Next
End Sub

4.

' when click "Restart" or Game over or new level, it reset game.
Private Sub ReStart()
    Call KillAll()
    myLevelScore = 0
    myTotalRockets = 50 + myLevel * 5 + IIf(myLevel Mod 2 = 0, 0, 15) 'some levels are easier
    myRemainEnemies = 5 + myLevel * 5
    mySpeed = 300 + Rnd(1) * 200 + IIf(myLevel Mod 2 = 0, 0, 200)
    ''Debug.WriteLine("mySpeed : " & mySpeed)
    addPlanes(myRemainEnemies)
    myBackground.RefreshData()
    Me.BringToFront()
End Sub

5.

' This part adds planes to your game
Private Sub addPlanes(ByVal myCount As Integer)
    Static myPlanesNo As Integer

    For i As Integer = 1 To myCount
        myPlanes = New frmPlanes
        myPlanesNo = myPlanesNo + 1
        myPlanes.Tag = "F" & myPlanesNo ' for control a special plane in a collectin
        myPlanesCollection.Add(myPlanes, "F" & myPlanesNo)
        myPlanes.Show()
        myRemainEnemies -= 1
        If myPlanesCollection.Count >= 5 Then Exit For ' you have only 5 active airplane
                                                       ' on screen. 
                                                       '( more planes need faster CPU )
    Next
    myEnemies = myRemainEnemies + myPlanesCollection.Count
    myBackground.RefreshData()
End Sub

我使用了 5 种不同类型的飞机和直升机作为敌人

6.

'timer for moving airplanes on screen, when planes are out of screen,
' they will placed in new location at top of screen
Private Sub tmrPlane_Tick(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles tmrPlane.Tick

    For Each myPlane As frmPlanes In myPlanesCollection
        If (myPlane.Location.X > myMaxWidth * 1.2 OrElse myPlane.Location.Y >
            myMaxHeight) AndAlso myPlane.planeType Mod 2 = 0 Then
            'airplane is out of screen at right
            myPlane.planeSize = Int(Rnd(1) * 25) + 5  ' random size at new location
            ' when I want to change location to new area at left, size of 
            ' plane is too big and make a bad effect.
            myPlane.Size = New Size(1, 1) 
            Application.DoEvents()
            ' new location at left
            myPlane.Location = New Point(Int(Rnd(1) * myMaxWidth / 4) - 100, -50)
        ElseIf myPlane.Location.X < -myMaxWidth *
            0.2 OrElse myPlane.Location.Y > myMaxHeight Then
            'airplane is out of screen at left
            myPlane.planeSize = Int(Rnd(1) * 25) + 5 ' random size at new location
            ' new location at right
            myPlane.Location = New Point(myMaxWidth + Int(Rnd(1) * myMaxWidth / 4), -50)
        End If

        myPlane.planeSize += (mySpeed / 800)
        If myPlane.planeSize > 200 Then myPlane.planeSize = 200
        myPlane.picPlanes.Size = New Size(myPlane.planeSize,
            myPlane.planeSize * 0.5)
        myPlane.Size = myPlane.picPlanes.Size
        myPlane.Location = New Point(myPlane.Location.X + IIf(myPlane.planeType Mod 2 = 0,
            1, -1) * (myPlane.Width / 20), myPlane.Location.Y + mySpeed / 500)
    Next
End Sub

7.

当您单击您的战斗机时,以下过程会发射火箭。

Private Sub PicHunter_MouseUp(ByVal sender As Object,
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles PicHunter.MouseUp
    canMoveHunter = False
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Call Shoot()
    End If
End Sub

您也可以通过键盘发射火箭。

'this is for controlling fighter by keyboard
Private Sub frmMain_KeyDown(ByVal sender As Object,
    ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    Dim myNewx As Integer

    Select Case e.KeyCode
        Case Keys.Left ' move your fighter to left
            myNewx = Me.Location.X - 12 : If myNewx < -Me.Width / 2 Then myNewx =
                -Me.Width / 2
            Me.Location = New Point(myNewx, Me.Location.Y)
        Case Keys.Right ' move your fighter to right
            myNewx = Me.Location.X + 12 : If myNewx > 
                myMaxWidth - Me.Width / 2 Then myNewx = myMaxWidth - Me.Width / 2
            Me.Location = New Point(myNewx, Me.Location.Y)
        Case Keys.Up
            mySpeed = mySpeed + 10
            If mySpeed > 800 Then mySpeed = 800
            myBackground.RefreshData()
        Case Keys.Down
            mySpeed = mySpeed - 10
            If mySpeed < 200 Then mySpeed = 200
            myBackground.RefreshData()
        Case Keys.Space, Keys.ControlKey    ' you can shoot by keyboard
            Call Shoot()
        Case Keys.Escape ' exit
            Call plzEnd()
    End Select
    Application.DoEvents()
End Sub

8.

' Launch a rocket
Private Sub Shoot()
    Static RocketNo As Integer
    Static myWing As Boolean  ' rockets launch from left and right of your fighter 
    Static IamBusy As Boolean ' preventing of running again when last rocket is
                              ' still launching

    If Not IamBusy Then
        IamBusy = True
        If myTotalRockets > 0 Then
            ' Note: VS2005 uses another way to play sound, you must remark following
            ' line and active second line
            If playSound Then sndPlaySound("Rocket.wav", 1) ' This is for VS 2003
            'If playSound Then My.Computer.Audio.Play("Rocket.wav",
                AudioPlayMode.Background) ' This is for VS 2005
            myWing = Not myWing
            myTotalRockets -= 1
            RocketNo += 1
            myRocket = New frmRocket ' make new one
            myRocket.Top = Me.Location.Y + Me.Height / 3
            myRocket.Left = IIf(myWing, Me.Location.X + Me.Width / 3.2,
                Me.Location.X + Me.Width / 1.8)
            myRocket.Tag = "R" & RocketNo
            myRocketsCollection.Add(myRocket, "R" & RocketNo)
            myRocket.Show()
            myBackground.RefreshData()
            Me.BringToFront()
        End If
        IamBusy = False
    End If
End Sub

9.

'timer for moving rockets to top of screen
Private Sub tmrRocket_Tick(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles tmrRocket.Tick
    For Each myRocket As frmRocket In myRocketsCollection
        If myRocket.Top < 0 Then
            ' when a rocket is out of screen at top, it must remove.
            ''Debug.WriteLine("Rocket " & myRocket.Tag & " disposed " & myRocket.Top)
            myRocketsCollection.Remove(myRocket.Tag)
            myRocket.Dispose()
        Else
            myRocket.Location = New Point(myRocket.Location.X, myRocket.Location.Y - 8)
        End If
    Next
End Sub

10.

' important part: timer for control hits and crashes
Private Sub tmrMain_Tick(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles tmrMain.Tick
    Dim myRocketX As Single
    Dim myRocketY As Single
    Dim myPlanesX1 As Single
    Dim myPlanesX2 As Single
    Dim myPlanesY1 As Single
    Dim myPlanesY2 As Single
    Static myExplodeNo As Integer
    .
    .
    .
        
End Sub

11.

    .
    .
    .
    'check rockets status
    For Each myRocket As frmRocket In myRocketsCollection
        myRocketX = myRocket.Location.X + myRocket.Width / 2
        myRocketY = myRocket.Location.Y
        If myRocketY > 20 Then
            ' check position of airplanese and check for contact with rocket
            For Each myPlanes As frmPlanes In myPlanesCollection
                ' only center of airplane is important for crash
                myPlanesX1 = myPlanes.Location.X + myPlanes.Width / 4
                myPlanesX2 = myPlanes.Location.X + myPlanes.Width / 4 * 3
                myPlanesY1 = myPlanes.Location.Y - 10
                myPlanesY2 = myPlanes.Location.Y + myPlanes.Height

                If myRocketX > myPlanesX1 AndAlso myRocketX <
                        myPlanesX2 AndAlso myRocketY > myPlanesY1 AndAlso myRocketY <
                        myPlanesY2 Then
                    ' create an explode form, replace it with airplane form and also
                    ' remove rocket from screen
                    ' Note: VS2005 uses another way to play sound, you must remark
                    ' following line and activate second line
                    If playSound Then sndPlaySound("explode.wav", 1) ' This is for VS 2003 
                    'If playSound Then My.Computer.Audio.Play("explode.wav",
                    '    AudioPlayMode.Background) ' This is for VS 2005
                    myLevelScore += 
                        ' if you hunt an enemy in top of screen, its score is 
                        ' more then in middle or bottom of screen.
                        Int((myMaxHeight - myPlanesY2) / myMaxHeight * 50) * 20 
                    myExplodeNo += 1
                    myExplode = New frmExplode ' create new one
                    Application.DoEvents()
                    myExplode.Top = myPlanes.Location.Y
                    myExplode.Left = myPlanes.Location.X
                    myExplode.Tag = "X" & myExplodeNo
                    myExplode.Text = "OK"
                    myExplodesCollection.Add(myExplode, "X" & myExplodeNo)
                    myExplode.Show() ' show explode form
                    myExplode.Size = myPlanes.Size
                    Me.BringToFront()
                    '
                    'Debug.WriteLine("planes " & myPlanes.Tag & " crashed  " & myPlanesY1)
                    myPlanesCollection.Remove(myPlanes.Tag) ' remove plane from collection
                    myPlanes.Dispose()
                    'Debug.WriteLine("Rocket " & myRocket.Tag & " exploded " & myRocket.Top)
                    myRocketsCollection.Remove(myRocket.Tag) ' remove rocket from collection
                    myRocket.Dispose()
                    myEnemies = myRemainEnemies + myPlanesCollection.Count
                    myBackground.RefreshData()
                    .
                    .
                    .

12.

12A.

                    .
                    .
                    .
                    ' if there is reserved airplane, show one of them.
                    If myRemainEnemies > 0 Then addPlanes(1) 
                    .
                    .
                    .

12B.

                    .
                    .
                    .
                    If myPlanesCollection.Count = 0 Then ' check for end of level
                        'remaining rockets have score
                        myTotalScore += 1000 + myLevelScore + myTotalRockets * 100
                        Call KillAll()
                        myLevel += 1
                        myShowLevel.ShowDialog() ' go to the next level
                        ReStart()
                    End If
                    Me.BringToFront()
                    Exit For
                    End If
                    .
                    .
                    .

12C.

    .
    .
    .
    ' when you lose all rockets but airplanes remained, game is over.
    If myTotalRockets <= 0 AndAlso myRocketsCollection.Count = 
        0 AndAlso myPlanesCollection.Count > 0 Then
        ' show game over form, maybe user want to exit.
        If myGameOver.ShowDialog() = Windows.Forms.DialogResult.Cancel Then plzEnd()
        ReStart()
    End If
    .
    .
    .

这是 "游戏结束" 窗口

关注点

您可以为游戏添加背景或使用新的飞机和更多关卡。

历史

这是第一次上传。  

 

© . All rights reserved.