Android Game with code for android freshers with and engine.
You can also download full code from fllowing link.
Click here First you have to configure andEngine lib files.then use following code
public class MainActivity extends BaseGameActivity implements
IOnSceneTouchListener{
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 780;
private static final int CAMERA_HEIGHT = 460;
// ===========================================================
// Fields
// ===========================================================
private BuildableTexture mBuildableTexture;
private TextureRegion mRedBlockTextureRegion;
private TextureRegion mBlueBlockTextureRegion;
private TextureRegion mBisopTextureRegion;
public Engine onLoadEngine() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final EngineOptions engineOptions = new EngineOptions(true,
ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), camera);
engineOptions.getTouchOptions().setRunOnUpdateThread(true);
return new Engine(engineOptions);
}
public void onLoadResources() {
mBuildableTexture = new BuildableTexture(1024, 1024,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
Texture mTexture = new Texture(1024, 1024,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
TextureRegionFactory.setAssetBasePath("gfx/");
mBlueBlockTextureRegion = TextureRegionFactory.createFromAsset(mBuildableTexture, this, "blueblock.png");
mRedBlockTextureRegion = TextureRegionFactory.createFromAsset(mBuildableTexture, this, "redblock.png");
mBisopTextureRegion = TextureRegionFactory.createFromAsset(mBuildableTexture, this, "bisop.png");
try {
mBuildableTexture.build(new BlackPawnTextureBuilder(1));
} catch (TextureSourcePackingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.getEngine().getTextureManager()
.loadTextures(this.mBuildableTexture, mTexture);
}
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene(2);
scene.setBackground(new ColorBackground(0, 0, 0));
scene.setOnSceneTouchListener(this);
return scene;
}
private void initLevel(int[][] level) {
int posx =0;
int posy = 50;
Sprite block = null;
for (int r = 0; r < level.length; r++) {
posx = 200;
for (int c = 0; c < level[r].length; c++) {
Log.d(r+" "+c,level[r][c]+"" );
if (level[r][c] == 1) {
block = new Sprite(posx, posy, mBlueBlockTextureRegion);
this.getEngine().getScene().getLastChild().attachChild(block);
}else if (level[r][c] == 0) {
block = new Sprite(posx, posy, mRedBlockTextureRegion);
this.getEngine().getScene().getLastChild().attachChild(block);
}
posx += block.getWidth();
}
posy += block.getHeight();
}
Sprite bisop = new Sprite(posx+(mBisopTextureRegion.getWidth()/4), posy+(mBisopTextureRegion.getHeight()/4), mBisopTextureRegion);
this.getEngine().getScene().getLastChild().attachChild(bisop);
}
public void onLoadComplete() {
int[][] level1 = { { 0, 1, 1, 1 },
{ 1, 0, 0, 1 },
{ 0, 1, 1, 1 },
{ 1, 1, 0, 1 }
};
initLevel(level1);
}
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
// TODO Auto-generated method stub
return false;
}
}