My First Android Game

First let's cut to the chase - here's the game on the Android Market:
Caterpillar Game.
It should run on any device running Android 1.6 (Donut) or later.

I first wrote this game about 20 years ago in Turbo C for DOS. Later I ported it to Java Swing. A few months ago I decided I wanted to learn more about Android, and one good way to do that is to write a simple program.

I got started with a book you can read about on the Reading List on my LinkedIn page. This book explained some key stuff:

  • Brief History of Android: what it is and where it came from.
  • Where to get the Android SDK and how to install it (I installed it on my Ubuntu Linux system).
  • Basic intro with examples for GUI forms, 2-D graphics, filesystem, sounds, touch screen, accelerometer, etc.
  • Android / Dalvik JVM limitations and common workarounds.
  • I plan to put the game on the Android Market, but first I want to get some beta testing. I've run it in the emulator and on my Acer A100 tablet. If you try this game, please let me know how it works (or doesn't work!), and what device and version of Android you're running it on.

    Here's an overview of the stuff in this game, areas or features of Android that it uses:

  • GUI screens: XML based GUI screens using standard Android layouts
  • Threads: the game runs on its own thread, separate from the main Android UI thread
  • Fonts: the game uses a custom font, Ubuntu (freely available)
  • Timers: the game runs in real time independent of the device CPU clock
  • Resources: sounds (MP3 files) and bitmap graphics
  • Activities: multiple Activities that trigger each other
  • Filesystem: it creates its own directory and data file to save high scores
  • Touch input: it uses the touch screen (both touch & swipe) to control the caterpillars
  • Accelerometer: it uses the accelerometer to control the caterpillar
  • Device detection: it detects the screen size and sizes/scales the game accordingly
  • Limitations, bugs and etc.

    My daughter made the game icon and we recorded the sound effects together.

    Every 15 seconds the game plays a quick melody (that I played on my fife) and speeds up by 10%.

    Accelerometer controls are "experimental". I've discovered that the accelerometer axis are different on tablets and phones, and I did all my testing on my tablet, so it may not work properly on phones.

    Relative controls are right-left only, from the caterpillar's frame of reference. Thus if the caterpillar is moving downward, a swipe (or touch) to the left will make him move right, which is HIS left.

    Absolute controls move in the direction of the swipe (or touch) regardless of the caterpillar's orientation.

    Swipe controls move the caterpiller in the direction of the swipe. The swipe can be anywhere on the screen.

    Touch controls split the screen into equal quadrants (or halves for relative controls). A touch in any quadrant (or half) is moves in that direction.

    Game setup will let more than one caterpillar be human controlled, but the game only allows one caterpillar to be human controlled. Any others set to human control will simply go in a straight line until they crash into something.

    You can play with no human controlled caterpillars. This can be interesting to watch the computer play against itself.

    The computer controlled caterpillars always run for the apples and use a recursive lookahead algorithm to figure out where it is safe to move. The maximum lookahead is 30 moves, and each move has 3 possible directions (left, right, straight), so that is worst-case 3^30 = 2e14 moves to analyze. If the game pauses when on maximum intelligence, this is why. This doesn't happen often because it quits as soon as it finds a safe path, so it doesn't always analyze this many moves. These pauses shouldn't cause a problem since the pause is on the game thread, not the main Android thread. To eliminate this pause, slide the intelligence down a notch or two. Each notch reduces the maximum pause by a factor of 3.