Sunday, January 30, 2011

J2ME MIDP2 Example Game with Sample Code

   My cousin recently got a decent feature-phone with a full qwerty keyboard. It's not a smartphone but it can run J2ME apps and since my Blackberry can as well, I decided to try and port my simplest example game to a Java midlet. The game isn't much beyond a few sprites, one of which the player moves up and down, but it's my game equivalent of "Hello World". I always port it to whatever new language or framework I'm learning because it contains the most basic things all games have.

   I had trouble finding resources that showed how the J2ME Canvas worked with a simple example game that would compile without errors on my system, that's what compelled me to post this. This is a simple and small working example for anyone new to J2ME who wants to see a basic game and build off it. Just be warned that I'm a Java newbie myself, this is the first thing I've ported to J2ME.

   Here's the end result, it's not much to look at:


   The first step is to get the build tool running. I'm using the older Sun Java Wireless ToolKit because it's unclear if Oracle will release the new Java ME tool for Linux. I'll give a very quick overview of how to use it and link to more detailed guides. Once you've got the latest release installed run the ktoolbar binary and start a new project, name the project 'ExampleGame' and the midlet class the same. For my Blackberry I had to set the program to use MIDP2.0 and CLDC1.1. You'll use the ktoolbar program to build and package your source code, and do your programming in your IDE or editor of choice. I use the Geany editor on Linux because of its speed and low system requirements.

   Save the example source file to the src folder, in Linux that should be under j2mewtk/2.5.2/apps/ExampleGame in your home directory. The image files will be stored in the res (for resource) directory. Try to build the program now, if there are any errors leave a comment and I'll try to help out. If it compiles, change the Device selector to "QwertyDevice" and click run to see how it looks in the phone emulator. The J2ME phone emulator sure is a heck of a lot faster than the Android emulator on my system. To build this into something you can deploy to a J2ME-capable phone select Project -> Package -> Create Package to make a jar file. This file will be located in the bin folder of the ExampleGame directory. Copy it over onto your phone and install it: check it out, you've got a base to build on.

   Now you can mess with the example source and get a feel for how it works, swap out the sprites with your own, start adding functionality, refactor it into something less ugly and noobish. That should be enough to get you started on making your own simple J2ME Canvas game. You could make Space Invaders yet again, or something new and different. Here's a list of some resources that will give you more detail than I've gone into here:

Writing J2ME Applications in Linux
J2ME Game Programming Crash Course
J2ME Gaming Book
Getting Started with the MIDP2.0 Game API

The player image used in the example game is from LostGarden.

No comments:

Post a Comment