Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / Windows Forms
Article

Aviation Fight

Rate me:
Please Sign up or sign in to vote.
4.80/5 (21 votes)
30 Nov 2008CPOL2 min read 49.2K   2.2K   43   11
This is a very simple 2D desktop game developed in Visual Studio

Sample Image

Image 2

Introduction

This is a very simple 2D desktop game developed in Visual Studio 2003 for Windows 2000/XP/Vista (doesn't work on Win 98 & Millennium ). In this game you have to destroy enemies with limited rockets, there are several levels and different difficulties.

You can move your fighter by dragging of mouse, and shoot by click on the fighter, you can also use keyboard (Arrow keys & Space bar), consider that you have to shoot to the center part of airplanes to destroy them .

Right click on the fighter will open a menu to change some options or exit of game.

Image 3

Background

I wrote this code in September 2006 and put it on the PSC, I just decided to copy it here.

Using the Code

This program uses Form's transparency and animated pictures to show a simple animation. All objects make in runtime and add to a collection so we can control and destroy them easily.

There are 3 timers to control movements and hits; you can see different parts of the program in following diagrams:

A. This is the first part, when you start the game; it shows an information panel and set screen and game level and more ....

Image 4

B. When you shoot a Rocket, following mechanism starts:

Image 5

C. And this is the main timer which control hits, remained enemies and rockets and also end of the game:

Image 6

And full diagram is something like this:

Image 7

Now I try to explain more:

1.

Image 8

These are some common variables in mdlMain module:

VB
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

I used 3 collections for Airplanes , Rockets and Explosions as following:

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

I made some Forms for different parts of game and defined some variables to use them in run time

VB
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.

Image 9

VB
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

This is myShowLevel.ShowDialog()

Image 10

And the following is myBackground.Show()

Image 11

3.

Image 12

VB
' 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.

Image 13

VB
' 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.

Image 14

VB
' 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

I used 5 different types of airplanes and helicopters as enemy

Image 15

6.

Image 16

VB
'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.

Image 17

When you click on your fighter, following procedure launchs a rocket.

VB
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

You can also shoot rockets by keyboard.

VB
'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.

Image 18

VB
' 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.

Image 19

VB
'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.

Image 20

VB
' 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.

Image 21

VB
.
.
.
'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.

Image 22

12A.

Image 23

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

12B.

Image 24

VB
.
.
.
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.

Image 25

VB
.
.
.
' 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
.
.
.

This is the "Game Over" window:

Image 26

Points of Interest

You can add a background to the game or use new airplanes and more levels.

History

This is the first upload.  

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Iran (Islamic Republic of) Iran (Islamic Republic of)

Comments and Discussions

 
QuestionTashakor! Pin
Member 113597667-Jan-15 12:00
Member 113597667-Jan-15 12:00 
GeneralThanks Pin
Mohammad Reza Khosravi5-Jan-13 19:00
Mohammad Reza Khosravi5-Jan-13 19:00 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey14-Mar-12 22:07
professionalManoj Kumar Choubey14-Mar-12 22:07 
Generaliranian boy Pin
ehsan500011-Apr-10 12:49
ehsan500011-Apr-10 12:49 
GeneralLooks Nice Pin
Rath242412-Jan-10 17:02
Rath242412-Jan-10 17:02 
GeneralVery addictive game Pin
rctaubert9-Dec-08 6:56
rctaubert9-Dec-08 6:56 
Generalthank you Pin
Ardavan Sharifi7-Dec-08 1:39
Ardavan Sharifi7-Dec-08 1:39 
General[Message Deleted] Pin
Mohammad Reza Khosravi7-Dec-08 7:19
Mohammad Reza Khosravi7-Dec-08 7:19 
GeneralRe: thank you Pin
Ardavan Sharifi7-Dec-08 10:05
Ardavan Sharifi7-Dec-08 10:05 
GeneralExcellent Pin
Mohammad Dayyan3-Dec-08 6:52
Mohammad Dayyan3-Dec-08 6:52 
GeneralMinimal changes, great improvements Pin
JoseMenendez18-Nov-08 6:59
JoseMenendez18-Nov-08 6:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.