
上QQ阅读APP看书,第一时间看更新
Time for action — the basic structure of your game
We will now build the basic structure for every game. Follow the given steps:
- 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
- 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
- Because we want to use the Monkey built-in framework
mojo
, we have to import it.Import mojo
- Now, we need to create the
pongo
class that extends frommojo's
app class. We will include emptyOnCreate, OnUpdate
, andOnRender
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
- 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
- Save the script now, under a name of your choice.
- 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.