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

📄 gamepage.java~3~

📁 J2ME游戏引擎,直接在JBUILDER2006下运行,不包含需要的SDK和虚拟机
💻 JAVA~3~
字号:
package GoGoGo;import java.io.*;import javax.microedition.rms.*;import java.util.Vector;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0.0 */public class GamePage extends GameFrameBase{    //=在游戏中需要用到的一些常量=====    final static byte SMALL_BLOCK_SIZE = 16;    final static byte START_POS_X = 4;    final static byte START_POS_Y = 1;    final static byte OPTION_TOTAL = 3;    //========================END=    //=Frame状态定义===============    final static byte PAGES_MAIN_MENU = 0;    final static byte PAGES_OPTIONS = 1;    final static byte PAGES_GAMING_OPTIONS = 2;    final static byte PAGES_HELP1 = 3;    final static byte PAGES_HELP2 = 4;    //========================END=    //=MENU ITEMS=================    final static byte MENU_PLAY = 0;    final static byte MENU_OPTION = 1;    final static byte MENU_HELP = 2;    final static byte MENU_EXIT = 3;    //========================END=    //=定义的一些中间变量============    private byte m_i_MenuItemIdx;    private byte m_i_OptionItemIdx;    private byte m_i_GameOption[];    //private byte m_i_GameState;    //========================END=    //=本Frame中使用的图片==========    private Surface m_Surface_MenuMain = null;    private Surface m_Surface_MenuOptions = null;    private Surface m_Surface_MenuHelp1 = null;    private Surface m_Surface_MenuHelp2 = null;    private Surface m_Surface_MenuOnOff = null;    private Surface m_Surface_MenuContinue = null;    private Surface m_Surface_TitleMain = null;    private Surface m_Surface_SmallRoad = null;    //========================END=    public GamePage()    {        m_vect_SurfacePath = new Vector();        m_vect_SurfacePath.addElement("/menu_main.png");        m_vect_SurfacePath.addElement("/menu_options.png");        m_vect_SurfacePath.addElement("/menu_help1.png");        m_vect_SurfacePath.addElement("/menu_help2.png");        m_vect_SurfacePath.addElement("/menu_onoff.png");        m_vect_SurfacePath.addElement("/menu_continue.png");        m_vect_SurfacePath.addElement("/title_main.png");        m_vect_SurfacePath.addElement("/smallroad.png");        m_i_GameOption = new byte[OPTION_TOTAL];        ReadOptionsRecords();    }    public void Create(GameViewBase view)    {        m_View = view;        m_BackGraphy = m_View.m_g_BackGraphy;        m_i_HalfScreenWidth = m_View.m_i_ScreenWidth / 2;        m_i_HalfScreenHeight = m_View.m_i_ScreenHeight / 2;        //注意调用的顺序要和m_vect_SurfacePath中图像路径的顺序一致        m_Surface_MenuMain = m_View.nextSurface();        m_Surface_MenuOptions = m_View.nextSurface();        m_Surface_MenuHelp1 = m_View.nextSurface();        m_Surface_MenuHelp2 = m_View.nextSurface();        m_Surface_MenuOnOff = m_View.nextSurface();        m_Surface_MenuContinue = m_View.nextSurface();        m_Surface_TitleMain = m_View.nextSurface();        m_Surface_SmallRoad = m_View.nextSurface();    }    public void Release()    {        WriteOptionsRecord();        if(m_View != null)            m_View.ReleaseAllSurfaces();    }    public void OnKeyDown(int iScanCode)    {        switch (m_i_FrameState)        {            case PAGES_MAIN_MENU:                MainMenu_Onkey(iScanCode);                break;            case PAGES_OPTIONS:            case PAGES_GAMING_OPTIONS:                Options_Onkey(iScanCode);                break;            case PAGES_HELP1:                m_i_FrameState = PAGES_HELP2;                break;            case PAGES_HELP2:                m_i_FrameState = PAGES_MAIN_MENU;                break;        }    }    public void OnKeyUp(int iScanCode)    {    }    private void Options_Onkey(int iScanCode)    {        switch (iScanCode)        {            case GameViewBase.KEY_SOFTKEY1:                switch (m_i_OptionItemIdx)                {                    case 0:                        m_i_GameOption[0] = (byte) (1 - m_i_GameOption[0]);                        break;                    case 1:                        m_i_GameOption[1] = (byte) (1 - m_i_GameOption[1]);                        break;                    case 2:                        m_i_GameOption[2] = (byte) (1 - m_i_GameOption[2]);                        break;                    case 4:                        m_i_FrameState = PAGES_MAIN_MENU;                        break;                }                break;            case GameViewBase.KEY_UP:                if (m_i_OptionItemIdx == 0)                    m_i_OptionItemIdx = 4;                else                    m_i_OptionItemIdx--;                if ( (m_i_OptionItemIdx == 3)                     && (m_i_FrameState != PAGES_GAMING_OPTIONS)                     )                    m_i_OptionItemIdx--;                break;            case GameViewBase.KEY_DOWN:                if (m_i_OptionItemIdx == 4)                    m_i_OptionItemIdx = 0;                else                    m_i_OptionItemIdx++;                if ( (m_i_OptionItemIdx == 3)                     && (m_i_FrameState != PAGES_GAMING_OPTIONS)                    )                    m_i_OptionItemIdx++;                break;        }    }    private void MainMenu_Onkey(int iScanCode)    {        switch (iScanCode)        {            case GameViewBase.KEY_SOFTKEY1:                switch (m_i_MenuItemIdx)                {                    case 0:                        GotoGame();                        break;                    case 1:                        m_i_FrameState = PAGES_OPTIONS;                        break;                    case 2:                        m_i_FrameState = PAGES_HELP1;                        break;                    case 3:                        m_i_FrameTimer = -1;                        //m_View.m_App.notifyDestroyed();                        break;                }                break;            case GameViewBase.KEY_UP:                if (m_i_MenuItemIdx == 0)                    m_i_MenuItemIdx = 3;                else                    m_i_MenuItemIdx--;                break;            case GameViewBase.KEY_DOWN:                if (m_i_MenuItemIdx == 3)                    m_i_MenuItemIdx = 0;                else                    m_i_MenuItemIdx++;                break;        }    }    public void GotoGame()    {        m_View.m_pge_gameplay.NewGame(0);        m_View.m_pge_gameswitch.SwitchFrame(this, m_View.m_pge_gameplay);    }    public void GotoMenu()    {        m_i_FrameTimer = 0;        m_i_MenuItemIdx = MENU_PLAY;        m_i_FrameState = PAGES_MAIN_MENU;    }    //=显示部分及其子函数===========    public void Show()    {        switch (m_i_FrameState)        {            case PAGES_MAIN_MENU:                DrawManuBackGround();                DrawMainMenu();                if(m_i_FrameTimer == -1)                {                    m_View.exit = true;                    return;                    //m_View.m_App.notifyDestroyed();                }                break;            case PAGES_OPTIONS:                DrawManuBackGround();                DrawOptionsMenu();                break;            case PAGES_HELP1:                DrawGameBackGround();                m_View.Blt(9, 11, m_Surface_MenuHelp1.width, m_Surface_MenuHelp1.height,                           0, 0, m_Surface_MenuHelp1);                break;            case PAGES_HELP2:                DrawGameBackGround();                m_View.Blt(9, 11, m_Surface_MenuHelp2.width, m_Surface_MenuHelp2.height,                           0, 0, m_Surface_MenuHelp2);                break;        }    }    private void DrawOptionsMenu()    {        m_View.Blt(22, 41, m_Surface_MenuOptions.width / 2, m_Surface_MenuOptions.height, 0, 0, m_Surface_MenuOptions);        m_View.Blt(22, 41 + m_Surface_MenuOptions.height / 5 * m_i_OptionItemIdx,                   m_Surface_MenuOptions.width / 2, m_Surface_MenuOptions.height / 5,                   m_Surface_MenuOptions.width / 2, m_Surface_MenuOptions.height / 5 * m_i_OptionItemIdx,                   m_Surface_MenuOptions);        for (int i = 0; i < 3; i++)        {            m_View.Blt(67, 44 + i * 13, m_Surface_MenuOnOff.width / 2, m_Surface_MenuOnOff.height,                       m_i_GameOption[i] * (m_Surface_MenuOnOff.width / 2), 0, m_Surface_MenuOnOff);        }    }    private void DrawHelp1()    {        DrawGameBackGround();        m_View.Blt(9, 11, m_Surface_MenuHelp1.width, m_Surface_MenuHelp1.height, 0,                   0, m_Surface_MenuHelp1);    }    private void DrawHelp2()    {        DrawGameBackGround();        m_View.Blt(9, 11, m_Surface_MenuHelp2.width, m_Surface_MenuHelp2.height, 0,                   0, m_Surface_MenuHelp2);    }    private void DrawManuBackGround()    {        m_BackGraphy.setColor(62, 62, 62);        m_BackGraphy.fillRect(0, 0, m_View.m_i_ScreenWidth, m_View.m_i_ScreenHeight);        m_View.Blt(0, 1, SMALL_BLOCK_SIZE, SMALL_BLOCK_SIZE,                   SMALL_BLOCK_SIZE * 2, 0, m_Surface_SmallRoad);        m_View.Blt(0, 1 + SMALL_BLOCK_SIZE * 8, SMALL_BLOCK_SIZE,                   SMALL_BLOCK_SIZE, SMALL_BLOCK_SIZE * 1, 0, m_Surface_SmallRoad);        for (int i = 1; i < 7; i++)        {            m_View.Blt(SMALL_BLOCK_SIZE * i, 1, SMALL_BLOCK_SIZE,                       SMALL_BLOCK_SIZE, 0, 0, m_Surface_SmallRoad);            m_View.Blt(SMALL_BLOCK_SIZE * i, 1 + SMALL_BLOCK_SIZE * 8,                       SMALL_BLOCK_SIZE, SMALL_BLOCK_SIZE, 0, 0, m_Surface_SmallRoad);        }        m_View.Blt(SMALL_BLOCK_SIZE * 7, 1, SMALL_BLOCK_SIZE, SMALL_BLOCK_SIZE,                   SMALL_BLOCK_SIZE * 3, 0, m_Surface_SmallRoad);        m_View.Blt(SMALL_BLOCK_SIZE * 7, 1 + SMALL_BLOCK_SIZE * 8,                   SMALL_BLOCK_SIZE, SMALL_BLOCK_SIZE, SMALL_BLOCK_SIZE * 4, 0,                   m_Surface_SmallRoad);        for (int i = 1; i < 8; i++)        {            m_View.Blt(SMALL_BLOCK_SIZE * 7, 1 + SMALL_BLOCK_SIZE * i,                       SMALL_BLOCK_SIZE, SMALL_BLOCK_SIZE, 0, 0,                       m_Surface_SmallRoad);            m_View.Blt(0, 1 + SMALL_BLOCK_SIZE * i, SMALL_BLOCK_SIZE,                       SMALL_BLOCK_SIZE, 0, 0, m_Surface_SmallRoad);        }    }    private void DrawMainMenu()    {        m_View.Blt(27, 23, m_Surface_TitleMain.width, m_Surface_TitleMain.height, 0,                   0, m_Surface_TitleMain);        m_View.Blt(32, 74, m_Surface_MenuMain.width / 2, m_Surface_MenuMain.height,                   0, 0, m_Surface_MenuMain);        m_View.Blt(32, 74 + m_Surface_MenuMain.height / 4 * m_i_MenuItemIdx,                   m_Surface_MenuMain.width / 2, m_Surface_MenuMain.height / 4,                   m_Surface_MenuMain.width / 2,                   m_Surface_MenuMain.height / 4 * m_i_MenuItemIdx,                   m_Surface_MenuMain);    }    private void DrawGameBackGround()    {        m_BackGraphy.setColor(62, 62, 62);        m_BackGraphy.fillRect(0, 0, m_View.m_i_ScreenWidth, m_View.m_i_ScreenHeight);        m_BackGraphy.setColor(255, 235, 0);        m_BackGraphy.fillRect(0, 0, m_View.m_i_ScreenWidth, START_POS_Y);        m_BackGraphy.fillRect(0, 0, START_POS_X, m_View.m_i_ScreenHeight);        m_BackGraphy.fillRect(m_View.m_i_ScreenWidth - START_POS_X, 0, m_View.m_i_ScreenWidth, m_View.m_i_ScreenHeight);        m_BackGraphy.fillRect(0, m_View.m_i_ScreenHeight - START_POS_Y, m_View.m_i_ScreenWidth, m_View.m_i_ScreenHeight);    }    //============================END=    //=Saving and Loading Section===================    String m_s_StoreString = "GoGoGoOptions";    public void ReadOptionsRecords()    {        RecordStore rs;        byte[] record;        try        {            rs = RecordStore.openRecordStore(m_s_StoreString, false);            if (rs.getNumRecords() == 0)            {                InitOptionsData();                WriteOptionsRecord();                return;            }            record = rs.getRecord(1);            rs.closeRecordStore();        }        catch (RecordStoreException rse)        {            InitOptionsData();            WriteOptionsRecord();            return;        }        ByteArrayInputStream bais = new ByteArrayInputStream(record);        DataInputStream is = new DataInputStream(bais);        try        {            is.read(m_i_GameOption, 0, OPTION_TOTAL);        }        catch (IOException e)        {            InitOptionsData();            WriteOptionsRecord();            return;        }        rs = null;        record = null;        bais = null;        is = null;        return;    }    public void InitOptionsData()    {        for(int i = OPTION_TOTAL - 1; i >= 0; i --)        {            m_i_GameOption[i] = 0;        }    }    public void WriteOptionsRecord()    {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        DataOutputStream os = new DataOutputStream(baos);        try        {            os.write(m_i_GameOption, 0, OPTION_TOTAL);        }        catch (IOException e)        {            return;        }        byte[] record = baos.toByteArray();        RecordStore rs;        try        {            RecordStore.deleteRecordStore(m_s_StoreString);        }        catch (RecordStoreException rse)        {        }        try        {            rs = RecordStore.openRecordStore(m_s_StoreString, true);            rs.addRecord(record, 0, record.length);            rs.closeRecordStore();        }        catch (RecordStoreException rse)        {            return;        }        rs = null;        record = null;        baos = null;        os = null;    }}

⌨️ 快捷键说明

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