Learning 2D Game Programming: Basic FrameWork Part 3 - Adding the Enemy


Table of Contents
  1. Introduction
  2. The AlienShip Type
  3. Adding the Enemy
  4. Summary
  5. Blitzmax commands introduced in this tutorial

Introduction

In the previous tutorial we were introduced to the Basic Game Framework and previous to that we use the User Define Types to create a template for our Player and subsequently created (instantiated) the player from the Type Template.

In this tutorial, we are going to again use the User Defined Type to create a blueprint from which we can create Alienships as the enemy in our simple game.

The AlienShip Type

Our AlienShip need to have the following attributes and behaviours:-
The Type will be called TAlienShip and the three field declarations are for the X, Y locations and Speed respectively. Note that we have created default (or starting) values for these attributes.

   
Type TAlienShip
    Field X:Int = 320
    Field Y:Int = 0

    Field Speed:Int=3
    Field Image:TImage
 


The Create Function (also sometimes called the Constructor of an Object) is very similar to the one we saw for TSpaceShip. In fact the only change is to substitute the word SpaceShip with AlienShip
   
    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

       
        Return Ship
    End Function

 


The UpdateState Method simply substracts the value of Speed (in this case 3) to the X position attribute. Y remains unchanged. We also need to reset the X position once its gone over the left side of the graphic screen.

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

The DrawSelf() Method is the same as what we have seen previously.

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

The SETUP GAME CONDITION block now look like this

    ' -----------------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)
Local Alien:TAlienShip = TAlienShip.Create(URL+"/cartoonufo_1-1.png",320,0)

The image we are using is this (from www.limefly.net)


And the MAIN LOOP looks like this:-



   
' ---------------------MAIN LOOP-------------------------
Repeat
         Cls
        Player.UpdateState()
        Player.DrawSelf()
        Alien.UpdateState()
        Alien.DrawSelf()

        Flip
Until KeyDown(KEY_ESCAPE) Or AppTerminate()


Adding the Enemy

Putting everything together yields:-

   
' -----------------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)
Local Alien:TAlienShip = TAlienShip.Create(URL+"/cartoonufo_1-1.png",320,0)

' ---------------------MAIN LOOP-------------------------
Repeat
        Cls
        Player.UpdateState()
        Player.DrawSelf()
        Alien.UpdateState()
        Alien.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


Type TAlienShip
    Field X:Int = 320
    Field Y:Int = 0

    Field Speed:Int=3
    Field Image:TImage


    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

       
        Return Ship
    End Function

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

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

End Type

If we build and run the above program we now get an AlienShip moving across our screen and the player ship at the lower end with the ability to move left and right. Now all we need is something to shoot the alien with :)



Summary

This is just a very simple tutorial to show how we can add new game objects. This however is just a transition to our next tutorial where we will be introduced to a new revised framework.

Blitzmax commands introduced in this tutorial

None :)

Back to the index, Previous Tutorial, Next Tutorial