📄 dungeoncanvas.java
字号:
package net.frog_parrot.dungeon;import java.util.Vector;import java.io.IOException;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import net.frog_parrot.util.*;/** * This class is the display of the game. * * @author Carol Hamer */public class DungeonCanvas extends GameCanvas implements CommandListener { //--------------------------------------------------------- // dimension fields // (constant after initialization) /** * the top corner x coordinate according to this * object's coordinate system:. */ static int CORNER_X = 0; /** * the top corner y coordinate according to this * object's coordinate system:. */ static 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 = 0xffffffff; /** * color constant */ public static final int OPAQUE_BLACK = 0xff000000; /** * color constant */ public static final int OPAQUE_BLUE = 0xff0000ff; //--------------------------------------------------------- // game object fields /** * a handle to the display. */ Display myDisplay; /** * a handle to the MIDlet object (to keep track of buttons). */ Dungeon myDungeon; /** * the LayerManager that handles the game graphics. */ DungeonManager myManager; /** * the Customizer. */ Customizer myCustomizer; /** * whether or not the game has ended. */ 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. */ int myDisplayGameTicks = 0; /** * the number of game ticks that have passed since the * beginning of the game. */ int myGameTicks = myDisplayGameTicks; /** * An array of number sprites to hold the digit images * for the time display. */ Sprite[] myNumberSprites = new Sprite[5]; /** * The button to exit the game. */ Command myExitCommand; /** * The button to display the command menu. */ Command myMenuCommand; /** * The button to go to the next board. */ Command myOkCommand; //--------------------------------------------------------- // menu-related fields /** * Whether the menu is currently displayed. */ boolean myMenuMode; /** * The index (in the menu vector) of the currently focused * command. */ int myFocusedIndex; /** * The images to use for the current menu items. */ Vector myMenuVector = new Vector(5); /** * The space between menu items. */ static int MENU_BUFFER; /** * The animated sprite that indicates the selected item * in the menu. */ Sprite myStars; /** * Menu sprite constant. */ int FOCUSED = 0; /** * Menu sprite constant. */ int UNFOCUSED = 1; /** * a menu image. */ Sprite myNext; /** * a menu image. */ Sprite myRestore; /** * a menu image. */ Sprite mySave; /** * a softkey image. */ Image myExit; /** * a softkey image. */ Image myMenu; /** * a softkey image. */ Image myOk; //----------------------------------------------------- // gets/sets /** * This is called when the game ends. */ void setGameOver() { myGameOver = true; myDungeon.pauseApp(); if(!myCustomizer.useSoftkeys()) { removeCommand(myMenuCommand); addCommand(myOkCommand); } } /** * Get the DungeonManager. */ DungeonManager getManager() { return myManager; } /** * Find out if the game has ended. */ static boolean getGameOver() { return(myGameOver); } /** * Get the Customizer. */ public Customizer getCustomizer() { return myCustomizer; } //----------------------------------------------------- // initialization and game state changes /** * Constructor sets the data, performs dimension calculations, * and creates the graphical objects. */ public DungeonCanvas(Dungeon midlet) throws Exception { super(false); myDisplay = Display.getDisplay(midlet); myDungeon = midlet; // calculate the dimensions based on the full screen setFullScreenMode(true); 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 > 375) || (DISP_HEIGHT > 375)) { throw(new Exception("Screen too large")); } // create the class that handles the differences among // the various platforms. myCustomizer = new Customizer(DISP_WIDTH, DISP_HEIGHT); // 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, myCustomizer, this); } } /** * Once the customizer has been initialized, this * method loads and initializes the graphical objects * for the timer and the menu. */ void start() throws IOException { myGameOver = false; // initialize the graphics for the timeclock: Image numberImage = myManager.getNumberImage(); int width = numberImage.getWidth() / 11; int height = numberImage.getHeight(); for(int i = 0; i < 5; i++) { myNumberSprites[i] = new Sprite(numberImage, width, height); myNumberSprites[i].setPosition(width*i, 0); } // frame 10 is the colon: myNumberSprites[2].setFrame(10); // if the customizer identifies the platform as // one we have keycode data for, we can implement // the softkeys with images if(myCustomizer.useSoftkeys()) { setFullScreenMode(true); DISP_WIDTH = getWidth(); DISP_HEIGHT = getHeight(); myExit = myCustomizer.getLabelImage("exit"); myMenu = myCustomizer.getLabelImage("menu"); myOk = myCustomizer.getLabelImage("ok"); } else { // if the customizer doesn't have keycodes // for the current platform, then lcdui // commands must be used: setFullScreenMode(false); myExitCommand = new Command(myCustomizer.getLabel("exit"), Command.EXIT, 99); addCommand(myExitCommand); myMenuCommand = new Command(myCustomizer.getLabel("menu"), Command.SCREEN, 1); addCommand(myMenuCommand); myOkCommand = new Command(myCustomizer.getLabel("ok"), Command.SCREEN, 1); setCommandListener(this); } // Now that the timer and softkeys are ready, // this screen can be displayed (since the menu is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -