Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / VBScript

VBJET Fighter - Visual Basic Game

Rate me:
Please Sign up or sign in to vote.
2.04/5 (12 votes)
21 Sep 2007CPOL1 min read 118.4K   2.5K   15   15
2D Game Developed using Visual Basic 6

Sample Image - Visual_Basic_Game.gif

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

VB.NET
' Global variable declarations
Option Explicit
Dim u, d, l, r As Boolean ' Basic true/false variables
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

'___________________________________________________________________________
' Forma load routine that is used to initialize things
Private Sub Form_Load()
' Startup and basic settings

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
'__________________________________________________________________________
' Called each time main window is repainted
Private Sub Form_Paint()
shooter.SetFocus
End Sub
'___________________________________________________________________________

' This is the main inputs event that monitors the user input key down events
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
'___________________________________________________________________________
' This event is called when user releases the keyboard key
Private Sub shooter_KeyUp(KeyCode As Integer, Shift As Integer)
' Check keyup event
' If keyup call then disable movement check
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
' Animation steps of Jet Fighter
If ch Then
shooter.Picture = Picture2.Picture
Else
shooter.Picture = Picture3.Picture
End If
End Sub
'___________________________________________________________________________
' This timer event is the main game loop that updates all things
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 ' Increase enemy speed
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
'___________________________________________________________________________
' Fires missile
Private Sub FireIt()
VBJetMissile.Visible = True

VBJetMissile.x = shooter.Left + 100
VBJetMissile.y = shooter.Top + 50
fire.Visible = True
End Sub
'___________________________________________________________________________
' Set enemy position
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
'___________________________________________________________________________
'This is timer event is used to move the enemy fighters.
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.

License

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


Written By
Web Developer Source Systems
Pakistan Pakistan
Thanks for reading


Humayun Shabbir
http://enthusiastprogrammer.com

Comments and Discussions

 
GeneralWith Apologies Pin
Ennis Ray Lynch, Jr.1-May-06 3:57
Ennis Ray Lynch, Jr.1-May-06 3:57 
GeneralRe: With Apologies Pin
Humayun Shabbir1-May-06 6:07
Humayun Shabbir1-May-06 6:07 
GeneralVB6 Game Pin
Anonymous9-Aug-05 3:45
Anonymous9-Aug-05 3:45 
GeneralRe: VB6 Game Pin
Humayun Shabbir10-Aug-05 15:59
Humayun Shabbir10-Aug-05 15:59 
GeneralRe: VB6 Game Pin
Anonymous30-Sep-05 2:30
Anonymous30-Sep-05 2:30 
NewsRe: VB6 Game Pin
Humayun Shabbir30-Sep-05 14:13
Humayun Shabbir30-Sep-05 14:13 
GeneralMSHFlexGrid Conttol Problem Pin
sabankocal28-Jul-05 22:57
sabankocal28-Jul-05 22:57 
GeneralRe: MSHFlexGrid Conttol Problem Pin
Humayun Shabbir10-Aug-05 16:03
Humayun Shabbir10-Aug-05 16:03 
GeneralRe: MSHFlexGrid Conttol Problem Pin
sabankocal10-Aug-05 20:37
sabankocal10-Aug-05 20:37 
GeneralLooking for comments... Pin
Humayun Shabbir15-Jul-05 1:45
Humayun Shabbir15-Jul-05 1:45 
GeneralRe: Looking for comments... Pin
dudethedreamer3-Aug-05 19:32
sussdudethedreamer3-Aug-05 19:32 
GeneralRe: Looking for comments... Pin
dudethedreamer3-Aug-05 19:44
sussdudethedreamer3-Aug-05 19:44 
GeneralRe: Looking for comments... Pin
Humayun Shabbir7-Aug-05 16:19
Humayun Shabbir7-Aug-05 16:19 
GeneralRe: Looking for comments... Pin
Anonymous10-Aug-05 8:50
Anonymous10-Aug-05 8:50 
GeneralRe: Looking for comments... Pin
Humayun Shabbir10-Aug-05 16:00
Humayun Shabbir10-Aug-05 16:00 

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.