![]() |
Languages »
VBScript »
General
Intermediate
VBJET Fighter - Visual Basic GameBy Humayun Shabbir Bhutta2D Game Developed using Visual Basic 6 |
VBScriptWin2K, WinXP, Win2003, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

Offical Website: http://vbjet-fighter.sourceforge.net/
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.
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.
Here is some code from the game that includes all the events and the main game loop.
' 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
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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |