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
 then open the door
        // otherwise bounce off
        if (myPrincess.collidesWith(myDoors[i]true)) {
          if ((myHeldKey != null)
              && (myDoors[i].getColor() == myHeldKey.getColor())) {
            setNeedsRepaint();
            myDoors[i].setVisible(false);
          else {
            // if she's not holding the right key, then
            // she has collided with the door just the same
            // as if she had collided with a wall:
            retVal = true;
          }
        }
      }
    }
    return (retVal);
  }

}

/**
 * This class is the display of the game.
 
 @author Carol Hamer
 */

class DungeonCanvas extends GameCanvas {

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

  /**
   * the height of the black region below the play area.
   */
  static int TIMER_HEIGHT = 32;

  /**
   * the top corner x coordinate according to this object's coordinate
   * system:.
   */
  static final int CORNER_X = 0;

  /**
   * the top corner y coordinate according to this object's coordinate
   * system:.
   */
  static final int CORNER_Y = 0;

  /**
   * the width of the portion of the screen that this canvas can use.
   */
  static int DISP_WIDTH;

  /**
   * the height of the portion of the screen that this canvas can use.
   */
  static int DISP_HEIGHT;

  /**
   * the height of the font used for this game.
   */
  static int FONT_HEIGHT;

  /**
   * the font used for this game.
   */
  static Font FONT;

  /**
   * color constant
   */
  public static final int BLACK = 0;

  /**
   * color constant
   */
  public static final int WHITE = 0xffffff;

  //---------------------------------------------------------
  //   game object fields

  /**
   * a handle to the display.
   */
  private Display myDisplay;

  /**
   * a handle to the MIDlet object (to keep track of buttons).
   */
  private Dungeon myDungeon;

  /**
   * the LayerManager that handles the game graphics.
   */
  private DungeonManager myManager;

  /**
   * whether or not the game has ended.
   */
  private static boolean myGameOver;

  /**
   * The number of ticks on the clock the last time the time display was
   * updated. This is saved to determine if the time string needs to be
   * recomputed.
   */
  private int myOldGameTicks = 0;

  /**
   * the number of game ticks that have passed since the beginning of the
   * game.
   */
  private int myGameTicks = myOldGameTicks;

  /**
   * we save the time string to avoid recreating it unnecessarily.
   */
  private static String myInitialString = "0:00";

  /**
   * we save the time string to avoid recreating it unnecessarily.
   */
  private String myTimeString = myInitialString;

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

  /**
   * This is called when the game ends.
   */
  void setGameOver() {
    myGameOver = true;
    myDungeon.pauseApp();
  }

  /**
   * Find out if the game has ended.
   */
  static boolean getGameOver() {
    return (myGameOver);
  }

  /**
   * Tell the layer manager that it needs to repaint.
   */
  public void setNeedsRepaint() {
    myManager.setNeedsRepaint();
  }

  //-----------------------------------------------------
  //    initialization and game state changes

  /**
   * Constructor sets the data, performs dimension calculations, and creates
   * the graphical objects.
   */
  public DungeonCanvas(Dungeon midletthrows Exception {
    super(false);
    myDisplay = Display.getDisplay(midlet);
    myDungeon = midlet;
    // calculate the dimensions
    DISP_WIDTH = getWidth();
    DISP_HEIGHT = getHeight();
    if ((!myDisplay.isColor()) || (myDisplay.numColors() 256)) {
      throw (new Exception("game requires full-color screen"));
    }
    if ((DISP_WIDTH < 150|| (DISP_HEIGHT < 170)) {
      throw (new Exception("Screen too small"));
    }
    if ((DISP_WIDTH > 250|| (DISP_HEIGHT > 250)) {
      throw (new Exception("Screen too large"));
    }
    // since the time is painted in white on black,
    // it shows up better if the font is bold:
    FONT = Font
        .getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
    // calculate the height of the black region that the
    // timer is painted on:
    FONT_HEIGHT = FONT.getHeight();
    TIMER_HEIGHT = FONT_HEIGHT + 8;
    // create the LayerManager (where all of the interesting
    // graphics go!) and give it the dimensions of the
    // region it is supposed to paint:
    if (myManager == null) {
      myManager = new DungeonManager(CORNER_X, CORNER_Y, DISP_WIDTH,
          DISP_HEIGHT - TIMER_HEIGHT, this);
    }
  }

  /**
   * This is called as soon as the application begins.
   */
  void start() {
    myGameOver = false;
    myDisplay.setCurrent(this);
    setNeedsRepaint();
  }

  /**
   * sets all variables back to their initial positions.
   */
  void reset() throws Exception {
    // most of the variables that need to be reset
    // are held by the LayerManager:
    myManager.reset();
    myGameOver = false;
    setNeedsRepaint();
  }

  /**
   * sets all variables back to the positions from a previously saved game.
   */
  void revertToSaved() throws Exception {
    // most of the variables that need to be reset
    // are held by the LayerManager, so we
    // prompt the LayerManager to get the
    // saved data:
    myGameTicks = myManager.revertToSaved();
    myGameOver = false;
    myOldGameTicks = myGameTicks;
    myTimeString = formatTime();
    setNeedsRepaint();
  }

  /**
   * save the current game in progress.
   */
  void saveGame() throws Exception {
    myManager.saveGame(myGameTicks);
  }

  /**
   * clears the key states.
   */
  void flushKeys() {
    getKeyStates();
  }

  /**
   * If the game is hidden by another app (or a menu) ignore it since not much
   * happens in this game when the user is not actively interacting with it.
   * (we could pause the timer, but it's not important enough to bother with
   * when the user is just pulling up a menu for a few seconds)
   */
  protected void hideNotify() {
  }

  /**
   * When it comes back into view, just make sure the manager knows that it
   * needs to repaint.
   */
  protected void showNotify() {
    setNeedsRepaint();
  }

  //-------------------------------------------------------
  //  graphics methods

  /**
   * paint the game graphics on the screen.
   */
  public void paint(Graphics g) {
    // color the bottom segment of the screen b

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

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

用户名: 查看更多评论

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

内 容:

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