一个J2ME地牢游戏的源码 |
| 作者:J2MEDEV 来源:J2MEDEV 发布时间:2006-7-25 9:06:59 |
|
23; 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 myBackground = 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(myBackground); // 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(myBackground); // 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 myBackground = 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(myBackground); // 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 // pixel coordinates to ge the right grid square. // If the player was not precisely alligned with a // grid square when the game was saved, the difference // will be shaved off. playerSquare[0] = myPrincess.getX() / SQUARE_WIDTH; playerSquare[1] = myPrincess.getY() / SQUARE_WIDTH; // save the coordinates of the current locations of // the keys, and if a key is currently held by the // player, we save the info of which one it was. int[][] keyCoords = new int[4][]; int heldKey = -1; for (int i = 0; i < myKeys.length; i++) { keyCoords[i] = new int[2]; keyCoords[i][0] = myKeys[i] |
| [] [返回上一页] [打 印] |
|
文章评论 |
