📄 gameshell.java
字号:
package javagapi;import java.io.*;import java.util.Random;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import javax.microedition.rms.RecordStore;import javax.microedition.rms.InvalidRecordIDException;import java.util.Calendar;//import java.util.Timer;//import java.util.TimerTask;/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! remember show/hideNotify (pause game) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*//** GameShell is a prototype for the class that the games converted * from C to Java will be embedded in. * * The converted game code should subclass this and implement * the abstract gameMain method. This method is the event handler. * * @author */abstract class GameShell extends Canvas { /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Constants %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ /** Not hard coded screen size =) */ protected int DISPLAY_WIDTH; protected int DISPLAY_HEIGHT; protected static final int ALIGN_TOP_LEFT = Graphics.TOP|Graphics.LEFT; // GAEVENT TYPES (enum) public static final byte GAStartEvent=0; public static final byte GARedrawEvent=1; public static final byte GAKeyboardEvent=2; public static final byte GATimerEvent=3; public static final byte GAReadByteEvent=4; public static final byte GATerminateEvent=5; public static final byte GAMenuSettingsEvent=6; public static final byte GAComOpenEvent=7; public static final byte GAComOpenResponseEvent=8; public static final byte GAComPutEvent=9; public static final byte GAComPutIPEvent=10; public static final byte GAComPutResponseEvent=11; public static final byte GAComCloseEvent=12; public static final byte GAComCloseResponseEvent=13; public static final byte GAComBTInquireResponseEvent=14; public static final byte GAComDNSLookupResponseEvent=15; public static final byte GAGetSMSNrFinnishEvent=16; public static final byte GAHighScoreEvent=17; public static final byte GAResetEvent=18; public static final byte GAPicSelectEvent=19; public static final byte GALostFocusEvent=20; public static final byte GAGainFocusEvent=21; public static final byte GALastEvent=22; //enum KeySym public static final byte KeyVolumeUp = 0; public static final byte KeyVolumeDown = 1; public static final byte KeyYes = 2; public static final byte KeyNo = 3; public static final byte KeyClear = 4; public static final byte KeyLeft = 5; public static final byte KeyRight = 6; public static final byte KeyZero = 7; public static final byte KeyOne = 8; public static final byte KeyTwo = 9; public static final byte KeyThree = 10; public static final byte KeyFour = 11; public static final byte KeyFive = 12; public static final byte KeySix = 13; public static final byte KeySeven = 14; public static final byte KeyEight = 15; public static final byte KeyNine = 16; public static final byte KeyStar = 17; public static final byte KeyHash = 18; public static final byte KeyFlipClose = 19; public static final byte KeyFlipOpen = 20; public static final byte KeyPHF = 21; public static final byte KeyUp = 22; public static final byte KeyDown = 23; public static final byte KeyLeftUp = 24; public static final byte KeyRightDown = 25; public static final byte KeyJoystickSelect = 26; public static final byte KeyVoiceNote = 27; public static final byte KeyOption = 28; /* OBSOLETE public static final byte KeyCbFirst = ; public static final byte KeyCbLast = KeyCbFirst + 0xFF; public static final byte KeyCbZero = ; public static final byte KeyCbOne = ; public static final byte KeyCbTwo = ; public static final byte KeyCbThree = ; public static final byte KeyCbFour = ; public static final byte KeyCbFive = ; public static final byte KeyCbSix = ; public static final byte KeyCbSeven = ; public static final byte KeyCbEight = ; public static final byte KeyCbNine = ; public static final byte KeyLast = ; */ //enum GATimerId public static final byte GATimer1 = 0; public static final byte GATimer2 = 1; public static final byte GATimer3 = 2; public static final byte GATimerAll = 3; /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Gapigraphic Constants %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ /** Predefined colors */ static final int GACOLOR_BLACK = RGB( 0, 0, 0); static final int GACOLOR_GRAY = RGB(0xAA, 0xAA, 0xAA); static final int GACOLOR_DARK_GRAY = RGB(0x55, 0x55, 0x55); static final int GACOLOR_WHITE = RGB(0xFF, 0xFF, 0xFF); static final int GACOLOR_DARK_BLUE = RGB( 0, 0, 0x80); static final int GACOLOR_BLUE = RGB( 0, 0, 0xFF); static final int GACOLOR_DARK_GREEN = RGB( 0, 0x80, 0); static final int GACOLOR_DARK_GREEN6 = RGB( 0, 0xDF, 0); static final int GACOLOR_GREEN = RGB( 0, 0xFF, 0); static final int GACOLOR_DARK_CYAN = RGB( 0, 0x80, 0x80); static final int GACOLOR_CYAN = RGB( 0, 0xFF, 0xFF); static final int GACOLOR_RED = RGB(0xFF, 0, 0); static final int GACOLOR_DARK_RED = RGB(0x80, 0, 0); static final int GACOLOR_YELLOW = RGB(0xFF, 0xFF, 0); static final int GACOLOR_DARK_YELLOW = RGB(0x80, 0x80, 0); static final int GACOLOR_MAGENTA = RGB(0xFF, 0, 0xFF); static final int GACOLOR_DARK_MAGENTA = RGB(0x80, 0, 0x80); static final int GACOLOR_PURPLE = RGB(0xA0, 0x20, 0xF0); static final int GACOLOR_ORANGE = RGB(0xFF, 0xC0, 0x00); static final int GACOLOR_PINK = RGB(0xF8, 0x9C, 0xBC); static final int GACOLOR_BROWN = RGB(0xA5, 0x7F, 0x50); static final int GACOLOR_SKYBLUE = RGB(0xC0, 0xDC, 0xC0); static final int GACOLOR_CREAM = RGB(0xFF, 0xFB, 0xF0); /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Game_info.h Constants %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ /** Unfortunately the menu select object constants had to be here in order * for the game to be able to access them without a scope prefix. */ /** * Different types of opponents a game have have * * @see GAOpponentObject_t * */ public static final int GAM_OPP_NONE = 0 ; // Meanless but just for the sake public static final int GAM_OPP_PHONE = 1 ; // Single player ( against the phone) public static final int GAM_OPP_SAME_PHONE = 2 ; // Several players on the same phone public static final int GAM_OPP_3_SAME_PHONE = 4 ; // Several players on the same phone public static final int GAM_OPP_4_SAME_PHONE = 8 ; // Several players on the same phone public static final int GAM_OPP_SMS = 16 ; // The opponent is "behind" a SMS number public static final int GAM_OPP_IRDA = 32 ; // The opponent is "behind" the irDA port public static final int GAM_OPP_BLUETOOTH = 64 ; // The opponent is "behind" a Bluetooth number public static final int GAM_OPP_CABLE = 128 ; // The opponent is "behind" the cable public static final int GAM_OPP_NETWORK = 256 ; // The opponent is "behind" the network public static final int GAM_OPP_ALL = 511; /** * Different selectable levels */ public static final int GAM_LEVEL_NONE = 0 ; // Meanless but just for the sake public static final int GAM_LEVEL_VERYEASY = 1; public static final int GAM_LEVEL_EASY = 2; public static final int GAM_LEVEL_MEDIUM = 4; public static final int GAM_LEVEL_HARD = 8; public static final int GAM_LEVEL_VERYHARD = 16; public static final int GAM_LEVEL_NUMBER = 128; public static final int GAM_BOARD_NUMBER = 256; public static final int GAM_LEVEL_ALL = 511; /** * Different selectable start modes */ public static final int GAM_START_NONE = 0; // Meanless but just for the sake public static final int GAM_START_NEW = 1; public static final int GAM_START_RESUME = 2; public static final int GAM_START_RESUME1 = 4; public static final int GAM_START_RESUME2 = 8; public static final int GAM_START_RESUME3 = 16; public static final int GAM_START_RESUME4 = 32; public static final int GAM_START_SETBIRTHDATE = 64; public static final int GAM_START_CLEARRESUME = 128; public static final int GAM_START_ALL = 255; /** * Different types of options * * @see GAOpponentObject_t * */ public static final int GAM_OPT_NONE = 0; public static final int GAM_OPT_SOUND = 1; public static final int GAM_OPT_VIBRATOR = 2; public static final int GAM_OPT_CHESSMOVE = 4; public static final int GAM_OPT_FOREGROUND_PIC_NONE = 8; public static final int GAM_OPT_FOREGROUND_PIC_TILE = 16; public static final int GAM_OPT_FOREGROUND_PIC_CENTER = 32; public static final int GAM_OPT_FOREGROUND_PIC_CENTER_FIT = 64; public static final int GAM_OPT_FOREGROUND_PIC_FILE =128; public static final int GAM_OPT_BACKGROUND_PIC_NONE =256; public static final int GAM_OPT_BACKGROUND_PIC_TILE =512; public static final int GAM_OPT_BACKGROUND_PIC_CENTER = 1024; public static final int GAM_OPT_BACKGROUND_PIC_CENTER_FIT= 2048; public static final int GAM_OPT_BACKGROUND_PIC_FILE = 4096; public static final int GAM_OPT_FORBIDDEN_RULES = 8192; public static final int GAM_OPT_PLAYER_SIDE_WHITE = 16384; public static final int GAM_OPT_PLAYER_SIDE_BLACK = 32768; public static final int GAM_OPT_ALL = 65535; /** * Different types of highscore methods * * @see GAOpponentObject_t * */ public static final int GAM_HIGHSCORE_NONE = 0; public static final int GAM_HIGHSCORE_VIEW = 1; public static final int GAM_HIGHSCORE_CLEAR = 2; public static final int GAM_HIGHSCORE_SEND = 4; public static final int GAM_HIGHSCORE_LIST_EMPTY = 8; public static final int GAM_HIGHSCORE_ALL = 255; /** * Different types of highscore methods * * @see GAOpponentObject_t * */ public static final int GAM_ACCSEARCH_NONE = 0; public static final int GAM_ACCSEARCH_ACCEPT = 1; public static final int GAM_ACCSEARCH_SEARCH = 2; public static final int GAM_ACCSEARCH_ALL = 255; /** * Different types of miscillious alternatives * * @see GAOpponentObject_t * */ public static final int GAM_ALTERNATIVES_NONE = 0; public static final int GAM_ALTERNATIVES_INFO = 1; public static final int GAM_ALTERNATIVES_MANAGEMENT= 2; public static final int GAM_ALTERNATIVES_REMOVEALL = 4; /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MIDP Related and Game Main %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ /** This is the Image that provides an offscreen graphics object for the game * to draw in. */ private Image offscreen; /** The menu select object is used by the game to tell the MIDlet which options * it allows in the menu system, and by the MIDlet to tell the game which * options were choosed by the user. */ public GAMenuSelectObject_t mso; /** The games that subclass GameShell may draw into this Graphics object at any * time. It is effectively an offscreen buffer, that is copied onto the screen * in the paint() method below. */ protected Graphics g; protected int lineColor; protected int fillColor; protected int clearColor; protected MIDlet midlet; private GAEvent curEvent = new GAEvent(); private Random randomGenerator = new Random(); protected GameTimerThread timerThread = null; /** Default constructor for subclasses of GameShell (i.e. converted games). * This means the games need not declare a constructor. * <p> * Creates an offscreen mutable Image. * Extracts the Graphics object from this Image, for the game to draw in. * Sets a basic font for the graphics. */ public GameShell() { DISPLAY_WIDTH = getWidth(); DISPLAY_HEIGHT = getHeight(); offscreen = Image.createImage(DISPLAY_WIDTH, DISPLAY_HEIGHT); g = offscreen.getGraphics(); g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,Font.SIZE_MEDIUM)); g.setColor(GACOLOR_BLACK); mso = new GAMenuSelectObject_t(); timerThread = new GameTimerThread(this); } public void setMIDlet(MIDlet m) { midlet = m; } /** Draws the offscreen buffer Image into the graphics passed as an argument. * The reason the game draws in an offscreen buffer is that it * must be able to perform drawing operations at any time, even outside paint(). */ protected void paint(Graphics g2) { //System.out.println("GameShell.paint()"); g2.drawImage(offscreen, 0, 0, Graphics.LEFT | Graphics.TOP ); timerThread.paintNotScheduled = true; } public byte[] keyArray = new byte[8]; int keyPos = -1; public boolean[] keyPressedArray = new boolean[8]; protected void keyPressed(int keyCode) { keyPos++; if(keyPos == 8) keyPos = 0; keyPressedArray[keyPos] = true; switch(keyCode) { case Canvas.KEY_NUM0: keyArray[keyPos] = KeyZero; break; case Canvas.KEY_NUM1: keyArray[keyPos] = KeyOne; break; case Canvas.KEY_NUM2: keyArray[keyPos] = KeyTwo; break; case Canvas.KEY_NUM3: keyArray[keyPos] = KeyThree; break; case Canvas.KEY_NUM4: keyArray[keyPos] = KeyFour; break; case Canvas.KEY_NUM5: keyArray[keyPos] = KeyFive; break; case Canvas.KEY_NUM6: keyArray[keyPos] = KeySix; break; case Canvas.KEY_NUM7: keyArray[keyPos] = KeySeven; break; case Canvas.KEY_NUM8: keyArray[keyPos] = KeyEight; break; case Canvas.KEY_NUM9: keyArray[keyPos] = KeyNine; break; case Canvas.KEY_STAR: keyArray[keyPos] = KeyStar; break; case Canvas.KEY_POUND: keyArray[keyPos] = KeyHash; break; default: int gameAction = getGameAction(keyCode); if(gameAction==Canvas.UP) keyArray[keyPos] = KeyUp; else if(gameAction==Canvas.DOWN) keyArray[keyPos] = KeyDown; else if(gameAction==Canvas.LEFT) keyArray[keyPos] = KeyLeft; else if(gameAction==Canvas.RIGHT) keyArray[keyPos] = KeyRight; else if(gameAction==Canvas.FIRE) keyArray[keyPos] = KeyYes; else keyArray[keyPos] = Byte.MIN_VALUE; } timerThread.keysUpdated++; } protected void keyReleased(int keyCode) { keyPos++; if(keyPos == 8) keyPos = 0; keyPressedArray[keyPos] = false; switch(keyCode) { case Canvas.KEY_NUM0: keyArray[keyPos] = KeyZero; break; case Canvas.KEY_NUM1: keyArray[keyPos] = KeyOne; break; case Canvas.KEY_NUM2: keyArray[keyPos] = KeyTwo; break; case Canvas.KEY_NUM3: keyArray[keyPos] = KeyThree; break; case Canvas.KEY_NUM4: keyArray[keyPos] = KeyFour; break; case Canvas.KEY_NUM5: keyArray[keyPos] = KeyFive; break; case Canvas.KEY_NUM6: keyArray[keyPos] = KeySix; break; case Canvas.KEY_NUM7: keyArray[keyPos] = KeySeven; break; case Canvas.KEY_NUM8: keyArray[keyPos] = KeyEight; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -