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.
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 ....
B. When you shoot a Rocket, following mechanism starts:
C. And this is the main timer which control hits, remained enemies and rockets and also end of the game:
And full diagram is something like this:
Now I try to explain more:
1.
These are some common variables in mdlMain module:
Friend myMaxWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width
Friend myMaxHeight As Integer = Screen.PrimaryScreen.WorkingArea.Height
Friend myLevel As Integer = 1
Friend mySpeed As Integer
Friend myEnemies As Integer
Friend myRemainEnemies As Integer
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:
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
Private myShowLevel As New frmLevel
Private myGameOver As New frmGameOver
Private myBackground As New frmBackground
Private myRocket As frmRocket
Private myExplode As frmExplode
Private WithEvents myPlanes As frmPlanes
2.
Private Sub frmMain_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Randomize()
myShowLevel.ShowDialog()
myBackground.Show()
Me.Location = New Point(myMaxWidth / 2, myMaxHeight - Me.Height - 20) ReStart()
End Sub
This is myShowLevel.ShowDialog()
And the following is myBackground.Show()
3.
Private Sub ReStart()
Call KillAll()
.
.
.
End Sub
Private Sub KillAll()
For Each myPlane As frmPlanes In myPlanesCollection
myPlanesCollection.Remove(1)
myPlane.Dispose()
Next
For Each myxExplode As frmExplode In myExplodesCollection
myExplodesCollection.Remove(1)
myxExplode.Dispose()
Next
For Each myRocket As frmRocket In myRocketsCollection
myRocketsCollection.Remove(1)
myRocket.Dispose()
Next
End Sub
4.
Private Sub ReStart()
Call KillAll()
myLevelScore = 0
myTotalRockets = 50 + myLevel * 5 + IIf(myLevel Mod 2 = 0, 0, 15)
myRemainEnemies = 5 + myLevel * 5
mySpeed = 300 + Rnd(1) * 200 + IIf(myLevel Mod 2 = 0, 0, 200)
addPlanes(myRemainEnemies)
myBackground.RefreshData()
Me.BringToFront()
End Sub
5.
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
myPlanesCollection.Add(myPlanes, "F" & myPlanesNo)
myPlanes.Show()
myRemainEnemies -= 1
If myPlanesCollection.Count >= 5 Then Exit For
Next
myEnemies = myRemainEnemies + myPlanesCollection.Count
myBackground.RefreshData()
End Sub
I used 5 different types of airplanes and helicopters as enemy
6.
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
myPlane.planeSize = Int(Rnd(1) * 25) + 5
myPlane.Size = New Size(1, 1)
Application.DoEvents()
myPlane.Location = New Point(Int(Rnd(1) * myMaxWidth / 4) - 100, -50)
ElseIf myPlane.Location.X < -myMaxWidth *
0.2 OrElse myPlane.Location.Y > myMaxHeight Then
myPlane.planeSize = Int(Rnd(1) * 25) + 5
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.
When you click on your fighter, following procedure launchs a rocket.
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.
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
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
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
Call Shoot()
Case Keys.Escape
Call plzEnd()
End Select
Application.DoEvents()
End Sub
8.
Private Sub Shoot()
Static RocketNo As Integer
Static myWing As Boolean
Static IamBusy As Boolean
If Not IamBusy Then
IamBusy = True
If myTotalRockets > 0 Then
If playSound Then sndPlaySound("Rocket.wav", 1)
AudioPlayMode.Background)
myWing = Not myWing
myTotalRockets -= 1
RocketNo += 1
myRocket = New frmRocket
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.
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
myRocketsCollection.Remove(myRocket.Tag)
myRocket.Dispose()
Else
myRocket.Location = New Point(myRocket.Location.X, myRocket.Location.Y - 8)
End If
Next
End Sub
10.
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.
.
.
.
For Each myRocket As frmRocket In myRocketsCollection
myRocketX = myRocket.Location.X + myRocket.Width / 2
myRocketY = myRocket.Location.Y
If myRocketY > 20 Then
For Each myPlanes As frmPlanes In myPlanesCollection
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
If playSound Then sndPlaySound("explode.wav", 1)
myLevelScore +=
Int((myMaxHeight - myPlanesY2) / myMaxHeight * 50) * 20
myExplodeNo += 1
myExplode = New frmExplode
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()
myExplode.Size = myPlanes.Size
Me.BringToFront()
myPlanesCollection.Remove(myPlanes.Tag)
myPlanes.Dispose()
myRocketsCollection.Remove(myRocket.Tag)
myRocket.Dispose()
myEnemies = myRemainEnemies + myPlanesCollection.Count
myBackground.RefreshData()
.
.
.
12.
12A.
.
.
.
If myRemainEnemies > 0 Then addPlanes(1)
.
.
.
12B.
.
.
.
If myPlanesCollection.Count = 0 Then
myTotalScore += 1000 + myLevelScore + myTotalRockets * 100
Call KillAll()
myLevel += 1
myShowLevel.ShowDialog()
ReStart()
End If
Me.BringToFront()
Exit For
End If
.
.
.
12C.
.
.
.
If myTotalRockets <= 0 AndAlso myRocketsCollection.Count =
0 AndAlso myPlanesCollection.Count > 0 Then
If myGameOver.ShowDialog() = Windows.Forms.DialogResult.Cancel Then plzEnd()
ReStart()
End If
.
.
.
This is the "Game Over" window:
Points of Interest
You can add a background to the game or use new airplanes and more levels.
History
This is the first upload.