Official Website: http://vbjet-fighter.sourceforge.net/
Introduction
VBJet is a very small game developed using Visual Basic 6. I have tried to make it simple as possible, yet it performs some nice animation and control. The first version of this game was developed in QuickBasic for DOS, which was very primitive and I used the drawing function of QuickBasic to draw the graphics functions like Draw
and PSet
. Visual Basic migration was very helpful to improve the game as I was able to use the bitmap graphics for displaying the game objects like jets and missiles.
The Code
Well, its program is very simple to understand but can be little confusing as well. So here is my try to explain the working of code.
Game Objects/Classes
- JETPlane
- EnemyJETPlane
- Missile
Here is some code from the game that includes all the events and the main game loop.
Game Loop and Main Code File
Option Explicit
Dim u, d, l, r As Boolean Dim VBJet As New JETPlane
Dim VBJetMissile As New Missile
Dim EnemyJet() As New EnemyJetPlane
Dim EnemyJetCount As Integer
Dim EnemyJetMVar As Integer
Dim score As Long
Dim stage As Long
Dim level_score As Integer
Private Sub Form_Load()
lblScore.Caption = "0"
VBJet.x = 0
VBJet.y = 0
EnemyJetCount = 2
EnemyJetMVar = 10
ReDim EnemyJet(EnemyJetCount)
Dim i As Integer
For i = 1 To EnemyJetCount
EnemyJet(i).speed = 5
Load en(i)
en(i).Visible = True
SetEn i
Next i
VBJet.Power = 1
stage = 1
End Sub
Private Sub Form_Paint()
shooter.SetFocus
End Sub
Private Sub shooter_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 49 Then speed = speed - 1
If speed <= 0 Then speed = 0
If speed > 30 Then speed = 30
If KeyCode = 50 Then speed = speed + 1
If KeyCode = vbKeyLeft Then l = True
If KeyCode = vbKeyRight Then r = True
If KeyCode = vbKeyUp Then u = True
If KeyCode = vbKeyDown Then d = True
If KeyCode = vbKeySpace Then
If Not VBJetMissile.Visible Then
FireIt
End If
End If
If KeyCode = vbKeyEscape Then Unload Me: Form2.Show
End Sub
Private Sub shooter_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyLeft Then l = False
If KeyCode = vbKeyRight Then r = False
If KeyCode = vbKeyUp Then u = False
If KeyCode = vbKeyDown Then d = False
End Sub
This timer event is used to update the animation frames of Jet Fighter
Private Sub Timer1_Timer()
Static ch As Boolean
ch = Not ch
If ch Then
shooter.Picture = Picture2.Picture
Else
shooter.Picture = Picture3.Picture
End If
End Sub
Private Sub Timer2_Timer()
If l Then
VBJet.x = VBJet.x - speed
If VBJet.x < 0 Then VBJet.x = 0
End If
If r Then
VBJet.x = VBJet.x + speed
If VBJet.x >= Me.ScaleWidth - 100 Then VBJet.x = Me.ScaleWidth - 100
End If
If u Then
VBJet.y = VBJet.y - speed
If VBJet.y < 0 Then VBJet.y = 0
End If
If d Then
VBJet.y = VBJet.y + speed
If VBJet.y >= Me.ScaleHeight - 100 Then VBJet.y = Me.ScaleHeight - 100
End If
Label5.Caption = "X = " & VBJet.x
Label6.Caption = "Y = " & VBJet.y
shooter.Left = VBJet.x
shooter.Top = VBJet.y
Label3.Caption = CStr(speed)
Dim i As Integer
For i = 1 To EnemyJetCount
If VBJetMissile.Visible Then
VBJetMissile.x = VBJetMissile.x + 20
If VBJetMissile.x > Me.ScaleWidth Then
VBJetMissile.Visible = False
fire.Visible = False
End If
fire.Left = VBJetMissile.x
fire.Top = VBJetMissile.y
If level_score = 9 Then
stage = stage + 1
level_score = 0
EnemyJet(i).speed = EnemyJet(i).speed + 5 en(i).Picture = LoadPicture((App.Path & "\data\op2.gif"))
End If
If (VBJetMissile.y > EnemyJet(i).y And VBJetMissile.y < EnemyJet(i).y + 60) _
And (VBJetMissile.x > EnemyJet(i).x) Then
score = score + 10
level_score = level_score + 1
VBJetMissile.Visible = False
SetEn i
End If
Else
fire.Visible = False
End If
EnemyJet(i).x = EnemyJet(i).x - EnemyJet(i).speed
If EnemyJet(i).y > VBJet.y Then
EnemyJet(i).y = EnemyJet(i).y - (Rnd(EnemyJetMVar))
Else
EnemyJet(i).y = EnemyJet(i).y + (Rnd(EnemyJetMVar))
End If
en(i).Left = EnemyJet(i).x
en(i).Top = EnemyJet(i).y
If EnemyJet(i).x < -200 Then
SetEn i
en(i).Top = EnemyJet(i).y
End If
lblScore = CStr(score)
lblStage = CStr(stage)
Label8.Caption = "EX = " & EnemyJet(i).x
Label7.Caption = "EY = " & EnemyJet(i).y
If (VBJet.y > EnemyJet(i).y - 40 And VBJet.y < EnemyJet(i).y + 30) _
And (VBJet.x > EnemyJet(i).x And VBJet.x < EnemyJet(i).x + 100) Then
VBJet.Power = VBJet.Power + 1
Picture1.BackColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)
SetEn i
Select Case VBJet.Power
Case 2
Image1.Picture = LoadPicture(App.Path & "\data\fuel50.gif")
Case 3
Image1.Picture = LoadPicture(App.Path & "\data\fuel20.gif")
Case 4
Image1.Picture = LoadPicture(App.Path & "\data\game-over.gif")
End Select
If VBJet.Power = 4 Then
MsgBox "Game Over", vbCritical, "Shooter"
Unload Me
Form2.Show
End If
End If
Next i
End Sub
Private Sub FireIt()
VBJetMissile.Visible = True
VBJetMissile.x = shooter.Left + 100
VBJetMissile.y = shooter.Top + 50
fire.Visible = True
End Sub
Public Sub SetEn(index As Integer)
EnemyJet(index).y = Int(Rnd * Me.ScaleHeight) - 100
EnemyJet(index).x = Int(Rnd * Me.ScaleHeight) + Me.ScaleWidth
en(index).Left = EnemyJet(index).x
en(index).Top = EnemyJet(index).y
End Sub
Private Sub Timer3_Timer()
Dim i As Integer
For i = 1 To EnemyJetCount
EnemyJet(i).speed = EnemyJet(i).speed + 5
EnemyJetMVar = EnemyJetMVar + 5
Next i
End Sub
Conclusion
Although this is a very simple VB6 application, it conveys some basics of game programming. This game also proves that game programming is fun and can be started from small efforts like this one which are simple but fun.