NME Game Example: “Pirate Pig”

If you are familiar with webOS, you may have heard of Enyo, a Javascript framework which powers many of the built-in applications for the HP TouchPad. Recently, Enyo was released as open-source software, so you can use the framework on more platforms in addition to webOS, such as iOS, Android or BlackBerry.

If you need to create an application, Enyo is a choice you may want to consider. You can learn more at enyojs.com, and if you are a fan of Haxe-based development, there are externs for Enyo 1.0 on haxelib which can enable you to use the framework while also benefiting from the many advantages of haxe JS.

Although most of the samples for Enyo are applications, at least one sample, “Pirate Pig,” is that of a game. It is a quirky game that uses similar mechanics to Bejeweled. You can view the original version here:

http://www.enyojs.com/samples/piratepig/

The Challenge

I wanted to discover how difficult it would be to create the same game using NME. I would not use any of the original Javascript source code, but instead would use the same assets to recreate the same mechanics.

In the spirit of a game jam, I wanted to create the game in two days. In the interest of supporting my wife, I wanted to also keep it during the day instead of working late.

Day 1

After feeling inspired to make the game, I started my project at 11:00 AM.

“Maybe this is a silly question…

But why do the carrots and lemons in Pirate Pig have mouse ears?”

After around two hours, I had built the basic layout of the game. The original chooses between three board sizes, depending on the size of your screen or window. I made the decision to have the game scale smoothly, because this is easy to do in NME and I did not see another good reason to go with only a few sizes.

I was finished populating the game board, and needed to begin checking for matched tiles.

There are multiple approaches you could take for finding matches. However you look at it, it is somewhat tricky to make sure that you accurately find matches, and take into account moving or empty tile locations.

Since Bejeweled is a popular game, I thought there might be a forum or blog post somewhere with the function I was looking for. When that did not turn up as I hoped, I decided to write my own algorithm, based on a simple check for horizontal matches, then vertical matches separately. This works well enough, and though it would be nice to count bisecting matches using a higher point score, I feel that the existing code accomplishes what it needs to for now.

A few more hours and the matching/swapping behaviors are both working as expected. The “dropTiles” method is creating all kinds of madness. I put a pause on fixing the behavior, commented it out and made sure everything was solid so far. I also played with motion blur, which helped make the game feel more dynamic.

Day 2

I had made the mistake of writing “tiles[selectedTile.row][selectedTile.row]” at one part of my code by accident. No wonder my tiles were jumping out of order! I was using the row number twice where I should have been using the row and column. I also removed my timer-based logic and added an Event.ENTER_FRAME listener instead.

After an hour and a half I was ready to begin adding sound. It worked great when I tried it in Flash, webOS or Windows, but I was having problems with sounds playing on Android. A part of this was caused by a slight bug in the NME command-line tools, when using custom IDs for sounds on Android (which I fixed on SVN today), but another problem was that I was playing my sounds without saving the sound to a class variable.

As a result, my sounds on Android were being collected before the Android SoundPool began streaming. I usually save references to my sounds to prevent this issue, but on Windows, webOS and Flash it did not seem to be necessary.

I let Deborah and the kids check out the game on my TouchPad, then I submitted to the webOS App Catalog and the Android Market. I could build for iOS and submit it today as well, but I have a feeling that the application may not be approved by Apple’s picky app review team, since this is meant more as a sample than a full commercial game.

The Results

As the original game is open-source, this version of Pirate Pigs is also open-source. Please feel free to download the source code and assets to build the game on your own.

I also have posted the Flash version online, as well as packages for webOS, Android and a ZIP file for Windows. If you would like me to build and post archives for Mac, Linux or a package for iOS, please let me know in the comments.

Download

Conclusion

This has been a fun experience, and I am happy to offer a complete sample you can use.

If I were to invest more time, I would like to see a game menu, additional UI to congratulate the player when they make a lot of matches, difficulty levels, an end game screen when there are no possible matches, and a looping background music track, among other things.

However, I’ve had fun playing this game and hope this all is a benefit for you!