Time for action — creating a StartNewGame method

This method will do various things, from cleaning up the layers to initializing the tile map to setting the end time.

  1. Start a new method called StartNewGame, inside the game class.
    Method StartNewGame:Int()
    
  2. Define a local tile variable with the type INT.
    Local tile:Int = 0
    
  3. Seed the random number generator with the current value of Millisecs and set the game score to 0.
    Seed = Millisecs()
    score = 0
    
  4. Set the game mode to gmPlay and activate layers with it.
    gameMode = gmPlay
    ActivateLayer(gameMode)
    
  5. To create the objects, set the default layer to layerGame and remove all objects form the game layer and the GFX layer.
    eng.SetDefaultLayer(layerGame)
    layerGame.RemoveAllObjects()
    layerGFX.RemoveAllObjects()
    
  6. Create the tile selector and make it invisible.
    selector = eng.CreateImage(atlas,64,0,64,64,0,0)
    selector.SetVisible(False)
    
  7. Start two FOR loops, one for the rows and and one for the columns.
    For Local y:Int = 1 To rows
    For Local x:Int = 1 To columns
    
  8. Initialize the tile map by setting the value to -1. Then, close the FOR loops.
    tileMap[x-1][y-1] = -1
    Next
    Next
    
  9. Again, start two FOR loops for rows and columns.
    For Local y:Int = 1 To rows
    For Local x:Int = 1 To columns
    
  10. Add a Repeat statement.
    Repeat
    
  11. Set the value of tile with a random number ranging from 0 to 6. Set the maximum allowable value to 5.
    tile = Rnd(6)
    If tile = 6 Then tile = 5
    
  12. Insert an Until statement, which will check gems horizontally and vertically to make sure that more than one gem doesn't have the same ID. This ensures that three matching tiles are not already in a row.
    Until (CheckGemsX(x,y,tile)<2 And CheckGemsY(x,y, tile)<2)
    
  13. Set the tile map with the new tile value.
    tileMap[x-1][y-1] = tile
    
  14. Depending on the tile value, create a new gem tile.
    Local obj:=CreateTile(x,y,tile)
    
  15. Close both FOR loops, and then set the field justStarted to True.
    Next
    Next
    justStarted = True
    
  16. Set the game ending time to the current value of Millisecs + gameTime. Then, close the method.
    endTime = Millisecs()+(gameTime)
    Return 0
    End
    

What just happened?

The method StartNewGame will just do that—start a new game. Of course, it will also clean up every setting that was done before so you can start a new game afresh.

Give me some information… displaying info

While someone is playing the game, they want to know how much time is left. Basically, it is a countdown in minutes and seconds. There is no out-of-the-box functionality in Monkey that will provide such a string, so let's create one.

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

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