Menu

howtocreategames

How To Create Games Using yLib Game Engine

Its very easy using VB6 to create a simple yLib game.
yLib provides a class named GameEngine to create your games but you can't use this class directly,
Because of it is a abstract class and it want a IGame inherited class in the constructor.

To Improve First Problem (Abstraction)

You can use GameEngine like the code below.

Dim g As yLib.GameEngine
Set g = GameEngine(...)

But there is a problem with arguments which is that first argument is IGameInterface As IGame


To Improve Second Problem (IGame)

You must put above code in a class that Implements yLib.IGame
See the code below ,its written in a class.

Implements yLib.IGame

Dim g As yLib.GameEngine

Public Sub Run() ' this method must call in sub Main()
Set g = GameEngine(Me,"Game_Name","800x600")
End Sub

And also you must implement IGame's class methods

Private Sub IGame_Initialize(ByVal e As yLib.GameEngineInitializationEventArgs)
calls when GameEngine method calling

End Sub

Private Sub IGame_Load(ByVal e As yLib.GameEngineTimingEventArgs)
calls when GameEngine execution ends

End Sub

Private Sub IGame_Update(ByVal e As yLib.GameEngineTimingEventArgs)
occures on each frame update

End Sub

Private Sub IGame_Draw(ByVal e As yLib.GameEngineTimingEventArgs)
occures on each frame after Update event

End Sub

Private Sub IGame_TimerTick(ByVal timer As yLib.GameTimer)
occures on each timer tick event

End Sub

Private Sub IGame_Terminating(Cancel As Boolean, UserRequested As Boolean)
occures when game is about to terminating

End Sub

Private Sub IGame_Terminated(ErrorsLogPath As String)
calls on game finish terminating

End Sub

Private Sub IGame_Debugger(Name As String, Description As String)
used for debug puposes ,application debug messages comes here to process.

End Sub


Here Is A Simple Screen Flash Game
{modMain Module}
Sub Main()
    Dim g As New mygame
    g.Run
    End
End Sub
{mygame Class}
Implements yLib.IGame

Dim ge As yLib.GameEngine
Dim g As yLib.VideoDevice
Dim a As yLib.AudioDevice
Dim d As yLib.y3DGenerator
Dim win As yLib.GameWindow

Dim ks As yLib.KeyboardState    provides information about keyboard and its keys.
Dim lks As yLib.KeyboardState

Public Sub Run()     this method must call in sub Main()
    Set ge = GameEngine(Me, "Game_Name", "800x600", False)
    Call ge.Run()     it freeze application till terminates
    Set ge = Nothing
 End Sub

Private Sub IGame_Initialize(ByVal e As yLib.GameEngineInitializationEventArgs)
    calls when GameEngine method calling
    Set ge = e.GameEngine
    it requires because ge is not evaluated yet till end of execution of this method

    Set g = e.GraphicDevice
    Set a = e.AudioDevice
    Set win = e.GameWindow
    Set d = e.y3D
End Sub

Private Sub IGame_Load(ByVal e As yLib.GameEngineTimingEventArgs)
    calls when GameEngine execution ends
    win.Text = "Game Window Title"
    Set ks = ge.getKeyboardState
    Set lks = ks
    Call Randomize
End Sub

Private Sub IGame_Update(ByVal e As yLib.GameEngineTimingEventArgs)
    occures on each frame update
    If (ks.DownKeys.isExist(Key) And (Not lks.DownKeys.isExist(Key))) Then
        ge.Terminate     this can cancel,but Exterminate cant.
    End If
End Sub

Private Sub IGame_Draw(ByVal e As yLib.GameEngineTimingEventArgs)
    occures on each frame after Update event
    Call ge.SuspendDrawings     suspend drawings during updating data
    Call g.Clear(QBColor(Rnd * 15))     clear the screen with random colors 30 times per second
    you can specify the fps but default fps is 30
    Call ge.ResumeDrawings     deactive SuspendDrawings method actions
End Sub

Private Sub IGame_TimerTick(ByVal timer As yLib.GameTimer)
    occures on each timer tick event
    do nothing
End Sub

Private Sub IGame_Terminating(Cancel As Boolean, UserRequested As Boolean)
    occures when game is about to terminating
    you can cancel terminate state by set Cancel = True
    but it prevent termination of the application and may cause in non closing game form!
End Sub

Private Sub IGame_Terminated(ErrorsLogPath As String)
    calls on game finish terminating
    ErrorsLogPath = app.Path & "/log.txt"
End Sub

Private Sub IGame_Debugger(Name As String, Description As String)
    used for debug puposes ,application debug messages comes here to process.
    Debug.Print Name & ": " & Description
End Sub

by Ali Mousavi Kherad


Related

Wiki: Home