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
ssage and displays the message..
   */
  void errorMsg(Exception e) {
    if (e.getMessage() == null) {
      errorMsg(e.getClass().getName());
    else {
      errorMsg(e.getClass().getName() ":" + e.getMessage());
    }
  }

  /**
   * Displays an error message alert if something goes wrong.
   */
  void errorMsg(String msg) {
    Alert errorAlert = new Alert("error", msg, null, AlertType.ERROR);
    errorAlert.setCommandListener(this);
    errorAlert.setTimeout(Alert.FOREVER);
    Display.getDisplay(this).setCurrent(errorAlert);
  }

}

/**
 * This class represents doors and keys.
 
 @author Carol Hamer
 */

class DoorKey extends Sprite {

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

  /**
   * The image file shared by all doors and keys.
   */
  public static Image myImage;

  /**
   * A code int that indicates the door or key's color.
   */
  private int myColor;

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

  /**
   @return the door or key's color.
   */
  public int getColor() {
    return (myColor);
  }

  //---------------------------------------------------------
  //    constructor and initializer

  static {
    try {
      myImage = Image.createImage("/images/keys.png");
    catch (Exception e) {
      throw (new RuntimeException(
          "DoorKey.<init>-->failed to load image, caught "
              + e.getClass() ": " + e.getMessage()));
    }
  }

  /**
   * Standard constructor sets the image to the correct frame (according to
   * whether this is a door or a key and what color it should be) and then
   * puts it in the correct location.
   */
  public DoorKey(int color, boolean isKey, int[] gridCoordinates) {
    super(myImage, DungeonManager.SQUARE_WIDTH, DungeonManager.SQUARE_WIDTH);
    myColor = color;
    int imageIndex = color * 2;
    if (isKey) {
      imageIndex++;
    }
    setFrame(imageIndex);
    setPosition(gridCoordinates[0* DungeonManager.SQUARE_WIDTH,
        gridCoordinates[1* DungeonManager.SQUARE_WIDTH);
  }

}

/**
 * This class is a set of simple utility functions that can be used to convert
 * standard data types to bytes and back again. It is used especially for data
 * storage, but also for sending and receiving data.
 
 @author Carol Hamer
 */

class DataConverter {

  //--------------------------------------------------------
  //  utilities to encode small, compactly-stored small ints.

  /**
   * Encodes a coordinate pair into a byte.
   
   @param coordPair
   *            a pair of integers to be compacted into a single byte for
   *            storage. WARNING: each of the two values MUST BE between 0 and
   *            15 (inclusive). This method does not verify the length of the
   *            array (which must be 2!) nor does it verify that the ints are
   *            of the right size.
   */
  public static byte encodeCoords(int[] coordPair) {
    // get the byte value of the first coordinate:
    byte retVal = (new Integer(coordPair[0])).byteValue();
    // move the first coordinate's value up to the top
    // half of the storage byte:
    retVal = (new Integer(retVal << 4)).byteValue();
    // store the second coordinate in the lower half
    // of the byte:
    retVal += (new Integer(coordPair[1])).byteValue();
    return (retVal);
  }

  /**
   * Encodes eight ints into a byte. This could be easily modified to encode
   * eight booleans.
   
   @param eight
   *            an array of at least eight ints. WARNING: all values must be 0
   *            or 1! This method does not verify that the values are in the
   *            correct range nor does it verify that the array is long
   *            enough.
   @param offset
   *            the index in the array eight to start reading data from.
   *            (should usually be 0)
   */
  public static byte encode8(int[] eight, int offset) {
    // get the byte value of the first int:
    byte retVal = (new Integer(eight[offset])).byteValue();
    // progressively move the data up one bit in the
    // storage byte and then record the next int in
    // the lowest spot in the storage byte:
    for (int i = offset + 1; i < + offset; i++) {
      retVal = (new Integer(retVal << 1)).byteValue();
      retVal += (new Integer(eight[i])).byteValue();
    }
    return (retVal);
  }

  //--------------------------------------------------------
  //  utilities to decode small, compactly-stored small ints.

  /**
   * Turns a byte into a pair of coordinates.
   */
  public static int[] decodeCoords(byte coordByte) {
    int[] retArray = new int[2];
    // we perform a bitwise and with the value 15
    // in order to just get the bits of the lower
    // half of the byte:
    retArray[1= coordByte & 15;
    // To get the bits of the upper half of the
    // byte, we perform a shift to move them down:
    retArray[0= coordByte >> 4;
    // bytes in Java are generally assumed to be
    // signed, but in this coding algorithm we
    // would like to treat them as unsigned:
    if (retArray[00) {
      retArray[0+= 16;
    }
    return (retArray);
  }

  /**
   * Turns a byte into eight ints.
   */
  public static int[] decode8(byte data) {
    int[] retArray = new int[8];
    // The flag allows us to look at each bit individually
    // to determine if it is 1 or 0. The number 128
    // corresponds to the highest bit of a byte, so we
    // start with that one.
    int flag = 128;
    // We use a loop that checks
    // the data bit by bit by performing a bitwise
    // and (&) between the data byte and a flag:
    for (int i = 0; i < 8; i++) {
      if ((flag & data!= 0) {
        retArray[i1;
      else {
        retArray[i0;
      }
      // move the flag down one bit so that we can
      // check the next bit of data on the next pass
      // through the loop:
      flag = flag >> 1;
    }
    return (retArray);
  }

  //--------------------------------------------------------
  //  standard integer interpretation

  /**
   * Uses an input stream to convert an array of bytes to an int.
   */
  public static int parseInt(byte[] datathrows IOException {
    DataInputStream stream = new DataInputStream(new ByteArrayInputStream(
        data));
    int retVal = stream.readInt();
    stream.close();
    return (retVal);
  }

  /**
   * Uses an output stream to convert an int to four bytes.
   */
  public static byte[] intToFourBytes(int ithrows IOException {
    ByteArray

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

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

用户名: 查看更多评论

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

内 容:

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