Click here to Skip to main content
6,596,602 members and growing! (21,296 online)
Email Password   helpLost your password?
Languages » VBScript » General     Intermediate

VBJET Fighter - Visual Basic Game

By Humayun Shabbir Bhutta

2D Game Developed using Visual Basic 6
VBScriptWin2K, WinXP, Win2003, Dev
Posted:21 Jun 2004
Updated:21 Sep 2007
Views:66,640
Bookmarked:10 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 2.32 Rating: 2.32 out of 5
2 votes, 20.0%
1
3 votes, 30.0%
2
2 votes, 20.0%
3
1 vote, 10.0%
4
2 votes, 20.0%
5

VBJET 1.1 Source Code

Sample Image - Visual_Basic_Game.gif

Offical 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. First version of this game was developed in QuickBasic for DOS, which was very primitive and i used 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 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

' Global variable delcarations

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 keyoard 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 timeer 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 enemny 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 missle

Private Sub FireIt()
VBJetMissile.Visible = True

VBJetMissile.x = shooter.Left + 100
VBJetMissile.y = shooter.Top + 50
fire.Visible = True
End Sub
'___________________________________________________________________________

' Set enemy postion

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 enemay 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, but it conveys some of 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Humayun Shabbir Bhutta


Member
Thanks for reading

Humayun Shabbir
Co-Founder
Source Systems
sourcesystems.net - similarsite.net - sharpbasic.com
Occupation: Web Developer
Location: Pakistan Pakistan

Other popular VBScript articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralWith Apologies PinmemberEnnis Ray Lynch, Jr.4:57 1 May '06  
GeneralRe: With Apologies PinmemberHumayun Shabbir Bhutta7:07 1 May '06  
GeneralVB6 Game PinsussAnonymous4:45 9 Aug '05  
GeneralRe: VB6 Game PinmemberHumayun Shabbir Bhutta16:59 10 Aug '05  
GeneralRe: VB6 Game PinsussAnonymous3:30 30 Sep '05  
NewsRe: VB6 Game PinmemberHumayun Shabbir Bhutta15:13 30 Sep '05  
GeneralMSHFlexGrid Conttol Problem Pinmembersabankocal23:57 28 Jul '05  
GeneralRe: MSHFlexGrid Conttol Problem PinmemberHumayun Shabbir Bhutta17:03 10 Aug '05  
GeneralRe: MSHFlexGrid Conttol Problem Pinmembersabankocal21:37 10 Aug '05  
GeneralLooking for comments... PinmemberHumayun Shabbir Bhutta2:45 15 Jul '05  
GeneralRe: Looking for comments... Pinsussdudethedreamer20:32 3 Aug '05  
GeneralRe: Looking for comments... Pinsussdudethedreamer20:44 3 Aug '05  
GeneralRe: Looking for comments... PinmemberHumayun Shabbir Bhutta17:19 7 Aug '05  
GeneralRe: Looking for comments... PinsussAnonymous9:50 10 Aug '05  
GeneralRe: Looking for comments... PinmemberHumayun Shabbir Bhutta17:00 10 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Sep 2007
Editor:
Copyright 2004 by Humayun Shabbir Bhutta
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project