人生寫的第一個Android遊戲,名字爲炸彈專家,能夠在github上找到。https://github.com/zl-leaf/BombExpert。各位歡迎吐嘈。 java
這個遊戲使用的是AndEngine的庫,連接爲 https://github.com/nicolasgramlich/AndEngine,另外還帶有其餘的庫和Example。要用這個庫來寫遊戲,有第一件事情固然是要搞清楚怎麼用這些庫了。下面就簡單的說一下我對AndEngine提供的其中一個例子的理解。 android
首先用github下載代碼AndEngine和其餘庫以及Example,如圖 git
與AndEngine相關的庫 github
AndEngine的例子 ide
導入到Eclipse裏面,運行(建議用真機運行,由於虛擬機好像有些問題)。 ui
下面要說的代碼來自於AndEngineExamples中的一個例子,org.andengine.examples.game.racer裏面的RacerGameActivity。 this
代碼以下 spa
package org.andengine.examples.game.racer; import org.andengine.engine.camera.Camera; import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl; import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener; import org.andengine.engine.camera.hud.controls.BaseOnScreenControl; import org.andengine.engine.options.EngineOptions; import org.andengine.engine.options.ScreenOrientation; import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy; import org.andengine.entity.primitive.Rectangle; import org.andengine.entity.scene.Scene; import org.andengine.entity.scene.background.Background; import org.andengine.entity.sprite.Sprite; import org.andengine.entity.sprite.TiledSprite; import org.andengine.entity.util.FPSLogger; import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld; import org.andengine.extension.physics.box2d.PhysicsConnector; import org.andengine.extension.physics.box2d.PhysicsFactory; import org.andengine.extension.physics.box2d.PhysicsWorld; import org.andengine.extension.physics.box2d.util.Vector2Pool; import org.andengine.opengl.texture.TextureOptions; import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas; import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory; import org.andengine.opengl.texture.region.ITextureRegion; import org.andengine.opengl.texture.region.TiledTextureRegion; import org.andengine.opengl.vbo.VertexBufferObjectManager; import org.andengine.ui.activity.SimpleBaseGameActivity; import org.andengine.util.math.MathUtils; import android.opengl.GLES20; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; import com.badlogic.gdx.physics.box2d.FixtureDef; /** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga * * @author Nicolas Gramlich * @since 22:43:20 - 15.07.2010 */ public class RacerGameActivity extends SimpleBaseGameActivity { // =========================================================== // Constants // =========================================================== private static final int RACETRACK_WIDTH = 64; private static final int OBSTACLE_SIZE = 16; private static final int CAR_SIZE = 16; private static final int CAMERA_WIDTH = RACETRACK_WIDTH * 5; private static final int CAMERA_HEIGHT = RACETRACK_WIDTH * 3; // =========================================================== // Fields // =========================================================== private Camera mCamera; private BitmapTextureAtlas mVehiclesTexture; private TiledTextureRegion mVehiclesTextureRegion; private BitmapTextureAtlas mBoxTexture; private ITextureRegion mBoxTextureRegion; private BitmapTextureAtlas mRacetrackTexture; private ITextureRegion mRacetrackStraightTextureRegion; private ITextureRegion mRacetrackCurveTextureRegion; private BitmapTextureAtlas mOnScreenControlTexture; private ITextureRegion mOnScreenControlBaseTextureRegion; private ITextureRegion mOnScreenControlKnobTextureRegion; private Scene mScene; private PhysicsWorld mPhysicsWorld; private Body mCarBody; private TiledSprite mCar; // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public EngineOptions onCreateEngineOptions() { this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera); } @Override public void onCreateResources() { BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); this.mVehiclesTexture = new BitmapTextureAtlas(this.getTextureManager(), 128, 16, TextureOptions.BILINEAR); this.mVehiclesTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mVehiclesTexture, this, "vehicles.png", 0, 0, 6, 1); this.mVehiclesTexture.load(); this.mRacetrackTexture = new BitmapTextureAtlas(this.getTextureManager(), 128, 256, TextureOptions.REPEATING_NEAREST); this.mRacetrackStraightTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mRacetrackTexture, this, "racetrack_straight.png", 0, 0); this.mRacetrackCurveTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mRacetrackTexture, this, "racetrack_curve.png", 0, 128); this.mRacetrackTexture.load(); this.mOnScreenControlTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR); this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0); this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0); this.mOnScreenControlTexture.load(); this.mBoxTexture = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR); this.mBoxTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBoxTexture, this, "box.png", 0, 0); this.mBoxTexture.load(); } @Override public Scene onCreateScene() { this.mEngine.registerUpdateHandler(new FPSLogger()); this.mScene = new Scene(); this.mScene.setBackground(new Background(0, 0, 0)); this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1); this.initRacetrack(); this.initRacetrackBorders(); this.initCar(); this.initObstacles(); this.initOnScreenControls(); this.mScene.registerUpdateHandler(this.mPhysicsWorld); return this.mScene; } @Override public void onGameCreated() { } // =========================================================== // Methods // =========================================================== private void initOnScreenControls() { final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() { @Override public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) { final Body carBody = RacerGameActivity.this.mCarBody; final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5); carBody.setLinearVelocity(velocity); Vector2Pool.recycle(velocity); final float rotationInRad = (float)Math.atan2(-pValueX, pValueY); carBody.setTransform(carBody.getWorldCenter(), rotationInRad); RacerGameActivity.this.mCar.setRotation(MathUtils.radToDeg(rotationInRad)); } @Override public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) { /* Nothing. */ } }); analogOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); analogOnScreenControl.getControlBase().setAlpha(0.5f); // analogOnScreenControl.getControlBase().setScaleCenter(0, 128); // analogOnScreenControl.getControlBase().setScale(0.75f); // analogOnScreenControl.getControlKnob().setScale(0.75f); analogOnScreenControl.refreshControlKnobPosition(); this.mScene.setChildScene(analogOnScreenControl); } private void initCar() { this.mCar = new TiledSprite(20, 20, CAR_SIZE, CAR_SIZE, this.mVehiclesTextureRegion, this.getVertexBufferObjectManager()); this.mCar.setCurrentTileIndex(0); final FixtureDef carFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); this.mCarBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.mCar, BodyType.DynamicBody, carFixtureDef); this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this.mCar, this.mCarBody, true, false)); this.mScene.attachChild(this.mCar); } private void initObstacles() { this.addObstacle(CAMERA_WIDTH / 2, RACETRACK_WIDTH / 2); this.addObstacle(CAMERA_WIDTH / 2, RACETRACK_WIDTH / 2); this.addObstacle(CAMERA_WIDTH / 2, CAMERA_HEIGHT - RACETRACK_WIDTH / 2); this.addObstacle(CAMERA_WIDTH / 2, CAMERA_HEIGHT - RACETRACK_WIDTH / 2); } private void addObstacle(final float pX, final float pY) { final Sprite box = new Sprite(pX, pY, OBSTACLE_SIZE, OBSTACLE_SIZE, this.mBoxTextureRegion, this.getVertexBufferObjectManager()); final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0.1f, 0.5f, 0.5f); final Body boxBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, box, BodyType.DynamicBody, boxFixtureDef); boxBody.setLinearDamping(10); boxBody.setAngularDamping(10); this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(box, boxBody, true, true)); this.mScene.attachChild(box); } private void initRacetrack() { // Straights { final ITextureRegion racetrackHorizontalStraightTextureRegion = this.mRacetrackStraightTextureRegion.deepCopy(); racetrackHorizontalStraightTextureRegion.setTextureWidth(3 * this.mRacetrackStraightTextureRegion.getWidth()); final ITextureRegion racetrackVerticalStraightTextureRegion = this.mRacetrackStraightTextureRegion; // Top Straight this.mScene.attachChild(new Sprite(RACETRACK_WIDTH, 0, 3 * RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackHorizontalStraightTextureRegion, this.getVertexBufferObjectManager())); // Bottom Straight this.mScene.attachChild(new Sprite(RACETRACK_WIDTH, CAMERA_HEIGHT - RACETRACK_WIDTH, 3 * RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackHorizontalStraightTextureRegion, this.getVertexBufferObjectManager())); // Left Straight final Sprite leftVerticalStraight = new Sprite(0, RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackVerticalStraightTextureRegion, this.getVertexBufferObjectManager()); leftVerticalStraight.setRotation(90); this.mScene.attachChild(leftVerticalStraight); // Right Straight final Sprite rightVerticalStraight = new Sprite(CAMERA_WIDTH - RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackVerticalStraightTextureRegion, this.getVertexBufferObjectManager()); rightVerticalStraight.setRotation(90); this.mScene.attachChild(rightVerticalStraight); } // Edges { final ITextureRegion racetrackCurveTextureRegion = this.mRacetrackCurveTextureRegion; // Upper Left final Sprite upperLeftCurve = new Sprite(0, 0, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackCurveTextureRegion, this.getVertexBufferObjectManager()); upperLeftCurve.setRotation(90); this.mScene.attachChild(upperLeftCurve); // Upper Right final Sprite upperRightCurve = new Sprite(CAMERA_WIDTH - RACETRACK_WIDTH, 0, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackCurveTextureRegion, this.getVertexBufferObjectManager()); upperRightCurve.setRotation(180); this.mScene.attachChild(upperRightCurve); // Lower Right final Sprite lowerRightCurve = new Sprite(CAMERA_WIDTH - RACETRACK_WIDTH, CAMERA_HEIGHT - RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackCurveTextureRegion, this.getVertexBufferObjectManager()); lowerRightCurve.setRotation(270); this.mScene.attachChild(lowerRightCurve); // Lower Left final Sprite lowerLeftCurve = new Sprite(0, CAMERA_HEIGHT - RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackCurveTextureRegion, this.getVertexBufferObjectManager()); this.mScene.attachChild(lowerLeftCurve); } } private void initRacetrackBorders() { final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager(); final Rectangle bottomOuter = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager); final Rectangle topOuter = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager); final Rectangle leftOuter = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager); final Rectangle rightOuter = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager); final Rectangle bottomInner = new Rectangle(RACETRACK_WIDTH, CAMERA_HEIGHT - 2 - RACETRACK_WIDTH, CAMERA_WIDTH - 2 * RACETRACK_WIDTH, 2, vertexBufferObjectManager); final Rectangle topInner = new Rectangle(RACETRACK_WIDTH, RACETRACK_WIDTH, CAMERA_WIDTH - 2 * RACETRACK_WIDTH, 2, vertexBufferObjectManager); final Rectangle leftInner = new Rectangle(RACETRACK_WIDTH, RACETRACK_WIDTH, 2, CAMERA_HEIGHT - 2 * RACETRACK_WIDTH, vertexBufferObjectManager); final Rectangle rightInner = new Rectangle(CAMERA_WIDTH - 2 - RACETRACK_WIDTH, RACETRACK_WIDTH, 2, CAMERA_HEIGHT - 2 * RACETRACK_WIDTH, vertexBufferObjectManager); final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f); PhysicsFactory.createBoxBody(this.mPhysicsWorld, bottomOuter, BodyType.StaticBody, wallFixtureDef); PhysicsFactory.createBoxBody(this.mPhysicsWorld, topOuter, BodyType.StaticBody, wallFixtureDef); PhysicsFactory.createBoxBody(this.mPhysicsWorld, leftOuter, BodyType.StaticBody, wallFixtureDef); PhysicsFactory.createBoxBody(this.mPhysicsWorld, rightOuter, BodyType.StaticBody, wallFixtureDef); PhysicsFactory.createBoxBody(this.mPhysicsWorld, bottomInner, BodyType.StaticBody, wallFixtureDef); PhysicsFactory.createBoxBody(this.mPhysicsWorld, topInner, BodyType.StaticBody, wallFixtureDef); PhysicsFactory.createBoxBody(this.mPhysicsWorld, leftInner, BodyType.StaticBody, wallFixtureDef); PhysicsFactory.createBoxBody(this.mPhysicsWorld, rightInner, BodyType.StaticBody, wallFixtureDef); this.mScene.attachChild(bottomOuter); this.mScene.attachChild(topOuter); this.mScene.attachChild(leftOuter); this.mScene.attachChild(rightOuter); this.mScene.attachChild(bottomInner); this.mScene.attachChild(topInner); this.mScene.attachChild(leftInner); this.mScene.attachChild(rightInner); } // =========================================================== // Inner and Anonymous Classes // =========================================================== }這個類繼承了SimpleBaseGameActivity(由AndEngine提供),而後要重寫四個方法onCreateEngineOptions()、onCreateResources()、onCreateScene()、onGameCreated()。
onCreateEngineOptions()裏面要設置Engine的參數,基本照着寫就能夠了(若是要用到聲音的話要加一些東西,以後說)。 code
onCreateResources()是加載資源。首先說一下BitmapTextureAtlas和ITextureRegion。 orm
BitmapTextureAtlas,能夠理解成一塊畫布,ITextureRegion則是畫布上的圖形。
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); this.mVehiclesTexture = new BitmapTextureAtlas(this.getTextureManager(), 128, 16, TextureOptions.BILINEAR); this.mVehiclesTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mVehiclesTexture, this, "vehicles.png", 0, 0, 6, 1); this.mVehiclesTexture.load();
以這段代碼爲例。首先設定圖片資源的路徑是gfx(就是項目的assets/gfx)
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
新建一個寬高爲128X16的BitmapTextureAtlasm VehiclesTexture
this.mVehiclesTexture = new BitmapTextureAtlas(this.getTextureManager(), 128, 16, TextureOptions.BILINEAR);
在畫布VehiclesTexture上畫圖
this.mVehiclesTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mVehiclesTexture, this, "vehicles.png", 0, 0, 6, 1);vehicles.png圖片以下
,圖的大小爲96X16,而後畫布的大小必定要比圖片大並且是2的整數倍(不知道爲何),因此新建VehiclesTexture的大小爲128X16。
createTiledFromAsset裏面的參數我是這麼理解的,在VehiclesTexture上起點爲(0,0)處畫圖vehicles.png,該圖片能夠以一行6列的格式分紅6張小圖。
加載畫布和畫布上面的圖片
this.mVehiclesTexture.load();一樣道理加載其餘圖片資源。
onCreateScene(),創造遊戲的場景(Scene,一改能夠理解成是遊戲的容器或者背景吧)。
新建一個物理環境
this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);裏面的參數意思能夠本身查(由於有一兩個我不是很理解)
而後就是要往Sence裏面添加精靈(Sprite,能夠理解成是一個單元,就像遊戲中一我的物,一隻怪物)。
最後註冊物理環境
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
下面以initCar()和initOnScreenControls()裏面的代碼來講一下
新建一個TiledSprite mCar,初始位置爲(20,20),寬高爲CAR_SIZEXCAR_SIZE的精靈,選用mVehiclesTextureRegion做爲圖片。
this.mCar = new TiledSprite(20, 20, CAR_SIZE, CAR_SIZE, this.mVehiclesTextureRegion, this.getVertexBufferObjectManager());而且使用 mVehiclesTextureRegion中的第0個圖案。(還記得以前設定好mVehiclesTextureRegion包含1行6列的小圖不?如今用第0個)
this.mCar.setCurrentTileIndex(0);生成一個實體做爲車子(mCar)的身體
final FixtureDef carFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); this.mCarBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.mCar, BodyType.DynamicBody, carFixtureDef);把車子(mCar)和車身(mCarBody)綁定在一塊兒
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this.mCar, this.mCarBody, true, false));在場景(Scene)中添加這一車子
this.mScene.attachChild(this.mCar);這樣一個initCar()就完成了。
下面是initOnScreenControls(),控制按鈕的製做。
用mOnScreenControlBaseTextureRegion(操控按鈕方向的那張圖片)和mOnScreenControlKnobTextureRegion(那個圓圓的搖桿圖片)製做AnalogOnScreenControl analogOnScreenControl,位於畫面的(0, CAMERA_HEIGHT -this.mOnScreenControlBaseTextureRegion.getHeight())處。
覆寫onControlChange方法,當搖桿移動的時候車子運動。
final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() { @Override public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) { final Body carBody = RacerGameActivity.this.mCarBody; final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5); carBody.setLinearVelocity(velocity); Vector2Pool.recycle(velocity); final float rotationInRad = (float)Math.atan2(-pValueX, pValueY); carBody.setTransform(carBody.getWorldCenter(), rotationInRad); RacerGameActivity.this.mCar.setRotation(MathUtils.radToDeg(rotationInRad)); } @Override public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) { /* Nothing. */ } });
而後就是各類註冊,添加到Scene中
analogOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); analogOnScreenControl.getControlBase().setAlpha(0.5f); analogOnScreenControl.refreshControlKnobPosition(); this.mScene.setChildScene(analogOnScreenControl);這樣操控按鈕就完成了。
onGameCreated()是當全部加載渲染都搞定以後運行的方法,這裏沒有任何操做,因此空空的。
解說到此結束,這是我寫的第一篇這麼長的博文了,有些地方直接省略了,若是我說的有問題或者有疑問歡迎吐嘈一下