⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dungeonmanager.java

📁 Creat mobile game Creat mobile game Creat mobile game Creat mobile game
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package net.frog_parrot.dungeon;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;/** * This class handles the graphics objects. *  * @author Carol Hamer */public class DungeonManager extends LayerManager {  //---------------------------------------------------------  //   dimension fields  //  (constant after initialization)  /**   * The x-coordinate of the place on the game canvas where    * the LayerManager window should appear, in terms of the    * coordiantes of the game canvas.   */  static int CANVAS_X;  /**   * The y-coordinate of the place on the game canvas where    * the LayerManager window should appear, in terms of the    * coordiantes of the game canvas.   */  static int CANVAS_Y;  /**   * The width of the display window.   */  static int DISP_WIDTH;  /**   * The height of this object's visible region.    */  static int DISP_HEIGHT;  /**   * the (right or left)  distance the player    * goes in a single keystroke.   */  static final int MOVE_LENGTH = 8;  /**   * The width of the square tiles that this game is divided into.   * This is the width of the stone walls as well as the princess and    * the ghost.   */  static final int SQUARE_WIDTH = 24;  /**   * The jump index that indicates that no jump is    * currently in progress..   */  static final int NO_JUMP = -6;  /**   * The maximum speed for the player's fall..   */  static final int MAX_FREE_FALL = 3;  //---------------------------------------------------------  //   game object fields  /**   * the handle back to the canvas.   */  DungeonCanvas myCanvas;  /**   * the background dungeon.   */  TiledLayer myWalls;  /**   * the player.   */  Sprite myPrincess;  /**   * the goal.   */  Sprite myCrown;  /**   * the doors.   */  DoorKey[] myDoors;  /**   * the keys.   */  DoorKey[] myKeys;  /**   * the key currently held by the player.   */  DoorKey myHeldKey;  /**   * The leftmost x-coordinate that should be visible on the    * screen in terms of this objects internal coordinates.   */  int myViewWindowX;  /**   * The top y-coordinate that should be visible on the    * screen in terms of this objects internal coordinates.   */  int myViewWindowY;  /**   * Where the princess is in the jump sequence.   */  int myIsJumping = NO_JUMP;  /**   * Whether or not the screen needs to be repainted.   */  boolean myModifiedSinceLastPaint = true;  /**   * Which board we're playing on.   */  int myCurrentBoardNum = 0;  //-----------------------------------------------------  //    gets/sets  /**   * Tell the layer manager that it needs to repaint.   */  public void setNeedsRepaint() {    myModifiedSinceLastPaint = true;  }  //-----------------------------------------------------  //    initialization  //    set up or save game data.  /**   * Constructor merely sets the data.   * @param x The x-coordinate of the place on the game canvas where    * the LayerManager window should appear, in terms of the    * coordiantes of the game canvas.   * @param y The y-coordinate of the place on the game canvas where    * the LayerManager window should appear, in terms of the    * coordiantes of the game canvas.   * @param width the width of the region that is to be    * occupied by the LayoutManager.   * @param height the height of the region that is to be    * occupied by the LayoutManager.   * @param canvas the DungeonCanvas that this LayerManager    * should appear on.   */  public DungeonManager(int x, int y, int width, int height, 			DungeonCanvas canvas) throws Exception {    myCanvas = canvas;    CANVAS_X = x;    CANVAS_Y = y;    DISP_WIDTH = width;    DISP_HEIGHT = height;    // create a decoder object that creates the dungeon and     // its associated Sprites from data.      BoardDecoder decoder = new BoardDecoder(myCurrentBoardNum);    // get the background TiledLayer    myWalls = decoder.getLayer();    // get the coordinates of the square that the princess     // starts on.    int[] playerCoords = decoder.getPlayerSquare();    // create the player sprite    myPrincess = new Sprite(Image.createImage("/images/princess.png"), 			    SQUARE_WIDTH, SQUARE_WIDTH);    myPrincess.setFrame(1);    // we define the reference pixel to be in the middle     // of the princess image so that when the princess turns     // from right to left (and vice versa) she does not     // appear to move to a different location.    myPrincess.defineReferencePixel(SQUARE_WIDTH/2, 0);    // the dungeon is a 16x16 grid, so the array playerCoords    // gives the player's location in terms of the grid, and     // then we multiply those coordinates by the SQUARE_WIDTH    // to get the precise pixel where the player should be     // placed (in terms of the LayerManager's coordinate system)    myPrincess.setPosition(SQUARE_WIDTH * playerCoords[0], 			   SQUARE_WIDTH * playerCoords[1]);    // we append all of the Layers (TiledLayer and Sprite)     // so that this LayerManager will paint them when     // flushGraphics is called.    append(myPrincess);    // get the coordinates of the square where the crown     // should be placed.    int[] goalCoords = decoder.getGoalSquare();    myCrown = new Sprite(Image.createImage("/images/crown.png"));    myCrown.setPosition((SQUARE_WIDTH * goalCoords[0]) + (SQUARE_WIDTH/4), 			(SQUARE_WIDTH * goalCoords[1]) + (SQUARE_WIDTH/2));    append(myCrown);    // The decoder creates the door and key sprites and places     // them in the correct locations in terms of the LayerManager's    // coordinate system.    myDoors = decoder.createDoors();    myKeys = decoder.createKeys();    for(int i = 0; i < myDoors.length; i++) {      append(myDoors[i]);    }    for(int i = 0; i < myKeys.length; i++) {      append(myKeys[i]);    }    // append the background last so it will be painted first.    append(myWalls);    // this sets the view screen so that the player is     // in the center.    myViewWindowX = SQUARE_WIDTH * playerCoords[0]       - ((DISP_WIDTH - SQUARE_WIDTH)/2);    myViewWindowY = SQUARE_WIDTH * playerCoords[1]       - ((DISP_HEIGHT - SQUARE_WIDTH)/2);    // a number of objects are created in order to set up the game,    // but they should be eliminated to free up memory:    decoder = null;    System.gc();  }  /**   * sets all variables back to their initial positions.   */  void reset() throws Exception {    // first get rid of the old board:    for(int i = 0; i < myDoors.length; i++) {      remove(myDoors[i]);    }    myHeldKey = null;    for(int i = 0; i < myKeys.length; i++) {      remove(myKeys[i]);    }    remove(myWalls);    // now create the new board:    myCurrentBoardNum++;    // in this version we go back to the beginning if     // all boards have been completed.    if(myCurrentBoardNum >= BoardReader.getNumBoards()) {      myCurrentBoardNum = 0;    }    // we create a new decoder object to read and interpret     // all of the data for the current board.    BoardDecoder decoder = new BoardDecoder(myCurrentBoardNum);    // get the background TiledLayer    myWalls = decoder.getLayer();    // get the coordinates of the square that the princess     // starts on.    int[] playerCoords = decoder.getPlayerSquare();    // the dungeon is a 16x16 grid, so the array playerCoords    // gives the player's location in terms of the grid, and     // then we multiply those coordinates by the SQUARE_WIDTH    // to get the precise pixel where the player should be     // placed (in terms of the LayerManager's coordinate system)    myPrincess.setPosition(SQUARE_WIDTH * playerCoords[0], 			   SQUARE_WIDTH * playerCoords[1]);    myPrincess.setFrame(1);    // get the coordinates of the square where the crown     // should be placed.    int[] goalCoords = decoder.getGoalSquare();    myCrown.setPosition((SQUARE_WIDTH * goalCoords[0]) + (SQUARE_WIDTH/4), 			(SQUARE_WIDTH * goalCoords[1]) + (SQUARE_WIDTH/2));    // The decoder creates the door and key sprites and places     // them in the correct locations in terms of the LayerManager's    // coordinate system.    myDoors = decoder.createDoors();    myKeys = decoder.createKeys();    for(int i = 0; i < myDoors.length; i++) {      append(myDoors[i]);    }    for(int i = 0; i < myKeys.length; i++) {      append(myKeys[i]);    }    // append the background last so it will be painted first.    append(myWalls);    // this sets the view screen so that the player is     // in the center.    myViewWindowX = SQUARE_WIDTH * playerCoords[0]       - ((DISP_WIDTH - SQUARE_WIDTH)/2);    myViewWindowY = SQUARE_WIDTH * playerCoords[1]       - ((DISP_HEIGHT - SQUARE_WIDTH)/2);    // a number of objects are created in order to set up the game,    // but they should be eliminated to free up memory:    decoder = null;    System.gc();  }  /**   * sets all variables back to the position in the saved game.   * @return the time on the clock of the saved game.   */  int revertToSaved() throws Exception {    int retVal = 0;    // first get rid of the old board:    for(int i = 0; i < myDoors.length; i++) {      remove(myDoors[i]);    }    myHeldKey = null;    for(int i = 0; i < myKeys.length; i++) {      remove(myKeys[i]);    }    remove(myWalls);    // now get the info of the saved game    // only one game is saved at a time, and the GameInfo object     // will read the saved game's data from memory.    GameInfo info = new GameInfo();    if(info.getIsEmpty()) {      // if no game has been saved, we start from the beginning.      myCurrentBoardNum = 0;      reset();    } else {      // get the time on the clock of the saved game.      retVal = info.getTime();      // get the number of the board the saved game was on.      myCurrentBoardNum = info.getBoardNum();      // create the BoradDecoder that gives the data for the       // desired board.      BoardDecoder decoder = new BoardDecoder(myCurrentBoardNum);      // get the background TiledLayer      myWalls = decoder.getLayer();      // get the coordinates of the square that the princess       // was on in the saved game.      int[] playerCoords = info.getPlayerSquare();      myPrincess.setPosition(SQUARE_WIDTH * playerCoords[0], 			     SQUARE_WIDTH * playerCoords[1]);      myPrincess.setFrame(1);      // get the coordinates of the square where the crown       // should be placed (this is given by the BoardDecoder       // and not from the data of the saved game because the       // crown does not move during the game.      int[] goalCoords = decoder.getGoalSquare();      myCrown.setPosition((SQUARE_WIDTH * goalCoords[0]) + (SQUARE_WIDTH/4), 			  (SQUARE_WIDTH * goalCoords[1]) + (SQUARE_WIDTH/2));      // The decoder creates the door and key sprites and places       // them in the correct locations in terms of the LayerManager's      // coordinate system.      myDoors = decoder.createDoors();      myKeys = decoder.createKeys();      // get an array of ints that lists whether each door is       // open or closed in the saved game      int[] openDoors = info.getDoorsOpen();      for(int i = 0; i < myDoors.length; i++) {	append(myDoors[i]);	if(openDoors[i] == 0) {	  // if the door was open, make it invisible	  myDoors[i].setVisible(false);	}      }      // the keys can be moved by the player, so we get their       // coordinates from the GameInfo saved data.      int[][] keyCoords = info.getKeyCoords();      for(int i = 0; i < myKeys.length; i++) {	append(myKeys[i]);	myKeys[i].setPosition(SQUARE_WIDTH * keyCoords[i][0], 			     SQUARE_WIDTH * keyCoords[i][1]);      }      // if the player was holding a key in the saved game,       // we have the player hold that key and set it to invisible.      int heldKey = info.getHeldKey();      if(heldKey != -1) {	myHeldKey = myKeys[heldKey];	myHeldKey.setVisible(false);      }      // append the background last so it will be painted first.      append(myWalls);      // this sets the view screen so that the player is       // in the center.      myViewWindowX = SQUARE_WIDTH * playerCoords[0] 	- ((DISP_WIDTH - SQUARE_WIDTH)/2);      myViewWindowY = SQUARE_WIDTH * playerCoords[1] 	- ((DISP_HEIGHT - SQUARE_WIDTH)/2);      // a number of objects are created in order to set up the game,      // but they should be eliminated to free up memory:      decoder = null;      System.gc();    }    return(retVal);  }  /**   * save the current game in progress.   */  void saveGame(int gameTicks) throws Exception {    int[] playerSquare = new int[2];    // the coordinates of the player are given in terms of     // the 16 x 16 dungeon grid. We divide the player's 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -