Android Game using AndEngine

January 12th, 2012 by admin | Filed under Android Game Programming, Android Tutorials.

We will learn how to develop a game using AndEngine which is a openSource, free game engine :) for android.
Step :1. Download the AndEngine from here :

AndEngine.jar

Step :2. Create a new project using motodev.
andEngine
Step :3. Create a new folder ‘lib’ and paste AndEngine.jar file there.
lib_folder

Paste the AndEngine.jar file
inside_lib_folder

Step :4. Go to project properyies and add AndEngine.jar file in the build path.
java build path

Step :5. Open MainActivity.java file and extend it to the BaseActivity

Step :6 You will get an error thrown. Now import the default methods of AndEngine.
extend to the base activity of AndEnine
Step :7. Your Main activity will look like this. These four are the default method of AndEngine

a)onLoadEngine()
a)onLoadResources()
a)onLoadScene()
a)onLoadComplete()

package com.andenginedemo;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class MainActivity  extends BaseGameActivity {

@Override
public Engine onLoadEngine() {
// TODO Auto-generated method stub
return null;
}

@Override
public void onLoadResources() {
// TODO Auto-generated method stub

}

@Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
return null;
}

@Override
public void onLoadComplete() {
// TODO Auto-generated method stub

}
/** Called when the activity is first created. */

}

Step :7. Open the assets folder and create a folder gfx and put the ball.png file there.

Step :8. Now put following code in your MainActivity.java class

package com.andenginedemo;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;

import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;

public class MainActivity  extends BaseGameActivity {

private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;

private Camera _refCamera;
private Texture _ballBitmapTexture;
private TextureRegion _ballTextureRegion;

@Override
public Engine onLoadEngine() {
this._refCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this._refCamera));
}

@Override
public void onLoadResources() {
this._ballBitmapTexture =  new Texture(256,256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this._ballTextureRegion= TextureRegionFactory.createFromAsset(this._ballBitmapTexture, this, "gfx/ball.png", 0, 0); // 64x32
this.mEngine.getTextureManager().loadTexture(this._ballBitmapTexture);
}

@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
Scene scene = new Scene(2);
scene.setBackground(new ColorBackground(0.0000f, 0.0000f, 0.0000f));
/* Calculate the coordinates for the face, so its centered on the camera. */
final int centerX = (CAMERA_WIDTH - this._ballTextureRegion.getWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this._ballTextureRegion.getHeight()) / 2;

/* Create the face and add it to the scene. */
final Sprite face = new Sprite(centerX, centerY, this._ballTextureRegion);
scene.getTopLayer().addEntity(face);
return scene;
}

@Override
public void onLoadComplete() {

}

}

Step :9. Run your app it will look like this. Enjoy :)
final
downloadSource

Tags: , , , , , , , , , , , , , , , , ,

One Response to “Android Game using AndEngine”

  1. Gaynelle Siegal says:

    Can you provide the source code ?

Leave a Reply