Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android飛機游戲敵機移動路徑

android飛機游戲敵機移動路徑

編輯:關於Android編程

基礎android的飛機類游戲,與前人一樣,由surfaceView繪制游戲畫面,另起線程控制繪制時間間隔達到動態效果。這裡附上最近自己寫的敵機自動飛行路徑代碼。請大家給點意見。

        在敵機管理模塊,加入此段代碼。movePingXing記錄該飛機直線軌跡運行時,每次canvas繪制的x、y的偏量值。moveYuanHu記錄該飛機按圓形軌跡運行時,每次canvas繪制的x、y的偏量值。String中,“、”前面得是x方向坐標偏移量,後面得是y方向坐標偏移量。

 

 

private static String[] movePingXing = { 5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 
            5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 
            5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 
            5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 
            5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0 }; 
    private static String[] moveYuanHu = { 5 + "," + 1, 5 + "," + 1, 
        4 + "," + 2, 4 + "," + 2,  
        3 + "," + 3,3 + "," + 3, 
            2 + "," + 4, 2 + "," + 4, 
            1 + "," + 5, 1 + "," + 5, 
            -1 + "," + 5,-1 + "," + 5, 
            -2 + "," + 4,-2 + "," + 4, 
            -3 + "," + 3, -3 + "," + 3, 
            -4 + "," + 2, -4 + "," + 2, 
            -5 + "," + 1, -5 + "," + 1, 
            -5 + "," + -1,-5 + "," + -1, 
            -4 + "," + -2,-4 + "," + -2, 
            -3 + "," + -3,-3 + "," + -3, 
            -2 + "," + -4,-2 + "," + -4, 
            -1 + "," + -5,-1 + "," + -5, 
            1 + "," + -5,1 + "," + -5, 
            2 + "," + -4,2 + "," + -4, 
            3 + "," + -3,3 + "," + -3, 
            4 + "," + -2,4 + "," + -2, 
            5 + "," + -1,5 + "," + -1}; 

private static String[] movePingXing = { 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,
   5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,
   5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,
   5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,
   5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0 };
 private static String[] moveYuanHu = { 5 + "," + 1, 5 + "," + 1,
  4 + "," + 2, 4 + "," + 2,
  3 + "," + 3,3 + "," + 3,
   2 + "," + 4, 2 + "," + 4,
   1 + "," + 5, 1 + "," + 5,
   -1 + "," + 5,-1 + "," + 5,
   -2 + "," + 4,-2 + "," + 4,
   -3 + "," + 3, -3 + "," + 3,
   -4 + "," + 2, -4 + "," + 2,
   -5 + "," + 1, -5 + "," + 1,
   -5 + "," + -1,-5 + "," + -1,
   -4 + "," + -2,-4 + "," + -2,
   -3 + "," + -3,-3 + "," + -3,
   -2 + "," + -4,-2 + "," + -4,
   -1 + "," + -5,-1 + "," + -5,
   1 + "," + -5,1 + "," + -5,
   2 + "," + -4,2 + "," + -4,
   3 + "," + -3,3 + "," + -3,
   4 + "," + -2,4 + "," + -2,
   5 + "," + -1,5 + "," + -1};
 

然後給出路徑添加方法,把這些坐標偏移量加入到moveList1。moveList1裡的內容一定要充足,必須保證在每次canvas繪制時,飛機都能得到一個有效路徑的String。否則會出現空指針異常。

 

 

public static boolean initMoveList1() { 
        addPingXing(); 
        addYuanHu(); 
        addPingXing(); 
        addPingXing(); 
        addPingXing(); 
        return true; 
    } 
    public static void addPingXing(){ 
        Map<String, String> map; 
        for (int i = 0; i < movePingXing.length; i++) { 
            map = new HashMap<String, String>(); 
            map.put("way", movePingXing[i]); 
            moveList1.add(map); 
        } 
    } 
     
    public static void addYuanHu(){ 
        Map<String, String> map; 
        for (int i = 0; i < moveYuanHu.length; i++) { 
            map = new HashMap<String, String>(); 
            map.put("way", moveYuanHu[i]); 
            moveList1.add(map); 
        } 
    } 

public static boolean initMoveList1() {
  addPingXing();
  addYuanHu();
  addPingXing();
  addPingXing();
  addPingXing();
  return true;
 }
 public static void addPingXing(){
  Map<String, String> map;
  for (int i = 0; i < movePingXing.length; i++) {
   map = new HashMap<String, String>();
   map.put("way", movePingXing[i]);
   moveList1.add(map);
  }
 }
 
 public static void addYuanHu(){
  Map<String, String> map;
  for (int i = 0; i < moveYuanHu.length; i++) {
   map = new HashMap<String, String>();
   map.put("way", moveYuanHu[i]);
   moveList1.add(map);
  }
 }
 

     調用initMoveList1()方法後,敵機管理類就可獲得一個記錄敵機飛行軌跡的偏移量的ArrayList了。

        在敵機移動的時候,插入下面代碼,實現每次繪制canvas時,讓敵機按自己設定的路徑動起來。我這裡設計時只是簡單的直線——圓行——直線飛機路徑。

 

 

Map<String, String> map= moveList1.get(enemy.getCurrentSecond()); 
                String moveWay = map.get("way"); 
                String[] zuobiao= moveWay.split(","); 
                enemy.x += Integer.parseInt(zuobiao[0]); 
                enemy.y += Integer.parseInt(zuobiao[1]); 

Map<String, String> map= moveList1.get(enemy.getCurrentSecond());
    String moveWay = map.get("way");
    String[] zuobiao= moveWay.split(",");
    enemy.x += Integer.parseInt(zuobiao[0]);
    enemy.y += Integer.parseInt(zuobiao[1]);

     上面currentSecond是一個int型變量,是敵機的屬性,記錄敵機在畫面中出現的時間。

        望高手給點意見,看有什麼地方能改進下。

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved