Learning 2D Game Programming: Basic FrameWork Part 5 - Adding Missiles


Table of Contents
  1. Introduction
  2. The New Game Program
  3. The Missile Type
  4. Modifying the TSpaceShip Type
  5. Summary
  6. Blitzmax commands introduced in this tutorial

Introduction

Now that we have a good framework in place, adding more objects into our game becomes relatively easier. To add missile firing capability we will need to things.
OK, lets us gets started

The New Game Program

I'm presenting you the new additions below. Go ahead and build and run the program so you can see what it does. The highlighted text are our new changes:-
   
' -----------------SETUP GAME CONDITIONS--------------------
Global GameObjectList:TList=CreateList()
Graphics 640,480,0

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

 ' ---------------------MAIN LOOP-------------------------
Repeat
        Cls
        For o:TGameObject=EachIn GameObjectList
               o.DrawSelf()
               o.UpdateState()
         Next
         Flip 
Until KeyDown(KEY_ESCAPE) Or AppTerminate()
End
   

' ---------------------TYPES, ATTRIBUTES AND BEHAVIOURS-------------------------

Type TGameObject

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

    Field Speed:Int=3
    Field Image:TImage

   
Method DrawSelf()
        DrawImage Image,X,Y
    End Method
    


     Method UpdateState() Abstract

End Type

 Type TSpaceShip Extends TGameObject

     Field MissileDelay:Int
   
    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

        ListAddLast GameObjectList, Ship

        Return Ship
    End Function

    Method UpdateState()
   
        If KeyDown(KEY_LEFT)
            X :- Speed
        EndIf   
        If KeyDown(KEY_RIGHT)
            X :+ Speed
        EndIf
         If KeyDown(KEY_SPACE)
            FireMissile()
        EndIf
        
MissileDelay :- 1 

        If X<0 Then X=0
        If X>620 Then X=620
       
    End Method

  
    Method FireMissile()
         If MissileDelay<0
            Local FileName:string=
"D:\Program Files\BlitzMax\samples\firepaint\bullet.png"
            Local Missile:TMissile=TMissile.Create(FileName, X+23,Y)
            MissileDelay=10
         End If
    End Method        
             
 

End Type


Type TAlienShip Extends TGameObject

    Function Create:TAlienShip(File:String,xstart:Int,ystart:Int)
        Local Ship:TAlienShip=New TAlienShip
        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

        ListAddLast GameObjectList, Ship

        Return Ship
    End Function

    Method UpdateState()
        X :- Speed
        If X<-ImageWidth(Image) Then X=620
   End Method

 
End Type

  Type TMissile Extends TGameObject

    Function Create:TMissile(File:String,xstart:Int,ystart:Int)
        Local Ship:TMissile=New TMissile

        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

        ListAddLast GameObjectList, Ship       
        Return Ship
    End Function

    Method UpdateState()
        Y :- Speed
        If Y<0  Then 
ListRemove(GameObjectList,Self)
   End Method


End Type

-.
Running the above program will give our game the ability to start firing missiles at our alien ship.

The Missile Type

Note the small differences between the TMissile Type and the TAlienShip Type. The main difference is the UpdateState() behaviour. For our missiles, once they leave the upper part of the graphic screen we should remove them from our game. 

      Type TMissile Extends TGameObject

    Function Create:TMissile(File:String,xstart:Int,ystart:Int)
        Local Ship:
:TMissile=New :TMissile
        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

        ListAddLast GameObjectList, Ship
       
        Return Ship
    End Function

    Method UpdateState()
        Y :- Speed
        If Y<0  Then 
ListRemove(GameObjectList,Self)
   End Method


End Type

The ListRemove function will remove/delete the missile from the GameObjectList as once they leave the screen we do not need them anymore. Remember that during our Create function, we inserted the created missile into the GameObjectList so that they will be updated and displayed during the Main Loop.

Modifying the TSpaceShip Type

We need to add the ability to detect the SPACEBAR and then add in the FireMissile method to our  TSpaceShip type plus adding some sort of delay to our Missile Reload Mechanism:-

      Type TSpaceShip Extends TGameObject

    Field
MissileDelay:Int

    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

  
     ListAddLast GameObjectList, Ship     
        Return Ship
    End Function
  
    Method UpdateState()
   
        If KeyDown(KEY_LEFT)
            X :- Speed
        EndIf   
        If KeyDown(KEY_RIGHT)
            X :+ Speed
        EndIf
        If KeyDown(KEY_SPACE)
            FireMissile()
        EndIf
        
MissileDelay :- 1 

        If X<0 Then X=0
        If X>620 Then X=620
       
    End Method

  
    Method FireMissile()
         If MissileDelay<0
            Local FileName:string=
"D:\Program Files\BlitzMax\samples\firepaint\bullet.png"
            Local Missile:TMissile=TMissile.Create(FileName, X+23,Y)
            MissileDelay=10
         End If
    End Method        
             
End Type

Summary

That was a relatively painless exercise of adding missile shooting capability to our game. The next obvious addition is collision! That will be in our next tutorial.

Blitzmax commands introduced in this tutorial

No new commands in this tutorial

Back to the index, Previous Tutorial, Next Tutorial