Time for action — creating the data structure

To store data in Treasure chest, we will add some fields to the game class.

  1. First, start with two FLOAT variables, which will hold the size of the canvas.
    Class game Extends App
    Field eng:engine
    Field isSuspended:Bool = False
    Field cw:Float
    Field ch:Float
    
  2. Next, add a field to store the mode of the game. Initialize it with a constant, which we will define shortly.
    Field gameMode:Int = gmTitle
    
  3. During the game, we need to measure how much time is left. For this, we need a field for minutes and one for seconds.
    Field minutes:Int
    Field seconds:Int
    
  4. When a game begins, we need to store the time in milliseconds, to check when it will end.
    Field endTime:Int
    
  5. Add a field for the game score.
    Field score:Int = 0
    
  6. The time for one round will be stored as well. It is initialized to 2000 milliseconds. This makes it easy for testing.
    Field gameTime:Int = (1000*60*2)
    
  7. Next, add two fields to define the size of our game grid.
    Field rows:Int = 6
    Field columns:Int = 7
    
  8. To store the grid, we need a two-dimensional array. It will be initialized later.
    Field tileMap:Int[][]
    
  9. We need to store the first and second selected gem during gameplay.
    Field firstTile:ftObject=Null
    Field secondTile:ftObject=Null
    
  10. The gem selector graphic will be another object.
    Field selector:ftObject=Null
    
  11. For the first frame of a new game, when the grid has just been filled with gems, we need to have a starting Boolean field.
    Field justStarted:Bool=False
    
  12. Now, add a field to store the sprite sheet (atlas).
    Field atlas:Image
    
  13. Add another field for the game font.
    Field font1:ftFont
    
  14. To display some text information (the score, the time left, and the final score of one round of play), we need three fields.
    Field txtScore:ftObject
    Field txtTime:ftObject
    Field txtFinalScore:ftObject
    
  15. To display the high-score list, we need an array of 10 objects. Each one reassembles one entry in the list.
    Field txtHighScore:ftObject[10]
    
  16. As we want to play back some sound effects, add fields for three sounds.
    Field sndSelect:ftSound
    Field sndExplo:ftSound
    Field sndFalse:ftSound
    
  17. Now, add the game layers.
    Field layerBackground:ftLayer
    Field layerGame:ftLayer
    Field layerGFX:ftLayer
    Field layerGameOver:ftLayer
    Field layerMenu:ftLayer
    Field layerScore:ftLayer
    Field layerTitle:ftLayer
    

    Those were all the fields we need. Now, let's add some constants.

  18. To control the game, add constants for the various game modes.
    Const gmTitle:Int = 1
    Const gmMenu:Int = 2
    Const gmPlay:Int = 4
    Const gmGameOver:Int = 5
    Const gmScore:Int = 7
    
  19. In the menu, and in the high-score list screen, we have buttons that can be pressed. To identify them, we need some more constants.
    Const btnPlay:Int = 11
    Const btnScore:Int = 12
    Const btnExit:Int = 13
    Const btnBack:Int = 14
    
  20. At some point, we will delete some objects though fantomEngine's timer feature. We need an ID for this.
    Const triDelete:Int = 21
    
  21. Of course, we will use some transitions too, so add more constants for showing the menu and removing objects after a transition is finished.
    Const tmShowMenu:Int = 31
    Const tmObjRemove:Int = 32
    
  22. Finally, add some constants that will store the name of the game, the author, and the version number.
    Const strTitle:String = "Treasure Chest"
    Const strVersion:String = "1.0"
    Const strAuthor:String = "YOU"
    Method OnCreate:Int()
    

What just happened?

Phew! A lot of fields and quite a few constants! This is the biggest game in the book and you can see that in the data definition that we just created too.

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

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