Saturday, December 29, 2012

The making of Dino Pong android game


DSC00016Dino Pong is my first android game from concept to completion. It is based on the android game engine AndEngine. This post gives the main hightights in the making of this fairly simple but interesting game. The game is now available on Google Play and can be downloaded at Dino Pong.
Do take a look at my earlier post “Creating a simple android game using AndEngine” to understand how the basic game can be setup.
For the Dino Pong game I wanted the following
  1. 3 animated sprites that bounced off walls and moved with different velocities and paddle
  2. A DigitalOnScreenController that controls the paddle
  3. Collision detection between the paddle and the sprites and between the sprites themselves
  4. Points awarded for hitting a sprite with a paddle and points deducted for misses at the point of contact
  5. A game board showing hits, misses and the total score
So I created 3 animated sprites. Take a look at my earlier post on how to create an animated dino. So in the onCreateResources the 3 animated sprites and the paddle are created as below
Animated Sprites and paddle
// Create a ball
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 32, TextureOptions.BILINEAR);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 0, 2, 1);
this.mBitmapTextureAtlas.load();
// Create a bront
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 160, 64, TextureOptions.BILINEAR);
this.mBrontTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "bront2_tiled.png", 0, 0, 5, 1); //
this.mBitmapTextureAtlas.load();
// Create a paddle
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTOnCextureManager(), 90, 30, TextureOptions.BILINEAR);
this.mPaddleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "paddle1.png", 0, 0);
this.mBitmapTextureAtlas.load();
// Create a Box face
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
this.mBoxFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0, 2, 1); // 64x32
this.mBitmapTextureAtlas.load();
In the onCreateScene the animated sprites and the paddle are added to the scene and attached to it as below
// Add ball to scene
finalfloat Y = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
ball = new Ball(X, Y, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
scene.attachChild(ball);
// Add box to scene
finalfloat X1 = (CAMERA_WIDTH - this.mBoxFaceTextureRegion.getWidth()) / 2;
finalfloat Y1 = 270;
box = new Box(X1, Y1, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager());
scene.attachChild(box);
// Add paddle
finalfloat centerX = (CAMERA_WIDTH - this.mPaddleTextureRegion.getWidth()) / 2;
float centerY = 320;
paddle = new Sprite(centerX, centerY, this.mPaddleTextureRegion, this.getVertexBufferObjectManager());
final PhysicsHandler physicsHandler = new PhysicsHandler(paddle);
paddle.registerUpdateHandler(physicsHandler);
scene.attachChild(paddle);
// Create a shaking brontosaurus
finalfloat cX = (CAMERA_WIDTH - this.mBrontTextureRegion.getWidth())/2;
finalfloat cY = 50;
bront = new Bront(cX, cY, this.mBrontTextureRegion, this.getVertexBufferObjectManager());
bront.registerUpdateHandler(physicsHandler);
scene.attachChild(bront);
The paddle is registered with a physicsHandler. All the animated instances all register with the physicsHandler to be able to detect collisions.
DigitalOnScreenController for controlling paddle : For this game I have used a DigitalOnScreenController as opposed to the analog version. The digital controller seems to have a smoother movement and diagonal movements are disabled. The code for this taken from AndEngine examples.
// Add a digital on screen control
this.mDigitalOnScreenControl = new DigitalOnScreenControl(50, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight() + 20, this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IOnScreenControlListener() {
@Override
publicvoid onControlChange(final BaseOnScreenControl pBaseOnScreenControl, finalfloat pValueX, finalfloat pValueY) {
physicsHandler.setVelocity(pValueX * 100, 0);
}
});
this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
this.mDigitalOnScreenControl.refreshControlKnobPosition();
scene.setChildScene(this.mDigitalOnScreenControl);
One of the thing I did was to disable vertical movements of the controlled object the paddle. Hence the physicsHandler sets the y value to '0' as shown above
publicvoid onControlChange(final BaseOnScreenControl pBaseOnScreenControl, finalfloat pValueX, finalfloat pValueY) {
physicsHandler.setVelocity(pValueX * 100, 0);
}
Handling collisions :
As I mentioned above all the animated sprites (brontosaurus, face_circle & face_box) register with physics handler when the object is instantiated
privatestaticclass Bront extends AnimatedSprite {
privatefinal PhysicsHandler mPhysicsHandler;
floatx,y;
public Bront(finalfloat pX, finalfloat pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
this.animate(100);
this.mPhysicsHandler = new PhysicsHandler(this);
this.registerUpdateHandler(this.mPhysicsHandler);
// Change the angle to the horizontal
this.mPhysicsHandler.setVelocity(BRONT_VELOCITY, BRONT_VELOCITY);
}
If the paddle misses the sprite then when the sprite collides with the bottom wall a point is deducted
if(this.mY < 0) {
this.mPhysicsHandler.setVelocityY(BRONT_VELOCITY);
//bText.setText("");
} elseif(this.mY + this.getHeight() + 80 > CAMERA_HEIGHT) {
x = this.getX();
y = this.getY();
bText.setPosition(x-10,y + 20);
bText.setText("-1");
misses = misses - 1;
score = score -1;
missesText.setText("Misses: "+ misses);
scoreText.setText("Score: " + score);
Also the sprite is restarted from the top at the same 'x' coordinate
// At bottom. Restart from the top
this.setPosition(x, 0);
this.mPhysicsHandler.setVelocityY(-BRONT_VELOCITY);
The collision with the paddle, face_circle & face_box are checked here
if(paddle.collidesWith(this) || this.collidesWith(paddle)){
x = this.getX();
y = this.getY();
bText.setPosition(x+10,y+10);
bText.setText("+1");
hits = hits + 1;
score = score + 1;
float vx = this.mPhysicsHandler.getVelocityX();
float vy = this.mPhysicsHandler.getVelocityY();
this.mPhysicsHandler.setVelocity(-vx,-vy);
}
if(ball.collidesWith(this)){
float vx = this.mPhysicsHandler.getVelocityX();
float vy = this.mPhysicsHandler.getVelocityY();
this.mPhysicsHandler.setVelocity(-vx,-vy);
}
Similarly the collision checks are done for the other 2 sprites.
When the paddle successfuly hits a sprite the points are awarded at the point of contact
if(paddle.collidesWith(this) || this.collidesWith(paddle)){
x = this.getX();
y = this.getY();
bText.setPosition(x-10,y + 20);
bText.setText("-1");
The score is updated simulataneously for each hit or miss
hitsText.setText("Hits: "+ hits);
scoreText.setText("Score: " + score);
hits = hits + 1;
score = score + 1;
Additional tweaks
  1. The size of the DigitalOnScreenController was shrunk by half as it seemed oversized for my Android phone
  2. A box is drawn within which the sprites can bounce off allowing space for the score at the bottom

final Line line1 = new Line(0, 0, 320, 0, 5, this.getVertexBufferObjectManager());
final Line line2 = new Line(320, 0, 320, 400, 5, this.getVertexBufferObjectManager());
final Line line3 = new Line(320, 400, 0, 400, 5, this.getVertexBufferObjectManager());
final Line line4 = new Line(0, 400, 0, 0, 5, this.getVertexBufferObjectManager());
// Add bounded rectangle to scene
scene.attachChild(line1);
scene.attachChild(line2);
scene.attachChild(line3);
scene.attachChild(line4);
  1. The velocities of the 3 sprites are made slightly different
  2. The x & y components of the velocity of the face_circle and face_box differ to enable a slightly different angle of motion.
A video clip of Dino Pong in action can be seen here – Dino Pong clip
You can clone the entire project at Git Hub  Dino Pong game
The complete code can be downloaded at DinoPong.zip
Issues: One of the issues I see is that when the paddle hits the middle of any sprite then the sprite appears to get locked and does not bounce off. Sometimes 2 sprites also get into this 'deadly embrace' before getting themselves released. It appears that successive collisions happen before the velocity and position can be changed hence resulting in this lock up. Any ideas on fixing this are welcome.
Do let me know your thoughts on this game.

Wednesday, December 26, 2012

Creating a simple android game using AndEngine


IMG_8928AndEngine 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
  1. Drawing a Sprite – SpriteExample.java
  2. Removing a Sprite – SpriteRemoveExample.java
  3. Drawing Animated Sprites – AnimatedSpriteExample.java
  4. A Moving ball example – MovingBallExample.java
  5. Analog On Screen Control – AnalogOnScreenControlExample.java
  6. 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
  1. Click Project->Properties->Java Compiler and chose “Enable project specific setting” and select 1.6
  2. Click Project->Properties->Android and select Android 4.2
  3. Click Project Properties 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
  1. Animated Sprite
  2. A moving animated sprite
  3. 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