Let's begin with the basic game structure:
RocketCommander.monkey
, in a folder of your choice. STRICT
mode.Strict
#rem Script: RocketCommander.monkey Description: Sample script from chapter #3 of the book "Monkey Game Development Beginners guide" by PacktPub Author: Michael Hartlef #end
gameClasses.monkey
.Import gameClasses
main
function. Without it, Monkey will complain when you try to build it.Function Main:Int()
main
function, we will create a new instance of the main class, called RocketCommander
, and store it inside a global variable called game
, which again will be created very soon in another file.game = New RocketCommander
Return True End
You might close it for now, but when you want to run the game later, this is the file you want to build from.
As you can see, this is quite an unusual approach, as we have used identifiers and objects which don't exist at the moment. But, you will create them very soon! First, we start with creating the file to import all the classes, functions, and data definitions.
gameClasses.monkey
. Strict
mode again (for this file and importing the mojo framework).Strict Import mojo
Import
command for the next file we will create.Import mainClass
gameClasses.monkey
file and close it for now.The next file we will create is called mainClass.monkey.
This will store our main class, RocketCommander
.
mainClass.monkey
. Strict
mode in this file and import the previously created file gameClasses.monkey
.Strict Import gameClasses
game
before, we will add its definition to this file now. Remember, game
holds the instance of the class RocketCommander
.Global game:RocketCommander
RocketCommander
class, which of course extends from mojo's App
class.Class RocketCommander Extends App
As usual, we will add the OnCreate, OnUpdate
, and OnRender
methods.
OnCreate
method. Remember that it will always have to contain a SetUpdateRate
statement.Method OnCreate:Int() SetUpdateRate(60) 'Set update rate to 60 fps Return True End
OnUpdate
and OnRender
methods.Method OnUpdate:Int() Return True End Method OnRender:Int() Return True End
Return True End
We have created three files for now. The RocketCommander.monkey
file holds the Main
function and is the one that has to be built when you want to compile the game. The gameClasses.monkey
file imports all other files where we actually defined the game. It's also the only file we will import in all other files. And last but not least, we have created our mainClass.monkey
file, which will hold the main class called RocketCommander
.
You could try to build and run the game now. If Monkey does not report any problems, you should see just a white screen in the browser. So let's get closer to something visible.
3.145.35.194