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
it(getWaitTime());
        catch (Exception e) {
        }
      }
      if (myShouldPause) {
        synchronized (this) {
          try {
            wait();
          catch (Exception e) {
          }
        }
      }
    }
  }

}

/**
 * This class contains the data for a game currently in progress. used to store
 * a game and to resume a stored game.
 
 @author Carol Hamer
 */

class GameInfo {

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

  /**
   * The name of the datastore.
   */
  public static final String STORE = "GameInfo";

  /**
   * This is set to true if an attempt is made to read a game when no game has
   * been saved.
   */
  private boolean myNoDataSaved;

  /**
   * The number that indicates which board the player is currently on.
   */
  private int myBoardNum;

  /**
   * The amount of time that has passed.
   */
  private int myTime;

  /**
   * The coordinates of where the player is on the board. coordinate values
   * must be between 0 and 15.
   */
  private int[] myPlayerSquare;

  /**
   * The coordinates of where the keys are currently found. MUST BE four sets
   * of two integer coordinates. coordinate values must be between 0 and 15.
   */
  private int[][] myKeyCoords;

  /**
   * The list of which doors are currently open. 0 = open 1 = closed WARNING:
   * this array MUST have length 8.
   */
  private int[] myDoorsOpen;

  /**
   * The number of the key that is currently being held by the player. if no
   * key is held, then the value is -1.
   */
  private int myHeldKey;

  //--------------------------------------------------------
  //  data gets/sets

  /**
   @return true if no saved game records were found.
   */
  boolean getIsEmpty() {
    return (myNoDataSaved);
  }

  /**
   @return The number that indicates which board the player is currently on.
   */
  int getBoardNum() {
    return (myBoardNum);
  }

  /**
   @return The number of the key that is currently being held by the player.
   *         if no key is held, then the value is -1.
   */
  int getHeldKey() {
    return (myHeldKey);
  }

  /**
   @return The amount of time that has passed.
   */
  int getTime() {
    return (myTime);
  }

  /**
   @return The coordinates of where the player is on the board. coordinate
   *         values must be between 0 and 15.
   */
  int[] getPlayerSquare() {
    return (myPlayerSquare);
  }

  /**
   @return The coordinates of where the keys are currently found. MUST BE
   *         four sets of two integer coordinates. coordinate values must be
   *         between 0 and 15.
   */
  int[][] getKeyCoords() {
    return (myKeyCoords);
  }

  /**
   @return The list of which doors are currently open. 0 = open 1 = closed
   *         WARNING: this array MUST have length 8.
   */
  int[] getDoorsOpen() {
    return (myDoorsOpen);
  }

  //--------------------------------------------------------
  //  constructors

  /**
   * This constructor records the game info of a game currently in progress.
   */
  GameInfo(int boardNum, int time, int[] playerSquare, int[][] keyCoords,
      int[] doorsOpen, int heldKeythrows Exception {
    myBoardNum = boardNum;
    myTime = time;
    myPlayerSquare = playerSquare;
    myKeyCoords = keyCoords;
    myDoorsOpen = doorsOpen;
    myHeldKey = heldKey;
    encodeInfo();
  }

  /**
   * This constructor reads the game configuration from memory. This is used
   * to reconstruct a saved game.
   */
  GameInfo() {
    RecordStore store = null;
    try {
      // if the record store does not yet exist, don't
      // create it
      store = RecordStore.openRecordStore(STORE, false);
      if ((store != null&& (store.getNumRecords() 0)) {
        // the first record has id number 1
        // it should also be the only record since this
        // particular game stores only one game.
        byte[] data = store.getRecord(1);
        myBoardNum = data[0];
        myPlayerSquare = DataConverter.decodeCoords(data[1]);
        myKeyCoords = new int[4][];
        myKeyCoords[0= DataConverter.decodeCoords(data[2]);
        myKeyCoords[1= DataConverter.decodeCoords(data[3]);
        myKeyCoords[2= DataConverter.decodeCoords(data[4]);
        myKeyCoords[3= DataConverter.decodeCoords(data[5]);
        myDoorsOpen = DataConverter.decode8(data[6]);
        myHeldKey = data[7];
        byte[] fourBytes = new byte[4];
        System.arraycopy(data, 8, fourBytes, 04);
        myTime = DataConverter.parseInt(fourBytes);
      else {
        myNoDataSaved = true;
      }
    catch (Exception e) {
      // this throws when the record store doesn't exist.
      // for that or any error, we assume no data is saved:
      myNoDataSaved = true;
    finally {
      try {
        if (store != null) {
          store.closeRecordStore();
        }
      catch (Exception e) {
        // if the record store is open this shouldn't throw.
      }
    }
  }

  //--------------------------------------------------------
  //  encoding method

  /**
   * Turn the data into a byte array and save it.
   */
  private void encodeInfo() throws Exception {
    RecordStore store = null;
    try {
      byte[] data = new byte[12];
      data[0(new Integer(myBoardNum)).byteValue();
      data[1= DataConverter.encodeCoords(myPlayerSquare);
      data[2= DataConverter.encodeCoords(myKeyCoords[0]);
      data[3= DataConverter.encodeCoords(myKeyCoords[1]);
      data[4= DataConverter.encodeCoords(myKeyCoords[2]);
      data[5= DataConverter.encodeCoords(myKeyCoords[3]);
      data[6= DataConverter.encode8(myDoorsOpen, 0);
      data[7(new Integer(myHeldKey)).byteValue();
      byte[] timeBytes = DataConverter.intToFourBytes(myTime);
      System.arraycopy(timeBytes, 0, data, 84);
      // if the record store does not yet exist, the second
      // arg "true" tells it to create.
      store = RecordStore.openRecordStore(STORE, true);
      int numRecords = store.getNumRecords();
      if (numRecords > 0) {
        store.setRecord(1, data, 0, data.length);
      else {
        store.addRecord(data, 0, data.length);
      }
    catch (Exception e) {
      throw (e);
    finally {
      try {
        if (store != null) {
          store.closeRecordStore();
        }
      catch (Exception e) {
        // if the record store is open this shouldn't throw.
      }
    }
  }

}

/**
 * This class contains the data

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

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

用户名: 查看更多评论

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

内 容:

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