Time for action — the basic structure of your game

We will now build the basic structure for every game. Follow the given steps:

  1. Your first command in Pongo, and in every game, should be the Strict command, so that Monkey will keep us on track regarding giving identifiers the right type.
    Strict
    
  2. Next should be some comments that describe the script somehow.
    #rem
    Script: Pongo.Monkey
    Description: Sample script from chapter #2 of the book "Monkey Game Development Beginners guide" by PacktPub
    Author: Michael Hartlef
    #end
    
  3. Because we want to use the Monkey built-in framework mojo, we have to import it.
    Import mojo
    
  4. Now, we need to create the pongo class that extends from mojo's app class. We will include empty OnCreate, OnUpdate, and OnRender methods that we will fill later on.
    Class pongo Extends App
    Method OnCreate:Int()
    SetUpdateRate(60)
    Return True
    End
    Method OnUpdate:Int()
    Return True
    End
    Method OnRender:Int()
    Return True
    End
    End
    Function Main:Int()
    New pongo
    Return True
    End
    
  5. The last thing a basic Monkey script needs is the Main function. It is the starting point of every Monkey script.
    Function Main:Int()
    New pongo 'This creates a new running instance from our class
    Return True
    End
    
  6. Save the script now, under a name of your choice.
  7. To use a pre-build file for the next steps, you can load up the file S2038_02_02.Monkey and let it run by pressing Ctrl + R on Windows or cmd + R on OSX. After you have selected the HTML5 platform target and Trans has created a HTML5 project, it should start your HTML5-compatible browser and display a blank canvas, most likely in white.

This basic structure can be reused with every game you start. Only the class name should be changed from pongo to something that fits your game.

Tip

Downloading the example code for this book

You can download the example code files for all Packt books you have purchased, using your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

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

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