O'Reilly logo

Monkey Game Development by Michael Hartlef

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action — creating a StartNewGame method

To start a new game, we will create a new method in the game class. Follow the given steps:

  1. Add a new method called StartNewGame.
    Method StartNewGame:Int()
    
  2. Seed the random number generator with the current millisecond value:
    Seed = Millisecs()
    
  3. Reset the Score value to 0, as shown in the following code:
    Score = 0
    
  4. Deactivate the title layer:
    layerTitle.SetActive(False)
    
  5. Remove all objects from the game layer:
    layerGame.RemoveAllObjects()
    
  6. Create a new ball:
    CreateBall()
    
  7. Create a new set of tiles:
    CreateTiles()
    
  8. Spawn a new enemy with a random x position. This position ranges from 20 to cw-20 (the canvas width minus 20):
    SpawnEnemy(Rnd(20,cw-20))
    
  9. Start with a level number of 1:
    levelNum = 1
    
  10. Set lifes to 3:
    lifes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required