Time for action — creating the data structure

To store the data, we will enhance the game class with some fields and constants, as follows:

  1. First, add two text objects that will display the scores of the player and the computer.
    Class game Extends App
    Field eng:engine
    Field isSuspended:Bool = False
    Field txtScore:ftObject
    Field txtScoreC:ftObject
    
  2. Next, add two text objects that will display if the player wins or loses.
    Field txtYouWin:ftObject
    Field txtYouLoose:ftObject
    
  3. The player plane and the enemy plane will be stored in separate objects.
    Field player:ftObject
    Field enemy:ftObject
    
  4. The game font also has to be stored.
    Field font1:ftFont
    
  5. Now, add five layers for the background, the game and clouds, and also the information and the title screen.
    Field layerBackground:ftLayer
    Field layerGame:ftLayer
    Field layerClouds:ftLayer
    Field layerInfo:ftLayer
    Field layerTitle:ftLayer
    
  6. To hold all the sound effects, we need a sound object for each sound.
    Field sndHit:ftSound
    Field sndExplo:ftSound
    Field sndShoot:ftSound
    Field sndEngine:ftSound
    
  7. Add an image object called atlas, which will hold the reference to the sprite sheet.
    Field atlas:Image
    
  8. To control the flow of the game, we need one field called gameMode It will be initialized with the constant gmMenu, which we will add shortly.
    Field gameMode:Int = gmMenu
    
  9. Next, add fields to store the score for the player and the computer.
    Field score:Int = 0
    Field scoreC:Int = 0
    
  10. Also, add two fields that store the number of times one plane got hit.
    Field hits:Int = 0
    Field hitsC:Int = 0
    
  11. Because coders are lazy typists, we need to enter fields that will store the width and height of the canvas.
    Field cw:Float = 0.0
    Field ch:Float = 0.0
    
  12. To determine if the enemy plane can shoot, add a Boolean field.
    Field canShoot:Bool=True
    

    You are done adding fields. Now, add some constants. Constants make your code more readable, and they are easier to remember, as compared to numbers.

  13. Add three constants for the three game modes that we will need—one for the title screen/menu, one for the game in play, and one for when a game is over.
    Const gmMenu:Int = 1
    Const gmPlay:Int = 2
    Const gmGameOver:Int = 3
    
  14. For collision check purposes, we need three collision groups— Player, Enemy, and a group for the bullet shots.
    Const grpPlayer:Int = 1
    Const grpEnemy:Int = 2
    Const grpShot:Int = 3
    
  15. Some objects have to be deleted after a transition, so add a transition ID for this.
    Const triDelete:Int = 6
    
  16. And lastly, some objects need to be acted on after a timer finishes. We need one constant for deleting objects, and one for enabling the enemy plane to shoot again.
    Const tmDelete:Int = 15
    Const tmCanShoot:Int = 16
    Method OnCreate:Int()
    SetUpdateRate(60)
    

What just happened?

You have added some fields to hold dynamic data and some constants to make your code more readable. Good job! But, you are far from being done with the project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.172.146