A busy dev’s guide
Guest post: First steps into Java games on Android
You may be pretty familiar when it comes to working with Java, but making a game on the Android platform is a whole different kettle of fish. Here’s a thoroughly unscientific exploration of Android game programming by John Purcell.
Recently I set to work to figure out the basics of game programming and animation in Android. After spending the best part of fourteen years as a software developer, I now make online programming video tutorials for a living. A few months ago I created an Android course and I had finally got round to the bit that I knew lots of my clients were waiting for; games and animation.
The only problem was that I've never created a game in Android in my life. Like most software developers, I've created the odd game or two for my own amusement, so I'm familiar with the basics of game development; I'd just never created a game for Android.
One thing I've learned over the years is that when you start to put together a game, it's best not to reinvent the wheel. Use a free established game library if you can. For instance, if you're writing games in Flash, you might use Flixel; in Python you could use Pygame and so on. Using a game library can save you from having to do stuff like implementing your own sprite animation system or your own collision detection code and lots more.
The situation with regard to Android turned out to be just a little more complex than I'd first imagined. Now, what I'm about to tell you isn't a comprehensive survey of the situation with regard to Android programming and games, or anywhere near it; it's just the impressions of a seasoned software developer who's new to this particular lark.
libgdx: A Cross-Platform Game Library
The first library I looked at was libgdx, a cross-platform Java game programming library. Libgdx is pretty popular, free, and allows you to write games on your desktop that can then be easily transferred to Android. You link your program with one set of jars for Android and another for Java desktop, for instance. By clever use of linked projects in Eclipse (cleverer than I'm frankly accustomed to), you can work on your code in one place and quickly build it for both platforms.
This all sounded great at first. It didn't feel like the absolute easiest thing in the world to dig up example code to work with, but after a few hours I managed to put together a sample application. The idea is that you divide up your program into files that correspond to different game states among other things. Libgdx is pretty nice; considering it's free, you can't complain. The point where I (perhaps foolishly) threw up my hands in disgust and dropped the whole idea was when I realised that libgdx doesn't appear to have any built-in 2D collision detection at the moment.
I know I'm probably being a fool, and some fellow or other has probably written an add-on library, but since I've been spoilt by Flixel, this came as quite a shock to the system. I immediately started looking around for other alternatives.
The next API I tried was AndEngine. AndEngine has a great reputation, and people seem to be saying that if you're serious about programming games in Android, it's worth persisting with. I totally don't doubt it. For me, however, it proved a challenge. AndEngine seems to be in the process of migrating from one version of its API to another, and it's often hard to tell which examples are meant for which (at least for me anyway, but even while I type these words I have visions of hordes of software developers and teenage coders with brains the sizes of planets rising up en masse and pouring scorn on my lack of software savvy).
Neither could I find anywhere where I could download a single jar for AndEngine. Once again I'm open to correction, but it seems there isn't one. The correct way to proceed with AndEngine is, I believe, to create a library project in Eclipse. I somehow managed to create a jar, which I need for psychological reasons having to do with early childhood trauma, but I just couldn't get any example of AndEngine to actually compile, much less run.
This is entirely my fault; I only gave the matter a few hours of attention before giving up. That's the kind of person I am. If you're a serious Android game developer, no doubt you'd have it all figured out pretty quickly. I think AndEngine looks great, but just don't expect it to immediately dance to your tune the second you look at it. You're going to need a little patience. It's in a state of constant development, which in many ways is great of course, and hats off to the developer, who seems to be a brainy-looking guy by the name of Nicolas Gramlich.
The Android SDK Game Examples
At this point I gave up on Android game libraries. I didn't want to fork out money for a commercial product; I hadn't the patience to set up AndEngine and, for the simple games I wanted to write, libgdx didn't seem to me to offer clear advantages over writing my own code (although I see that it is capable of doing many things that I haven't explored). So I turned in desperation to the examples included with the Android SDK itself.
These are a mixed bunch. To be fair, I only tried two; Lunar Lander and JetBoy. I couldn't get either to actually work on my phone, an HTC Desire C. They did run, but I couldn't figure out the controls.
After examining the source code, I concluded that my phone was probably supposed to have a keyboard, and doesn't. They also seemed to crash if I tried to switch away from them to another application and then go back again.
Aside from these niggles, these two applications, together with a lot of Googling, did allow me to eventually figure out how to put together my own simple game programming framework, complete with sound and 2D frame-by-frame animation; enough to satisfy me, and hopefully, my course subscribers.
In case you're interested, here are the main things that I figured out. And I might mention at this stage that, yes, it probably would have been easier to use libgdx - but then I wouldn't have had the fun of working with the Android API directly.
Firstly, you can extend a class called SurfaceView and then put your extended SurfaceView component in your layout. Surface view has a method called getHolder() which allows you to return a SurfaceHolder object. You can do two really useful things with this.
One is that you can use addCallback() to add a listener which will be notified when the drawing surface is created or destroyed; paying careful attention to this will enable you to stop and start your drawing code when the user starts or navigates away from your application. Oh, I didn't mention this yet, but you're going to have to set up a separate thread to draw to the surface every few milliseconds and update your game. This isn't handled for you. Just use an ordinary Thread class with a loop in it.
The second thing you can do with your SurfaceHolder is to lock it using a method called lockCanvas(). This allows you to retrieve a canvas object which you can draw on. Don't forget to unlock it, and don't forget that lockCanvas() won't always succeed; you need to check that the returned canvas is not null. Since the drawing surface is locked before you draw on it, as far as I can see, you don't need any further thread synchronization, which is nice.
You'll need to spend a bit of time initially figuring when to stop and start your drawing thread (hint: use the surface callback) and stuff like that, but once you've got the basics nailed, the rest is just standard game programming. Of course, if you want to do anything more complex that draw 2D images, you'd be well-advised to use a game programming library instead of trying to roll your own.
Playing sounds is very straightforward, mercifully. Use the SoundPool class to load and play sounds in mp3 or some other format.
The easiest way to create simple animated games in Android is probably to use libgdx, which has the additional benefit of being cross-platform. If you're really serious about game programming, you'll probably want to use AndEngine, or even make use of a proprietrary product like Unity3D. If you want to roll your own, make use of the SurfaceView class (Google for it!), create your own thread to hold your game loop, and use SoundPool to play sounds.
About the author: John W. Purcell worked for a variety of global and not-so-global corporations in the UK and Holland for the best part of fourteen years before moving to Budapest and starting the site Cave of Programming, from where he now makes a living by creating online video programming courses. His latest fledgling project is an ill-conceived and frankly somewhat insane attempt to convince everyone that the world is merely a projection of our collective minds and can be found at QuantumLifetime.com.