Get the latest Education e-news
  • Excerpt: Android Game Elements and Tools

    - Richard A. Rogers
  •  [In this excerpt from Learning Android Game Programming, author Richard A. Rogers presents some basic and essential tips for making your own game on Android devices.]

    From the overview in the last chapter, it's obvious that creating a good mobile game involves writing some rather complex software, and also requires the creation of other components, such as graphics, animation, sound effects, and music. To be able to offer your game for others to play, you need to have commercial rights to all of this intellectual property, and the easiest way to get the rights is to create the game all yourself.

    Fortunately, tools are available for all of these components, and many of them are available for free. If you've got an Internet connection and a development machine, you've got access to just about all you need.

    The example game used in this book, Virgins Versus Vampires, was written using the Java programming language, which runs in the Dalvik virtual machine on Android devices. The game makes use of an open-source game engine called AndEngine and a physics engine called Box2D, both of which have been ported to Android. The game and all of its intellectual property were created using freely available software development tools, graphics tools, and audio. All of these items are described in this chapter, which also offers our first exposure to actually writing some code-specifically, a splash screen for the game.

    It's important to realize that the games we are writing are normal Android applications. They are written in Dalvik/Java, with full access to the Android application programming interfaces (APIs), and their activities have all the characteristics that we expect of Android activities (e.g., pause, resume). Our games will link to the AndEngine library, and each will include a copy of the library in the Android application package file (.apk) for the game.

    Software Development Tools

    We need software development tools to write software. As luck would have it, excellent tools are available for writing mobile software in general and games in particular. Even luckier for us, many of these tools are free to download and use, even if we're creating a game we plan to sell.

    Android Software Development Kit

    If you are not already familiar with the Android SDK, stop right here and take the time needed to become familiar with it. The first step is to go through the download and installation instructions at the following website:

    The Android SDK uses Eclipse for its integrated development environment (IDE) and tools that come with Oracle's Java Development Kit ( JDK). The installation instructions on the Android website will lead you through their installation (if that step is required). As of this book's writing, the SDK is componentized. The examples and figures you see in the book were all built using the following versions of its components:

    • Android SDK
    • Android SDK Platform Components through 4.0
    • Android SDK Tools, r14
    • ADT Plugins for Eclipse 14.0.0
    • Eclipse Helios (the version recommended for this version of the SDK)
    • Oracle/Sun Java Development Kit ( JDK 6, also called JDK 1.6)

    You may be using later versions than the ones identified here, but the example code is relatively independent of version. If something doesn't work, take a look at the book's companion website at and see if updates have been published for later versions. As this is being written, the current version of Android is 4.0, also known as Ice Cream Sandwich.

    You should also have created Android Virtual Devices (AVDs) for each type of device you intend to support, using the Android SDK and AVD Manager that comes with the SDK. For the examples shown in the book, we created an AVD that is a lot like the HTC EVO smartphone:

    • Name: EVO
    • Target: Android 2.2 (API level 8)
    • Skin: HVGA
    • SD Card: 128M
    • SdCard: yes
    • Accelerometer: yes
    • LCD.density: 160
    • AudioOutput: yes
    • Camera: no (not supported on this version of the emulator, and not needed)
    • Battery: yes

    You should have also at least gone through the tutorials that are provided for the SDK and be familiar with the processes of creating Android projects, editing code, building projects, running them on the Android emulator, and debugging projects using the Eclipse debugger, LogCat, and the other tools provided in the SDK. If you plan to publish your game to Android Market, you need to make sure it runs well on actual phones, so you should also have some experience loading and running .apk files on an Android phone.

    The documentation on the Android developer site is excellent and very thorough. If you need a gentler introduction or more examples, many excellent Android programming books are also available, including Sam's Teach Yourself Android Application Development in 24 Hours by Lauren Darcey and Shane Conder.

    AndEngine Game Engine Library

    AndEngine is a game engine library that makes it easier to write two-dimensional games for Android devices. Nicolas Gramlich led the effort to create AndEngine and wrote much of its code. The project is open source, so you are encouraged to go to the project website and join in the development effort for AndEngine yourself.

    We could, of course, write our own routines in Java, using the Android APIs to implement the components of a game. Nevertheless, there are good reasons for leveraging a game engine that is already written:

    - We leverage the work of others. As an extreme example, we could also write our own IDE for Android if we really wanted to, or even our own Java compiler, but unless we really need some special functionality, doing so makes no sense.

    - With open-source code, like that used in AndEngine, we always have the option of extending the engine functionality any way we like. If the extension might be useful to others, we can contribute the changes back into the open-source repository and improve the engine for everyone.

    - When we run into problems, we can access a community of developers using the same technology. It's likely that someone else has already dealt with our problem and knows how to solve or work around it.

    - We get the benefit of many developers' optimizations. Games use a fair amount of computer resources to draw graphics, animate the graphics, compute object physics, render sounds, and keep up with the user input. By using a game engine, we have ready access to optimizations that have already been tuned.

    Other game engines are currently under development for Android, but this book will focus on AndEngine. Important websites for AndEngine are listed here:

    These locations may have changed by the time you read this book. If they have, just use your browser to search for "AndEngine Android," and you should be able to find the current locations.

    AndEngine comes as a .jar file-that is, a Java archive. You'll see how to use that archive with your Android SDK project when we start our game coding later in this chapter. The engine is provided under the GNU Lesser GPL License, which allows you to use the source code and link to the binaries for just about any reasonable purpose. (Note: I am not a lawyer-you or your lawyer can read the text of the license in the file that is referenced on the repository website.)

    AndEngine Game Concepts

    The movie analogy we referred to earlier is a good way to approach AndEngine. Your game is like a movie, and the game engine includes concepts that are analogous to those involved in making a movie.


    The "camera" of the game determines the view of the game that is presented to players. It is very much like a movie camera in two-dimensional space. The camera can pan and zoom across the scene to change the view presented. The panning and zooming can either be under the player's control or be driven programmatically.


    A game, like a movie, consists of a series of scenes where the action takes place. In a movie, the scenes are edited together in a fixed way. In games, the sequence of scenes is driven by the play of the game. Games are like movies edited on the fly.


    Scenes are composed of layers of graphics. The layers are superimposed on one another, much like the animation cels used to create cartoons in the old days. Layers can also be used to introduce 2½D effects, where, as the camera pans, closer layers move faster than more distant layers.


    Sprites are the visual representation of the actors in our movie, whether those actors are people or objects. Sprites can be animated or not, but they often move about the scene during the course of game play. Sprite textures are often loaded from one large image that comprises a collection of sprite images, called a sprite sheet.


    In AndEngine, entities are just about anything that's drawn to the screen. Sprites are entities, as are tiles, geometric shapes, and lines drawn on the screen. All entities have properties, such as color, rotation, scale and position, that can be changed by modifiers.


    Modifiers change the properties of an entity, and they are very powerful in AndEngine. They can be used on any entity, and the change they cause can either be immediate or occur gradually over a specified duration. In our game, we'll use modifiers frequently to create effects with sprites and other entities.


    A texture is a 2D, generally bitmapped graphic that can be applied to objects to give them, well, texture. Textures define the way entities look, and much of the OpenGL graphics environment is built around the use of textures.

    Texture Region

    A texture defines a complete bitmap graphic, and a texture region defines a subset of that region. We'll talk a lot about performance optimizations of 2D graphics later, and using texture regions to map small pieces of a large combined bitmap is one of the key tricks used to create these optimizations.


    An engine runs a scene. It takes care of letting animations and modifiers know when to update the presented graphics, coordinates the actual drawing, handles user input events (touch, keys, sensors), and generally manages the progress of the game. The engine is a lot like the producer/director of our movie, telling everyone what they need to do.


    This class, which extends the Android Activity class, will be the basis of each scene in our game. BaseGameActivity does all the work common to all scenes, setting up the game engine, conforming to the Android Activity Lifecycle requirements, and enabling sensors. We'll explore this class in more depth in Chapter 3.

    Physics Connector

    AndEngine includes basic physics capabilities in the base engine, but the Box2D physics engine expands greatly on those capabilities. We connect AndEngine objects with Box2D through a physics connector. If your game doesn't use Box2D physics, you won't have a physics connector.

    Box2D Physics Engine

    AndEngine includes the open-source JBox2D port of the Box2D physics engine. It can be used to realistically simulate the interaction of physical objects in the following ways (among others):

    • Simulation of the physics of rigid bodies
    • Stable stacking
    • Gravity
    • User-defined units
    • Efficient solving for collisions/contacts
    • Sliding friction
    • Boxes, circles, and polygons
    • Several joint types: distance, revolute, prismatic, pulley, gear, mouse
    • Sleeping (removes motionless bodies from simulation until touched)


comments powered by Disqus