Saturday, May 28, 2011

Libgdx Android Example Game and Source Code

   I've written a bunch of small projects in Python and Actionscript 3 for desktop computers, but only created one small game in Java for smartphones. I've been itching to learn more Java and build a small Android app for my HTC Inspire that would improve on my simple J2ME avoider game. In a coffee-fueled frenzy late at night I quickly created this basic game using libgdx. It's not much, but I hope the source code is a useful example to another newbie.

   After looking at some of the other options available I decided to learn libgdx and use it for my "hello world" non-AIR Android project. I chose it because it offers the ability to write and test a game on a desktop computer and deploy it easily to an Android phone. I had some trouble at first, since I've gotten used to higher level frameworks lately, but SDL is also fairly low-level and my experience with it through Pygame was helpful in figuring things out.

   This project also gave me an excuse to finally setup Eclipse and the Android SDK on my desktop and get familiar with everybody's favorite bloated IDE. I had been using my slow laptop with Geany for coding, but Eclipse has a lot built in to make writing Java Android apps less painful, so I moved over to my fast desktop to use it.

   Hopefully the source code I've posted is useful for someone looking for an example of a small libgdx game that is less complicated than the games that come with the libgdx source, but more involved than the triangle drawing tutorial. The various tests in the source are a huge help to anyone who learns better from reading code, and I recommend checking those out next.

   The game I wrote is as simple as a touchscreen phone avoider-style game can be, so I thought it'd be useful to another newbie, as having very basic example source code helps me out when I'm learning a new library or framework. I'm not much of a programmer, not yet anyway, so don't laugh too much at the quality of the code. It runs, as the apk I've uploaded should prove, and that's good enough for me right now.

   The (ugly) source code
   The Android apk file

   I'm not a competent Java programmer (yet!) but libgdx is so simple even I can figure out how to get something playable on my phone, and it runs on my desktop as well. As soon as I get around to following the applet instructions it'll run in a web browser too. The framework takes care of cross-platform support, now all I have to do is improve my Java and write better games.

   To motivate myself to get better I've gone and paid the $25 to be able to publish to the Android Market. I've also given up trying to work my way through Essential Actionscript 3 and have instead gotten a copy of Beginning Android Games, which is written by the guy behind libgdx. Abandoning one thing to try another is something I do too often, but since my friends have Android phones, wanting to make something cool to show them will push me to improve my programming.


  1. Wow, this is great. Would you mind posting it in our contributions forum over at I think a lot of people could learn from this!

    Keep it up, good job!

  2. I want to try your code but could you provide the texture picture too. THX!

  3. You can easily swap out the 3 images with something else, but I'll upload a zip with them if I can find them.

  4. Here's a zip of the assets to go along with the code.