⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameshell.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// package javagapi;import java.io.*;import java.util.Random;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.rms.RecordStore;import javax.microedition.rms.InvalidRecordIDException;import java.util.Calendar;/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  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 implements Runnable{    // Handling thread stuff    private boolean stopped;    private boolean paused;    // For dynamic 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;    public static final byte GALoadImgEvent = 23;    //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;    //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;    /**     * 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_LANGUAGE = 65536;    public static final int GAM_OPT_ALL = 131073;    /**     * Some stuff for the communications     */    public static final boolean USE_MAC = true;    public static final boolean HAS_WAP = true;    /**     * 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;    // High Score send variables    /*    private static final String defaultURL = "http://cip-ext.aus.teleca.se/cip/highscore/index.jsp";    private static final String m = "T610";    private static final byte v = 11;   // The version of the algorithm used for generating the authentication key    private static int srl = -1;     */    // For easy High Score layout    public static short DiffWidth;    private static final short HSTMargin = 0;   // High Score top margin    private static final short SpaceW = 3;      // Spacing between characters    private static final short TextPack = 0;    // Pack text lines by subtracting space between them    // For easy Progress Bar layout    private static final short PBTMargin = 20;   // Message box top margin    private static final short PBSMargin = 5;   // Message box side margin    private static final short PBBMargin = 5;   // Progress Bar margin    private static final short PBFMargin = 2;   // Bar frame margin    private static final short PBHeight = 12;   // Progress Bar height    /*     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     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 scoreColor;    protected int fillColor;    protected int clearColor;    protected MIDlet midlet;    private short loadProgress;    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()    {        stopped = false;        // The drawing will start the first time showNotify() is called, until then pause        paused = true;        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);    }    /**     * Called by GameMIDlet in order to launch this application; that is loading images     * while showing progress bar and sending start event to the game main method.     */    public void launchGame()    {        loadProgress = 0;        GAImageProgress(0);        // set up initial frame        repaint();        Display.getDisplay(midlet).callSerially(this);    }    /**     * Used in order to show progress bar while loading images.     * When images are loaded, the game is started.     */    public void run()    { // called after previous repaint is finished        if (loadProgress < 6)        {            loadProgress++;            GAImageProgress(loadProgress);            loadImages(loadProgress);            repaint();            Display.getDisplay(midlet).callSerially(this);        }        else        {            GAEvent e = new GAEvent();            e.type = GAStartEvent;            gameMain(e);        }    }    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, g2.LEFT | g2.TOP);        // timerThread.paintNotScheduled = true;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -