AndEngine is the really cool android game engine developed by Nicolas Gramlich. This post gives the steps needed to create a simple android game using AndEngine. Please look at my previous post “Getting started with AndEngine” for details of downloading and configuring the AndEngine in your Eclipse environment.
Fortunately AndEngine comes with a lot of examples which are a good starting point for creating of a game. After you installed AndEngine on your phone do give the examples a try and understand their behavior. You should then be able to suitably mix & match different components for the game you need.
In my case as a start I wanted to develop a simple Pong game with a paddle and an animated sprite for the ball. So I checked out the following examples
- Drawing a Sprite – SpriteExample.java
- Removing a Sprite – SpriteRemoveExample.java
- Drawing Animated Sprites – AnimatedSpriteExample.java
- A Moving ball example – MovingBallExample.java
- Analog On Screen Control – AnalogOnScreenControlExample.java
- Collision Detection – CollisionDetectionExample.java
Once I was fairly familiar with the above examples I started by creating an Android Project from Eclipse. I next copied the entire contents of AnalogOnScreenControlExample .java to the /src folder in a file named Pong.java. I changed the package details and also the class name from AnalogOnScreenControlExample to Pong.
Once this is done you have to do the following steps which is very important
- Click Project->Properties->Java Compiler and chose “Enable project specific setting” and select 1.6
- Click Project->Properties->Android and select Android 4.2
- Click Project-> Properties->Android and under Library click the Add button and select AndEngine as a library.
Managing a paddle with the AnalogOnScreenController
Since I wanted to move a Pong paddle instead of the sprite in the above example I downloaded a jpg file for the paddle and copied it to
/assets/gfx
You must also copy the onscreen_control_base.png and onscreen_control_knob.png to /assets/gfx folder.
Build and run you program by connecting your phone through a USB cable. You should see the on screen control and the paddle. For my game I did not need the rotary control so I removed it and only kept the control for handling the velocity of my paddle.
Once you have your basic code working you can add the other parts. For my game I needed the following
- Animated Sprite
- A moving animated sprite
- Collision detection of the sprite with the paddle
Animated Sprite: To create an animated sprite you have to create a tiled picture with slight variations of the image. I downloaded a jpg of a brontosaurus and used GIMP to tile the picture with 5 tiles. For this in GIMP choose Filters->Map-> and choose %. Unlink the Width & Height and set the Width to 500% and height to 100%. This will create 5 vertical adjacent tiles. Then I applied transform->shear to each individual tile so that in effect it will look like an animated dino.
One this png is created you will have to copy it to assets/gfx folders and use in onCreateResources()
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 160, TextureOptions.BILINEAR);
this.mBrontTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, “bront1_tiled.png”, 0, 0, 1, 5); //
this.mBitmapTextureAtlas.load();
This typically is animated as follows
bront = new AnimatedSprite(pX, pY, this.mBrontTextureRegion, this.getVertexBufferObjectManager());
bront.animate(200);
Creating a moving animated Sprite : For this I picked up the code from the MovingBallExample.java as follows and replaced the ball sprite with my bront sprite
final Bront bront = new Bront(cX, cY, this.mBrontTextureRegion, this.getVertexBufferObjectManager());
bront.registerUpdateHandler(physicsHandler);
scene.attachChild(bront);
….
privatestaticclass Bront extends AnimatedSprite {
public Bront(finalfloat pX, finalfloat pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
this.animate(100);
…
Create a moving sprite For this I picked up the appropriate code from the MovingBallExample.java and massaged it a bit to handle my animated bront sprite
privatestaticclass Bront extends AnimatedSprite {
privatefinal PhysicsHandler mPhysicsHandler;
public Bront(finalfloat pX, finalfloat pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
….
this.mPhysicsHandler = new PhysicsHandler(this);
this.registerUpdateHandler(this.mPhysicsHandler);
this.mPhysicsHandler.setVelocity(DEMO_VELOCITY, DEMO_VELOCITY);
}
@Override
protectedvoid onManagedUpdate(finalfloat pSecondsElapsed) {
if(this.mX < 0) {
this.mPhysicsHandler.setVelocityX(DEMO_VELOCITY);
} elseif(this.mX + this.getWidth() > CAMERA_WIDTH) {
this.mPhysicsHandler.setVelocityX(-DEMO_VELOCITY);
}
if(this.mY < 0) {
this.mPhysicsHandler.setVelocityY(DEMO_VELOCITY);
} elseif(this.mY + this.getHeight() > CAMERA_HEIGHT) {
this.mPhysicsHandler.setVelocityY(-DEMO_VELOCITY);
}
…
Handling collisions: To handle the collisions the code in CollisionDetectionExample.java comes handy. So the paddle which is controlled by the onScreenAnalogControl will detect collisions with the animated sprite as below
and reverses the velocity component on collision detection
@Override
protectedvoid onManagedUpdate(finalfloat pSecondsElapsed) {
….
if(paddle.collidesWith(this)){
float vx = this.mPhysicsHandler.getVelocityX();
float vy = this.mPhysicsHandler.getVelocityY();
this.mPhysicsHandler.setVelocity(-vx,-vy);
}
super.onManagedUpdate(pSecondsElapsed);
So thats about all. We have a basic pong game ready! The game definitely needs more enhancements which I propose to do in the coming days. Watch this space!
Checkout the video clip of the Pong game in action.
You can download the code from Pong.zip
Find me on Google+
One thought on “Creating a simple android game using AndEngine”