Learning 2D Game Programming: Basic Framework Part 2 - Introducing the Basic Game Framework


Table of Contents
  1. The Basic Game Framework
  2. Summary
  3. Blitzmax commands introduced in this tutorial

The Basic Game Framework

In the previous tutorial we saw how we could create a player controlled object (which we called a SpaceShip) using the BlitzMax feature called User Defined Types. We also had a rudimentary way of controlling its movement using the mouse.

In this tutorial we are going to learn how to
Before we start to learn anything, type in or cut and paste the program below into the MaxIDE and click on the 'Build and Run' button. Use the arrow keys on the keyboard to move the spaceship. To exit the program either press the ESCAPE key or click on the close window button.

    ' Tutorial 3: The Game Loop
' -----------------SETUP GAME CONDITIONS--------------------
Graphics 640,480,0

Local URL:String="http::www.2dgamecreators.com/../basic/"
Local Player:TSpaceShip = TSpaceShip.Create(URL+"/blobship_1-1.png",320,420)

' ---------------------MAIN LOOP-------------------------
Repeat
        Cls
        Player.UpdateState()
        Player.DrawSelf()
        Flip
Until KeyDown(KEY_ESCAPE) Or AppTerminate()
End

' ---------------------TYPES, ATTRIBUTES AND BEHAVIOURS-------------------------
Type TSpaceShip
    Field X:Int = 320
    Field Y:Int = 420

    Field Speed:Int=3
    Field  Image:TImage
   
    Function Create:TSpaceShip(File:String,xstart:Int,ystart:Int)
        Local Ship:TSpaceShip=New TSpaceShip
        Ship.X=xstart
        Ship.Y=ystart
        Ship.Image=LoadImage(LoadBank(File))

       If Ship.Image=Null
           Print "Not able to load image file. Program aborting"
           End
       EndIf

       
        Return Ship
    End Function

    Method UpdateState()
   
        If KeyDown(KEY_LEFT)
            X :- Speed
        EndIf   
        If KeyDown(KEY_RIGHT)
            X :+ Speed
        EndIf
           
        If X<0 Then X=0
        If X>620 Then X=620
       
    End Method
   
   Method DrawSelf()
        DrawImage Image, X,Y
    End Method


End Type


Now that you have seen how the program works, let us go through the lines to make sure we understand what is happening here

First note that we are calling our player Player instead of SpaceShip and we are now using a Create function to create our spaceship given the image filename and its starting location

    Local URL:String="http::www.2dgamecreators.com/../basic/"
Local Player:TSpaceShip = TSpaceShip.Create(URL+"/blobship_1-1.png",320,420)

 
Notice how I have brought together various parts of the codes from the previous tutorial into a Create function will which Create a Player for us given the image File and its starting X and Y position.
    Type TSpaceShip
    Field X:Int = 320
    Field Y:Int = 420

    Field Speed:Int=3
    Field  Image:TImage

   
    Function Create:TSpaceShip(File:String,xstart:Int,ystart:Int)
        Local Ship:TSpaceShip=New TSpaceShip
        Ship.X=xstart
        Ship.Y=ystart
        Ship.Image=LoadImage(LoadBank(File))

       If Ship.Image=Null
           Print "Not able to load image file. Program aborting"
           End
       EndIf

             
       Return Ship
    End Function

 Main Game Loop now look like this,
    Repeat
        Cls
        Player.UpdateState()
        Player.DrawSelf()
        Flip
Until KeyDown(KEY_ESCAPE) Or AppTerminate()

The Game Loop will continue to repeat until the ESCAPE key is pressed or Window Close button is clicked. 

The KeyDown(KEY_ESCAPE)  function will be true when then the ESCAPE key is pressed. Similarly the AppTerminate() function will be true when the Window Close Button is clicked.

So the loop will exit when either the ESCAPE key is pressed OR the Close button is clicked.

Notice below how we now have numbers after the Field variable declaration. BlitzMax allows us to assign default numbers to variables. In this instance we want our spaceship to be located at coordinates 320,420 at the beginning of the game. If you have not noticed this before, try re-running the above program again. The speed (which in this case is the number of pixels moving per frame is set to 3).

    Type TSpaceShip
    Field X:Int = 320
    Field Y:Int = 420

    Field Speed:Int=3

The UpdateState Method now checks for keypresses, if the  LEFT cursor key was pressed, then the location variable X is reduced by the amount stored in variable Speed, in this case 3 pixels. Similarly for when the RIGHT cursor key is pressed the X position increases by 3 pixels.

       Method UpdateState()
   
        If KeyDown(KEY_LEFT)
            X :- Speed
        EndIf   
        If KeyDown(KEY_RIGHT)
            X :+ Speed
        EndIf
           
        If X<0 Then X=0
        If X>620 Then X=620
       
    End Method

The IF statements are then used to make sure the location variable X does not go outside the screen..

       Method UpdateState()
   
        If KeyDown(KEY_LEFT)
            X :- Speed
        EndIf   
        If KeyDown(KEY_RIGHT)
            X :+ Speed
        EndIf
           
        If X<0 Then X=0
        If X>620 Then X=620
       
    End Method


The DrawSelf() Method was left unchanged from previously.
 
       Method DrawSelf()
        DrawImage Image, X,Y
    End Method
    

Summary

There you have it a fairly simple game structure which demonstrate a fundamental structure of any game, the game loop where all the action happens.

Notice the three main sections in the above code
This setup is the basic framework of our game program.

Blitzmax commands introduced in this tutorial

Only two new functions here:-

Function AppTerminate()
True if user has requested to terminate application.

Function KeyDown( key )
Check for key state, returns True if key is currently down..

Keyword Function
Begin a Function declaration.

Keyword Return
Return from a Function.

Keyword EndFunction
End a Function declaration.

Back to the index, Previous Tutorial, Next Tutorial