WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术程序开发一个J2ME地牢游戏的源码

一个J2ME地牢游戏的源码
作者:J2MEDEV  来源:J2MEDEV  发布时间:2006-7-25 9:06:59
 for the map of the dungeon. This is a utility
 * class that allows a developer to write the data for a board in a simple
 * format, then this class encodes the data in a format that the game can use.
 
 * note that the data that this class encodes is hard-coded. that is because
 * this class is intended to be used only a few times to encode the data. Once
 * the board data has been encoded, it never needs to be encoded again. The
 * encoding methods used in this class could be generalized to be used to create
 * a board editor which would allow a user to easily create new boards, but that
 * is an exercise for another day...
 
 @author Carol Hamer
 */

class EncodingUtils {

  //--------------------------------------------------------
  //  fields

  /**
   * data for which squares are filled and which are blank. 0 = empty 1 =
   * filled
   */
  private int[][] mySquares = {
      111111111111111},
      100000110000000},
      100000000001000},
      111001100001110},
      111000010011100},
      101110000000000},
      100000001100110},
      100111000100000},
      100001000011100},
      111111100000000},
      100000100000110},
      100011110000000},
      100111110001100},
      100000011001110},
      111110000000000},
      111111111111111}};

  /**
   * The coordinates of where the player starts on the map in terms of the
   * array indices.
   */
  private int[] myPlayerSquare = 710 };

  /**
   * The coordinates of the goal (crown).
   */
  private int[] myGoalSquare = 510 };

  //--------------------------------------------------------
  //  get/set data

  /**
   * Creates the array of door sprites. (call this only once to avoid creating
   * redundant sprites).
   */
  int[][] getDoorCoords() {
    int[][] retArray = new int[8][];
    for (int i = 0; i < retArray.length; i++) {
      retArray[inew int[2];
    }
    // red
    retArray[0][012;
    retArray[0][15;
    retArray[1][014;
    retArray[1][13;
    // green
    retArray[2][03;
    retArray[2][18;
    retArray[3][012;
    retArray[3][19;
    // blue
    retArray[4][06;
    retArray[4][12;
    retArray[5][07;
    retArray[5][114;
    // yellow
    retArray[6][011;
    retArray[6][11;
    retArray[7][03;
    retArray[7][113;
    return (retArray);
  }

  /**
   * Creates the array of key sprites. (call this only once to avoid creating
   * redundant sprites.)
   */
  int[][] getKeyCoords() {
    int[][] retArray = new int[4][];
    for (int i = 0; i < retArray.length; i++) {
      retArray[inew int[2];
    }
    // red
    retArray[0][012;
    retArray[0][12;
    // green
    retArray[1][02;
    retArray[1][12;
    // blue
    retArray[2][013;
    retArray[2][15;
    // yellow
    retArray[3][04;
    retArray[3][18;
    return (retArray);
  }

  //--------------------------------------------------------
  //  encoding / decoding utilities

  /**
   * Encodes the entire dungeon.
   */
  byte[][] encodeDungeon() {
    byte[][] retArray = new byte[2][];
    retArray[0new byte[16];
    // the first byte is the version number:
    retArray[0][00;
    // the second byte is the board number:
    retArray[0][10;
    // the player's start square:
    retArray[0][2= DataConverter.encodeCoords(myPlayerSquare);
    // the goal (crown) square:
    retArray[0][3= DataConverter.encodeCoords(myGoalSquare);
    //encode the keys:
    int[][] keyCoords = getKeyCoords();
    for (int i = 0; i < keyCoords.length; i++) {
      retArray[0][i + 4= DataConverter.encodeCoords(keyCoords[i]);
    }
    //encode the doors:
    int[][] doorCoords = getDoorCoords();
    for (int i = 0; i < doorCoords.length; i++) {
      retArray[0][i + 8= DataConverter.encodeCoords(doorCoords[i]);
    }
    //encode the maze:
    try {
      retArray[1= encodeDungeon(mySquares);
    catch (Exception e) {
      e.printStackTrace();
    }
    return (retArray);
  }

  /**
   * Takes a dungeon given in terms of an array of 1s and 0s and turns it into
   * an array of bytes. WARNING: the array MUST BE 16 X 16.
   */
  static byte[] encodeDungeon(int[][] dungeonMapthrows Exception {
    if ((dungeonMap.length != 16|| (dungeonMap[0].length != 16)) {
      throw (new Exception(
          "EncodingUtils.encodeDungeon-->must be 16x16!!!"));
    }
    byte[] retArray = new byte[32];
    for (int i = 0; i < 16; i++) {
      retArray[* i= DataConverter.encode8(dungeonMap[i]0);
      retArray[* i + 1= DataConverter.encode8(dungeonMap[i]8);
    }
    return (retArray);
  }

  //--------------------------------------------------------
  //  main prints the bytes to standard out.
  // (note that this class is not intended to be run as a MIDlet)

  /**
   * Prints the byte version of the board to standard out.
   */
  public static void main(String[] args) {
    try {
      EncodingUtils map = new EncodingUtils();
      byte[][] data = map.encodeDungeon();
      System.out.println("EncodingUtils.main-->dungeon encoded");
      System.out.print("{\n   " + data[0][0]);
      for (int i = 1; i < data[0].length; i++) {
        System.out.print(", " + data[0][i]);
      }
      for (int i = 1; i < data[1].length; i++) {
        System.out.print(", " + data[1][i]);
      }
      System.out.println("\n};");
    catch (Exception e) {
      e.printStackTrace();
    }
  }

}
/**
 * This class handles the graphics objects.
 
 @author Carol Hamer
 */

class DungeonManager extends LayerManager {

  //---------------------------------------------------------
  //   dimension fields
  //  (constant after initialization)

  /**
   * The x-coordinate of the place on the game canvas where the LayerManager
   * window should appear, in terms of the coordiantes of the ga

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码