一个J2ME地牢游戏的源码 |
| 作者:J2MEDEV 来源:J2MEDEV 发布时间:2006-7-25 9:06:59 |
|
s the // background dungeon map: myLayer = new TiledLayer(16, 16, 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[2 * i] = new DoorKey(i, false, myDoors[2 * i]); retArray[2 * i + 1] = new DoorKey(i, false, myDoors[2 * 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[i] = new 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 offset) throws 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 + 2 * i] & flag) != 0) { frame = 1; } else { frame = 0; } dungeon.setCell(j, i, frame); if ((data[offset + 2 * 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 |
| [] [返回上一页] [打 印] |
|
文章评论 |
