一个J2ME地牢游戏的源码 |
| 作者:J2MEDEV 来源:J2MEDEV 发布时间:2006-7-25 9:06:59 |
|
me 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. */ private DungeonCanvas myCanvas; /** * the background dungeon. */ private TiledLayer myBackground; /** * the player. */ private Sprite myPrincess; /** * the goal. */ private Sprite myCrown; /** * the doors. */ private DoorKey[] myDoors; /** * the keys. */ private DoorKey[] myKeys; /** * the key currently held by the player. */ private DoorKey myHeldKey; /** * The leftmost x-coordinate that should be visible on the screen in terms * of this objects internal coordinates. */ private int myViewWindowX; /** * The top y-coordinate that should be visible on the screen in terms of * this objects internal coordinates. */ private int myViewWindowY; /** * Where the princess is in the jump sequence. */ private int myIsJumping = NO_JUMP; /** * Whether or not the screen needs to be repainted. */ private boolean myModifiedSinceLastPaint = true; /** * Which board we're playing on. */ private 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 myBackground = 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(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 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(myBackground); // now create the new board: myCurrentBoardNum++; // in this version we go back to the beginning if // all boards have been completed. if (myCurrentBoardNum == BoardDecoder.getNumBoards()) |
| [] [返回上一页] [打 印] |
|
文章评论 |
