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
s the
    // background dungeon map:
    myLayer = new TiledLayer(1616,
        Image.createImage("/images/stone.png"),
        DungeonManager.SQUARE_WIDTH, DungeonManager.SQUARE_WIDTH);
    // now we call an internal utility that reads the array
    // of data that gives the positions of the blocks in the
    // walls of this dungeon:
    decodeDungeon(data, myLayer, 16);
  }

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

  /**
   @return the number of boards currently stored in this class.
   */
  public static int getNumBoards() {
    return (myData.length);
  }

  /**
   * get the coordinates of where the player starts on the map in terms of the
   * array indices.
   */
  public int[] getPlayerSquare() {
    return (myPlayerSquare);
  }

  /**
   * get the coordinates of the goal crown in terms of the array indices.
   */
  public int[] getGoalSquare() {
    return (myGoalSquare);
  }

  /**
   * get the tiled layer that gives the map of the dungeon.
   */
  public TiledLayer getLayer() {
    return (myLayer);
  }

  /**
   * Creates the array of door sprites. (call this only once to avoid creating
   * redundant sprites).
   */
  DoorKey[] createDoors() {
    DoorKey[] retArray = new DoorKey[8];
    for (int i = 0; i < 4; i++) {
      retArray[* inew DoorKey(i, false, myDoors[* i]);
      retArray[* i + 1new DoorKey(i, false, myDoors[* i + 1]);
    }
    return (retArray);
  }

  /**
   * Creates the array of key sprites. (call this only once to avoid creating
   * redundant sprites.)
   */
  DoorKey[] createKeys() {
    DoorKey[] retArray = new DoorKey[4];
    for (int i = 0; i < 4; i++) {
      retArray[inew DoorKey(i, true, myKeys[i]);
    }
    return (retArray);
  }

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

  /**
   * Takes a dungeon given as a byte array and uses it to set the tiles of a
   * tiled layer.
   
   * The TiledLayer in this case is a 16 x 16 grid in which each square can be
   * either blank (value of 0) or can be filled with a stone block (value of
   * 1). Therefore each square requires only one bit of information. Each byte
   * of data in the array called "data" records the frame indices of eight
   * squares in the grid.
   */
  private static void decodeDungeon(byte[] data, TiledLayer dungeon,
      int offsetthrows Exception {
    if (data.length + offset < 32) {
      throw (new Exception(
          "BoardDecoder.decodeDungeon-->not enough data!!!"));
    }
    // a frame index of zero indicates a blank square
    // (this is always true in a TiledLayer).
    // This TiledLayer has only one possible (non-blank)
    // frame, so a frame index of 1 indicates a stone block
    int frame = 0;
    // Each of the 32 bytes in the data array records
    // the frame indices of eight block in the 16 x 16
    // grid. Two bytes give one row of the dungeon,
    // so we have the array index go from zero to 16
    // to set the frame indices fro each of the 16 rows.
    for (int i = 0; i < 16; i++) {
      // 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;
      // Here we check two bytes at the same time
      // (the two bytes together correspond to one row
      // of the dungeon). We use a loop that checks
      // the bytes bit by bit by performing a bitwise
      // and (&) between the data byte and a flag:
      for (int j = 0; j < 8; j++) {
        if ((data[offset + * i& flag!= 0) {
          frame = 1;
        else {
          frame = 0;
        }
        dungeon.setCell(j, i, frame);
        if ((data[offset + * i + 1& flag!= 0) {
          frame = 1;
        else {
          frame = 0;
        }
        dungeon.setCell(j + 8, i, frame);
        // 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;
      }
    }
  }

}

/**
 * This class contains the loop that keeps the game running.
 
 @author Carol Hamer
 */

class GameThread extends Thread {

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

  /**
   * Whether or not the main thread would like this thread to pause.
   */
  private boolean myShouldPause;

  /**
   * Whether or not the main thread would like this thread to stop.
   */
  private static boolean myShouldStop;

  /**
   * A handle back to the graphical components.
   */
  private DungeonCanvas myDungeonCanvas;

  /**
   * The System.time of the last screen refresh, used to regulate refresh
   * speed.
   */
  private long myLastRefreshTime;

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

  /**
   * standard constructor.
   */
  GameThread(DungeonCanvas canvas) {
    myDungeonCanvas = canvas;
  }

  //----------------------------------------------------------
  //   utilities

  /**
   * Get the amount of time to wait between screen refreshes. Normally we wait
   * only a single millisecond just to give the main thread a chance to update
   * the keystroke info, but this method ensures that the game will not
   * attempt to show too many frames per second.
   */
  private long getWaitTime() {
    long retVal = 1;
    long difference = System.currentTimeMillis() - myLastRefreshTime;
    if (difference < 75) {
      retVal = 75 - difference;
    }
    return (retVal);
  }

  //----------------------------------------------------------
  //   actions

  /**
   * pause the game.
   */
  void pause() {
    myShouldPause = true;
  }

  /**
   * restart the game after a pause.
   */
  synchronized void resumeGame() {
    myShouldPause = false;
    notify();
  }

  /**
   * stops the game.
   */
  synchronized void requestStop() {
    myShouldStop = true;
    this.notify();
  }

  /**
   * start the game..
   */
  public void run() {
    // flush any keystrokes that occurred before the
    // game started:
    myDungeonCanvas.flushKeys();
    myShouldStop = false;
    myShouldPause = false;
    while (true) {
      myLastRefreshTime = System.currentTimeMillis();
      if (myShouldStop) {
        break;
      }
      myDungeonCanvas.checkKeys();
      myDungeonCanvas.updateScreen();
      // we do a very short pause to allow the other thread
      // to update the information about which keys are pressed:
      synchronized (this) {
        try {
          wa

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

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

用户名: 查看更多评论

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

内 容:

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