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
OutputStream baos = new ByteArrayOutputStream(4);
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeInt(i);
    baos.close();
    dos.close();
    byte[] retArray = baos.toByteArray();
    return (retArray);
  }

  //--------------------------------------------------------
  //  integer interpretation illustrated

  /**
   * Java appears to treat a byte as being signed when returning it as an
   * int--this function converts from the signed value to the corresponding
   * unsigned value. This method is used by nostreamParseInt.
   */
  public static int unsign(int signed) {
    int retVal = signed;
    if (retVal < 0) {
      retVal += 256;
    }
    return (retVal);
  }

  /**
   * Takes an array of bytes and returns an int. This version will return the
   * same value as the method parseInt above. This version is included in
   * order to illustrate how Java encodes int values in terms of bytes.
   
   @param data
   *            an array of 1, 2, or 4 bytes.
   */
  public static int nostreamParseInt(byte[] data) {
    // byte 0 is the high byte which is assumed
    // to be signed. As we add the lower bytes
    // one by one, we unsign them because because
    // a single byte alone is interpreted as signed,
    // but in an int only the top byte should be signed.
    // (note that the high byte is the first one in the array)
    int retVal = data[0];
    for (int i = 1; i < data.length; i++) {
      retVal = retVal << 8;
      retVal += unsign(data[i]);
    }
    return (retVal);
  }

  /**
   * Takes an arbitrary int and returns an array of four bytes. This version
   * will return the same byte array as the method intToFourBytes above. This
   * version is included in order to illustrate how Java encodes int values in
   * terms of bytes.
   */
  public static byte[] nostreamIntToFourBytes(int i) {
    byte[] fourBytes = new byte[4];
    // when you take the byte value of an int, it
    // only gives you the lowest byte. So we
    // get all four bytes by taking the lowest
    // byte four times and moving the whole int
    // down by one byte between each one.
    // (note that the high byte is the first one in the array)
    fourBytes[3(new Integer(i)).byteValue();
    i = i >> 8;
    fourBytes[2(new Integer(i)).byteValue();
    i = i >> 8;
    fourBytes[1(new Integer(i)).byteValue();
    i = i >> 8;
    fourBytes[0(new Integer(i)).byteValue();
    return (fourBytes);
  }

  /**
   * Takes an int between -32768 and 32767 and returns an array of two bytes.
   * This does not verify that the argument is of the right size. If the
   * absolute value of i is too high, it will not be encoded correctly.
   */
  public static byte[] nostreamIntToTwoBytes(int i) {
    byte[] twoBytes = new byte[2];
    // when you take the byte value of an int, it
    // only gives you the lowest byte. So we
    // get the lower two bytes by taking the lowest
    // byte twice and moving the whole int
    // down by one byte between each one.
    twoBytes[1(new Integer(i)).byteValue();
    i = i >> 8;
    twoBytes[0(new Integer(i)).byteValue();
    return (twoBytes);
  }

}
/**
 * This class contains the data for the map of the dungeon..
 
 @author Carol Hamer
 */

class BoardDecoder {

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

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

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

  /**
   * The coordinates of the doors. the there should be two in a row of each
   * color, following the same sequence as the keys.
   */
  private int[][] myDoors;

  /**
   * The coordinates of the Keys. the there should be of each color, following
   * the same sequence as the doors.
   */
  private int[][] myKeys;

  /**
   * The coordinates of the stone walls of the maze, encoded bit by bit.
   */
  private TiledLayer myLayer;

  /**
   * The data in bytes that gives the various boards. This was created using
   * EncodingUtils... This is a two-dimensional array: Each of the four main
   * sections corresponds to one of the four possible boards.
   */
  private static byte[][] myData = {
      00, -108, -100, -2465215853, -54, -116, -58, -56, -84,
          115, -118, -1, -1, -1281, -103, -15, -12825, -97, -127,
          -12879, -141, -126121, -1221, -113, -49, -1161,
          -100, -3, -1245, -25, -27, -1281, -1, -},
      0112290, -6234, -4372, -59, -2956, -5598126, -79,
          61, -1, -1, -1251, -12817, -2629, -3157, -721,
          -128, -51, -10065, -12457, -21, -12613, -1131,
          -9725, -127, -99, -81, -1, -},
      02108, -2418, -2610230, -5846, -28, -8834, -9897,
          -41, -1, -1, -961, -12657, -997, -12769, -11973,
          -1271, -10959, -1261, -26103, -12765, -103115,
          -12765, -2573, -1281, -1, -},
      03, -11418, -3427, -39, -60, -76, -501189082, -88,
          34, -74, -1, -1, -661, -128121, -26125, -128, -123,
          -10329, -1121, -10949, -1121, -116, -31, -1285,
          -1225, -3213, -127, -51, -1251, -1, -}};

  //--------------------------------------------------------
  //  initialization

  /**
   * Constructor fills data fields by interpreting the data bytes.
   */
  public BoardDecoder(int boardNumthrows Exception {
    // we start by selecting the two dimensional
    // array corresponding to the desired board:
    byte[] data = myData[boardNum];
    // The first two bytes give the version number and
    // the board number, but we ignore them because
    // they are assumed to be correct.
    // The third byte of the first array is the first one
    // we read: it gives the player's starting coordinates:
    myPlayerSquare = DataConverter.decodeCoords(data[2]);
    // the next byte gives the coordinates of the crown:
    myGoalSquare = DataConverter.decodeCoords(data[3]);
    // the next four bytes give the coordinates of the keys:
    myKeys = new int[4][];
    for (int i = 0; i < myKeys.length; i++) {
      myKeys[i= DataConverter.decodeCoords(data[i + 4]);
    }
    // the next eight bytes give the coordinates of the doors:
    myDoors = new int[8][];
    for (int i = 0; i < myDoors.length; i++) {
      myDoors[i= DataConverter.decodeCoords(data[i + 8]);
    }
    // now we create the TiledLayer object that i

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

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

用户名: 查看更多评论

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

内 容:

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