Android原生遊戲開發:使用JustWeEngine開發微信打飛機

使用JustWeEngine開發微信打飛機:

做者博客: 博客園
引擎地址:JustWeEngine
示例代碼:EngineDemojava

JustWeEngine?

JustWeEngine是託管在Github 的一個開源的Android原生開發框架,能夠讓Android的開發人員
很是便捷,無需切換語言和編譯器的製做Android原生遊戲。git

使用方法

  • 引入Engine做爲Library進行使用。
  • 引入"/jar"文件夾下的jar包。
  • 使用Gradle構建:
    • Step 1. Add the JitPack repository to your build file
      Add it in your root build.gradle at the end of repositories:
    allprojects {
              repositories {
                  ...
                  maven { url "https://jitpack.io" }
              }
          }
    • Step 2. Add the dependency
    dependencies {
              compile 'com.github.lfkdsk:JustWeEngine:v1.01'
        }
  • 使用Maven構建:
    • Step 1. Add the JitPack repository to your build file
    <repositories>
          <repository>
              <id>jitpack.io</id>
              <url>https://jitpack.io</url>
          </repository>
      </repositories>
    • Step 2. Add the dependency
    <dependency>
          <groupId>com.github.lfkdsk</groupId>
          <artifactId>JustWeEngine</artifactId>
          <version>v1.01</version>
      </dependency>

1.Activity繼承基類:

因爲框架所有使用SurfaceView進行繪製,不使用諸如Button、Layout等原生控件,因此應該首先新建類繼承引擎核心類Engine,負責遊戲的流程,註釋中已有明確的標明功能。github

public class Game extends Engine {
    // 通常在構造函數完成變量的初始化
    public Game() {
        // 控制debug模式是否開始,debug模式打印日誌、幀數、pause次數等信息
        super(true);
        
    }
    
    // 載入一些UI參數和設置屏幕放向,默認背景色,設定屏幕的掃描方式等
    @Override
    public void init() {
        // 初始化UI默認參數,必須調用該方法,其中有一些用於多機型適配的參數須要初始化
        UIdefaultData.init(this);
    }

    // 一般用於精靈,背景,圖片等物體的設置和載入
    @Override
    public void load() {

    }

    // draw 和 update 在線程中進行不斷的循環刷新
    // draw 負責繪製 update 負責更新數據和對象信息
    @Override
    public void draw() {

    }

    @Override
    public void update() {

    }
    
    // 將touch 事件傳回 功能和所設定的屏幕掃描方式有關
    @Override
    public void touch(MotionEvent event) {

    }
    
    // 將碰撞事件傳回 傳回精靈和物品的基類 
    // 用於處理碰撞事件 默認使用矩形碰撞
    @Override
    public void collision(BaseSub baseSub) {

    }
    }

2.添加圖片、文字、對象:

2.1 添加文字:

printer = new GameTextPrinter();
    printer.setTextColor(Color.BLACK);
    printer.setTextSize(24);
    printer.setLineSpaceing(28);

初始化文字繪製printer,並設置顏色尺寸和圖片。canvas

@Override
public void draw() {
    canvas = super.getCanvas();
    printer.setCanvas(canvas);
    printer.drawText("Engine demo", 10, 20);
}

在draw中便可繪製。微信

2.2 使用圖片:

首先添加一張大的圖片,須要的敵軍戰機,和可控的飛機都在這張圖上。框架

GameTexture texture1 = new GameTexture(this);
    texture1.loadFromAsset("pic/shoot.png");

而且初始化敵人的圖片和子彈的圖片:dom

shoot = new GameTexture(this);
    shoot.loadFromAsset("pic/flare.png");

    enemyPic = new GameTexture(this);
    enemyPic.loadFromAsset("pic/enemy.png");

2.3 對象:

咱們要新建一個BaseSprite對象並設置一些基礎屬性。maven

BaseSprite ship;
    // 新建對象,每張幀圖片寬100高124
    ship = new BaseSprite(this, 100, 124, FrameType.COMMON);
    // 從剛纔的那張圖片取出幀
    ship.setTexture(texture1);
    // 添加兩幀 參數是座標
    ship.addRectFrame(0, 100, 100, 124);
    ship.addRectFrame(167, 361, 100, 124);
    // 添加動畫 在兩幀之間不斷切換
    ship.addAnimation(new FrameAnimation(0, 1, 1));
    // 設定位置
    ship.setPosition(UIdefaultData.centerInHorizontalX - ship.getWidthWithScale() / 2,
            UIdefaultData.screenHeight + ship.getHeightWidthScale());
    // 設定拉伸後的大小
    ship.setDipScale(96, 96);
    // 添加固定動畫 即小飛機出場
    ship.addfixedAnimation("start",
            new MoveAnimation(UIdefaultData.centerInHorizontalX - ship.getWidthWithScale() / 2,
                    UIdefaultData.screenHeight - 2 * ship.getHeightWidthScale(), new Float2(10, 10)));
    // 設定名稱和類別 對象分組
    ship.setName("SHIP");
    ship.setIdentifier(SHIP);
    // 添加到精靈組中
    addToSpriteGroup(ship);

2.4添加背景:

GameTexture tex = new GameTexture(this);
    if (!tex.loadFromAsset("pic/background.png")) {
        fatalError("Error loading space");
    }
    backGround2X = Bitmap.createBitmap(
            UIdefaultData.screenWidth,
            UIdefaultData.screenHeight * 2,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(backGround2X);
    Rect dst = new Rect(0, 0, UIdefaultData.screenWidth - 1,
            UIdefaultData.screenHeight);
    canvas.drawBitmap(tex.getBitmap(), null, dst, null);
    dst = new Rect(0, UIdefaultData.screenHeight,
            UIdefaultData.screenWidth,
            UIdefaultData.screenHeight * 2);
    canvas.drawBitmap(tex.getBitmap(), null, dst, null);
            bg_rect = new Rect(0, 0, UIdefaultData.screenWidth,         UIdefaultData.screenHeight);
    bg_scroll = new Point(0, 0);

這裏獲取了一張背景圖片,而且建立了一張Bitmap包含兩份的背景圖片,這是爲了讓背景滾動起來。ide

2.5生成子彈、敵人:

public void fireBullet() {
    if (!shoottimer.stopWatch(300)) return;
    BaseSprite bullet;
    if (getTypeSizeFromRecycleGroup(BULLET) > 0) {
        bullet = (BaseSprite) recycleSubFromGroup(BULLET);
        bullet.clearAllAnimation();
        removeFromRecycleGroup(bullet);
    } else {
        bullet = new BaseSprite(this);
        bullet.setTexture(shoot);
        bullet.setDipScale(8, 18);
        bullet.setIdentifier(BULLET);
    }
    double angle = 270.0;
    float speed = 20.0f;
    int lifetime = 2500;
    bullet.addAnimation(new V elocityAnimation(angle, speed,
            lifetime));
    bullet.setPosition(ship.s_position.x +
                    ship.getWidthWithScale() / 2
                    - bullet.getWidthWithScale() / 2,
            ship.s_position.y - 24);
    bullet.setAlive(true);
    addToSpriteGroup(bullet);
}

private void addEnemy() {
    BaseSprite enemy;
    if (getTypeSizeFromRecycleGroup(ENEMY) > 0) {
        enemy = (BaseSprite) recycleSubFromGroup(ENEMY);
        enemy.clearAllAnimation();
        removeFromRecycleGroup(enemy);
    } else {
        enemy = new BaseSprite(this);
        enemy.setTexture(enemyPic);
        enemy.setIdentifier(ENEMY);
        enemy.setDipScale(49, 36);
    }
    double angle = 90.0;
    float speed = 5.0f;
    int lifetime = 5000;
    enemy.addAnimation(new VelocityAnimation(angle, speed,
            lifetime));
    enemy.setPosition(random.nextInt(UIdefaultData.screenWidth),
            -enemy.getWidthWithScale());
    enemy.setAlive(true);
    addToSpriteGroup(enemy);
    enemyNum++;
}

2.6進行繪製:

更新draw 和 update的方法進行繪製:函數

@Override
public void draw() {
    canvas = super.getCanvas();
    canvas.drawBitmap(backGround2X, bg_rect, bg_rect, paint);
    printer.setCanvas(canvas);
    printer.drawText("Engine demo", 10, 20);
}

@Override
public void update() {
    // 
    if (timer.stopWatch(20)) {
        scrollBackground();
    }
    // 進行入場動畫
    if (ship.getFixedAnimation("start").animating) {
        ship.fixedAnimation("start");
    } else {
    // 射擊和新建敵人
        fireBullet();
//      int size = getTypeSizeFromRecycleGroup(ENEMY);
//      if (size > 0)
//      enemyNum -= size;
        if (enemyTimer.stopWatch(200)) {
            addEnemy();
        }
    }
}

2.7碰撞監測和touch事件:

@Override
public void touch(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            offsetX = event.getX() - startX;
            offsetY = event.getY() - startY;
            if (Math.abs(offsetX) > Math.abs(offsetY)) {
                if (ship.s_position.x + offsetX > 0
                        && ship.s_position.x + offsetX +
                        ship.getHeightWidthScale() < UIdefaultData.screenWidth) {
                    ship.s_position.x += offsetX;
                    resetEvent(event);
                }
            } else {
                if (ship.s_position.y + offsetY > 0
                        && ship.s_position.y + offsetY +
                        ship.getHeightWidthScale() < UIdefaultData.screenHeight) {
                    ship.s_position.y += offsetY;
                    resetEvent(event);
                }
            }
            break;
        case MotionEvent.ACTION_DOWN:
            startX = (int) event.getX();
            startY = (int) event.getY();
            break;
    }
}

@Override
public void collision(BaseSub baseSub) {
    BaseSprite other = (BaseSprite) baseSub.getOffender();
    if (baseSub.getIdentifier() == BULLET &&
            other.getIdentifier() == ENEMY) {
        other.setAlive(false);
        removeFromSpriteGroup(other);
        addToRecycleGroup(other);
        enemyNum--;
    }
}

2.8滾動背景:

private void resetEvent(MotionEvent event) {
    startX = (int) event.getX();
    startY = (int) event.getY();
}

public void scrollBackground() {
    bg_scroll.y += 10.0f;
    bg_rect.top = bg_scroll.y;
    bg_rect.bottom = bg_rect.top + UIdefaultData.screenHeight - 1;
    if (bg_scroll.y + bg_rect.height() > backGround2X.getHeight()) {
        bg_scroll.y = bg_scroll.y - bg_rect.height();
    }
}
相關文章
相關標籤/搜索