Book Image

Monkey Game Development: Beginner's Guide

By : Michael Hartlef
Book Image

Monkey Game Development: Beginner's Guide

By: Michael Hartlef

Overview of this book

Monkey is a programming language and toolset that allows its user to develop modern 2D games easily for mobile and other platforms like iOS, Android, HTML5, FLASH, OSX, Windows and XNA. With Monkey you can create best selling games in a matter of weeks, instead of months.Monkey Game Development Beginner's Guide provides easy-to-follow step by step instructions on how to create eight different 2D games and how to deploy them to various platforms and markets. Learning about the structure of Monkey and how everything works together you will quickly create eight classical games and publish them to the modern app markets. Throughout the book you will learn important game development techniques like collision detection, handling player input with mouse, keyboard or touch events and creating challenging computer AI. The author explains how to emit particle effects, play sound and music files, use sprite sheets, load or save high-score tables and handle different device resolutions. Finally you will learn how to monetize your games so you can generate revenue.
Table of Contents (16 chapters)
Monkey Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
3
Game #2, Rocket Commander
4
Game #3, CometCrusher
5
Game #4, Chain Reaction
6
Game #5, Balls Out!
8
Game #7, Air Dogs 1942
9
Game #8, Treasure Chest

Time for action — read the manual


There will be a time when you will be participating in some online communities, mostly regarding game development. And of course, you will ask questions. Sometimes they will be good ones, but also sometimes questions about things that you could have figured out on your own. If other people see this, it could happen that you will get told to read the manual/instructions first before asking. Do yourself a huge favor and do just that, as it is rule number one. For every tool you use, read the instructions first. It can be dry and boring sometimes, but don't skip that task. It might come back to you, if you don't. When it comes to Monkey, it ships with a good-sized documentation. Monkey's creator and some helpers took quite some time to create the documentation. You will find information about Monkey's language, the specific modules of Monkey, and also two nice little tutorials to get you started. You will find all this when you open Monk and click on the Docs tab of the coding area.

When you are done with this, come back to this book and we will go over what you have seen. So go... now

What just happened ?

Wow!... the documentation for Monkey is quite a book (when printed)! You can get a lot of information out of it. It describes Monkey's features pretty well. But what are those features?

The Trans tool and the supported target platforms

The Trans tool translates your monkey source code into the native source code for a specific platform. Thanks to Monkey, there are already quite a few platforms you can choose from.

HTML5

Without having installed any platform SDKs and tools, such as Xcode and so on, Monkey can create HTML5 apps right out of the box. If your goal is only to create games for websites, then you won't need anything else besides a HTML5-compatible browser to develop your games. An HTML5 game is always a combination of an HTML file and a Javascript file where your game logic rests. All of your Monkey code is translated to Javascript code. Together with a calling HTML file, you can play this code directly in your browser or put it onto a web server to play it online.

FLASH

To create FLASH games, you need to have another tool installed—FLEX. We will go through this process later on in the book, so don't worry about how to install FLEX. In the process of creating a FLASH game, TRANS translates your Monkey source code into ACTIONSCRIPT code. ACTIONSCRIPT is the programming language behind FLASH apps/games. Then, after TRANS is done, FLEX compiles the created ACTIONSCRIPT code into a FLASH file. The created FLASH file can then be loaded onto a web server and can be played from there. FLASH games created with Monkey tend to perform a little faster than HTML5 apps, but that can change. To play FLASH apps/games, all you need is a browser with a FLASH player plugin installed in it.

iOS

Of course, iOS is supported. iOS is the iPhone/iPod/iPad platform, and for this, you need to have Xcode with the iOS SDK installed. Again, we will install that together later. If you want to create an iOS app, TRANS translates your Monkey code into C++ code. Then Monk will execute XCODE, which will compile your code into an iOS application. The resulting app can then be played inside the Xcode simulator. To test your game on an actual device, you need to sign up for a separate iOS developer account at Apple. There you can create the certificates needed to have an app installed on a device.

Android

The second mobile platform supported is Android. To create Android apps, you need to install the Android SDK, which we will also do together, later on. TRANS will translate your Monkey code into JAVA code. Then, the compiler of the Android SDK will create an application that you can either test on the Android simulator or on an actual device. To test an app on a device, you don't need to sign up for a Google developer account. You need to do this only when you want to sell your game on the Android market.

XNA

Behind XNA are actually three platforms: XNA for Windows desktop, for Mobile Phone 7, and for the XBOX 360. Again, you need to install an SDK for it, the Windows XNA SDK. Monkey's TRANS tool will translate your source code into C#, and then Visual Studio C# will create an application that can run on one of the mentioned XNA platforms.

GLFW

Just like XNA is basically three platforms, GLFW is two platforms. It depends on which development platform you compile your game with, OSX or Windows. For OSX, you need Xcode installed, and for Windows you need Visual Studio C++. So you actually have two platforms for the Windows desktop. Again, TRANS will translate your code into C++ source code, and the platform-corresponding SDK tools will compile that generated C++ code into an executable.

The Monkey standard modules

Modules are a huge part of the concept of Monkey. Besides the general language features, other commands are added to Monkey, via modules. They are written in Monkey code and maybe native code (C++, JavaScript, and so on). These modules either provide new commands and/or interface with commands from external native code, so you can use them in your game. In the following text, we will see what they are good for. It won't be a detailed explanation of how they work; only what you can do with them.

For more information on modules, please look into the Monkey documentation within the Module Reference section.

Lang

The lang module adds basic features to Monkey, such as Print, different data type-related handling functions, and one of the most important features, objects—the base for Monkey's class implementation. Here is a short code example:

Function Main()
Print ("Hello World")
End

Lists

If you know the concept of linked lists, this module provides something that will be familiar to you. If not, Wikipedia has a good explanation at http://en.wikipedia.org/wiki/Linked_list.

A list is a data container that lets you store something, from the same type of object as a node, inside memory. The list supports easy addition and removing from objects. You can iterate through the list of nodes in each direction. Another nice feature of a list is that you can convert it to an array of strings. To find a specific node inside the list, you need to iterate through the list, node-by-node, till you find the one you need.

Here is a small code example for a list in Monkey, without further explanation:

Function Main()
'create a new list
Local myList:=New StringList
'add some data
myList.AddLast "first entry"
myList.AddLast "second entry"
myList.AddLast "third entry"
'iterate through a list with an EachIn loop
For Local item:=Eachin myList
Print item
Next
End

Map

A map data container is similar to a list, as it is built from nodes too, and you can iterate through them. And node of a map is a pair of a value object and a key object. This key can only exist once. With it, you can retrieve the corresponding value very fast. If you need to find elements of a data container very fast, choose a map over a list.

The following image shows how map nodes have a relationship with each other. In a list, there is always a pre and post node from a current one. In a map, nodes have a loose relationship with each other. That means that you CAN iterate through a map, like you can with a list, but the real strength comes from searching nodes with the corresponding keys. So when you add a node to a map, this node will be placed inside a pool of nodes.

That is shown in the following screenshot with with Node 6. Knowing their keys, you can retrieve the corresponding data very very fast.

Here is a small code example with no further explanation:

Function Main()
'Define a string map with a string key
Local myMap:= New StringMap<String>
'insert entries
myMap.Insert("First","First entry")
myMap.Insert("Second","Second entry")
myMap.Insert("Third","Third entry")
'Print all entries
Print (myMap.Get("Third"))
Print (myMap.Get("First"))
Print (myMap.Get("Second"))
End

Math

Almost each game needs something to be calculated. The Math module provides a good set of mathematical functions for your game developing needs. Here is a nice example where the Sin function is used:

Function Main()
Local pi:Float
pi = 3.14159265
Print (Sin(pi))
End

Random

Since our game should not always run in the same way as the last time we played it, we need some randomness in it. The random module provides functions just for that—to retrieve random numbers. The following code is a little example of how to retrieve a random number:

Function Main()
'Retrieve a random number and print it inside the console
Local r:Float = Rnd(10,100)
Print (r)
End

Set

A set stores objects. It is like a shoebox that you can store your toy cars in. Like lists and maps, you can iterate through them. The difference from a list is that the objects in a set can only exist once in it. No matter how many times you add an object to it, the set will contain it only once. So it is good to keep track of a number of objects.

In the next example, you will see that the same entries won't be stored inside the set:

Function Main()
'create a new set
Local mySet:=New StringSet
'Add entries
mySet.Insert("First")
mySet.Insert("Second")
mySet.Insert("Second")
mySet.Insert("Second")
mySet.Insert("Third")
'Print all entries in the set
For Local s:= Eachin mySet
Print (s)
Next
End

Stack

Monkey provides a few ways to store data. The last module in this category is Stack. Imagine a stack of books. Basically, you can store them by placing one book on top of another (Push). If you want to take away one, you take it back from the top (Pop). Of course, you can squish a book in between the stack (Insert) or remove one from in between (Remove). And like all data storing features, you can iterate through a Stack in all directions.

Here is a small example for the usage of Stacks:

Function Main()
'create a new stack
Local myStack:=New Stack<String>
'Add entries
myStack.Push("First")
myStack.Push("Second")
myStack.Push("Third")
'Print all entries in the set
For Local s:= Eachin myStack
Print (s)
Next
End

Mojo — The 2D framework/modules

Monkey ships with a 2D framework module called Mojo. It provides all the meat (functionality) for each supported target platform. From handling graphics, to sound over input and device events, Mojo provides everything a good 2D game needs.

App

The app module provides some basic functions to load strings from the device and store/retrieve an application's state. Also, you can control the update rate for your render process with it and make it react to certain events, such as onResume, onSuspend, and others.

Audio

Ding dong!... Bang!...Wooosh! Almost all games have some kind of sound effect, and the audio module is Mojo's one-stop-shop for that. Load and play sound effects or total music for your background.

Graphics

Ladies and gents...please have a look at...the engine! The graphics module is responsible for loading and rendering images, drawing all kinds of graphical shapes, such as lines, rectangles, and circles, and also text. How else could you tell the player what to do next? Without this module, there is no output in Monkey. So this is a very important part of Monkey.

Input

When it comes to mouse input, violently hitting the keyboard, touching softly the screen of your mobile device, or tilting it wildly in every possible direction, the Input module is the way to go to retrieve information about how the user interacts with a game and its controls.

Monkey is extendable

One of Monkey's greatest features is that you can easily extend it. And the good thing is that you don't have to wait till Blitz Research does that for you. You are not bound to what the creator of Monkey will implement.

Your own modules

If you have Monkey code that you will use all the time and in every project you work on, then it is time for a module. Study how the native modules of Monkey implement new commands and functionalities.

Native source code

Another great thing is that you can add and link to external native code, import that code, and make some external declarations. It is very easy to do.

Third-party modules

Slowly, there are more and more third-party modules being developed for Monkey. Just to name a few, the following modules add some pretty interesting and valuable features:

  • fling—A physaxe 2D implementation that will add nice physics to your games

  • diddy—A great library of mixed functions

  • mnet—A network module to add access to server-based network connections

Note

These modules can be found at the following websites:

fling: http://code.google.com/p/fling/

diddy: http://code.google.com/p/diddy/

mnet: http://code.google.com/p/mnet/

You will find links to these modules and other modules inside the module registry section of Monkey's website. You can find this section at http://www.monkeycoder.co.nz/Community/modules.php.

Your own targets

The final feature you have with Monkey is that you can add your own target platforms, but, for this, you would need good knowledge of Monkey's internal structure and extensive knowledge to code for this platform. We definitely won't cover this topic further in this book. Say someone is working on a target for the Nintendo DS; you can find more about this at:

http://www.monkeycoder.co.nz/Community/posts.php?topic=652#5053.

Your game is easily portable

The last feature, which you're going to love, is that you don't have to code differently when it comes to making your game run on different platforms. Your Monkey code stays the same; the difference lies inside the created source code on each platform.