国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

游戲框架之心得體會(2)

2019-11-18 16:17:40
字體:
來源:轉載
供稿:網友

        游戲框架之心得體會(2)
       上次寫到的合金代表框架,所有的框架內容都集中在1個類中。
也就是說大眾的游戲(商業)的大都是這樣的寫法,為了節省空間!
        今天要介紹的另一個框架體系是以Funmobile公司的代碼為代表的,各個功能版塊分別個為一個類,另外強烈推薦
       這種代碼的另一個優點是混合了高級UI和低級UI,個人認為游戲對菜單界面的美觀程度要求不大。而且這種寫發很利于
應用代碼的書寫!

 MSN alfylove@hotmail.com


下面還是老習慣 先看我截取的代碼

游戲都是那幾個部分

        開始游戲
        關于
        游戲幫助
        最高分
        游戲設置
        游戲菜單

//主菜單 以下所有類都滿足默認構造函數和按鍵控制
public class NokiaUI_menu extends List
    implements CommandListener{
  NokiaUI_menu(RollerMIDlet pMidlet, RollerLogic pLogic)
    {
        super("Menu", 3); }
  public void commandAction (Command pCommand, Displayable pDisplay)
    {}                           }
   
 //最高分  
public class NokiaUI_hiscore extends Form
    implements CommandListener{}
 //游戲幫助
  public class NokiaUI_help extends Form
    implements CommandListener {}
 //游戲關于
     public class NokiaUI_about extends Form
    implements CommandListener{}
 //游戲設置
 public class NokiaUI_setting extends Form
    implements CommandListener{}
  //游戲暫停 實際是游戲中菜單
 public class NokiaUI_pause extends List
    implements CommandListener{}
   
   
  RollerMIDlet.java 
 
  /*在生命控制程序中所有的類都在此集合,實例化。
  這種方法相當便利了各個類的調用*/
 
 
public class RollerMIDlet extends MIDlet

 
 
public RollerMIDlet()
    {
        mLogic = new RollerLogic(this);
        mNokia = new NokiaUI_menu(this, mLogic);
        mSetting = new NokiaUI_setting(this, mLogic);
        mHiScore = new NokiaUI_hiscore(this, mLogic);
        mHelp = new NokiaUI_help(this);
        mAbout = new NokiaUI_about(this);
        mPause = new NokiaUI_pause(this, mLogic);
    }


    PRotected void startApp()
        throws MIDletStateChangeException
    {
        if(mLogic != null)
        {
            Display.getDisplay(this).setCurrent(mLogic);
            mLogic.start();
        }
    }

    protected void pauseApp()
    {
        if(mLogic != null)
            mLogic.stop();
    }

    protected void destroyApp(boolean p0)
    {
        if(mLogic != null)
            mLogic.stop();
    }

    void StartGame()
    {
        if(mLogic != null)
            mLogic.NewGame();
    }

    void QuitGame()
    {
        destroyApp(false);
        notifyDestroyed();
    }

//轉控分支 

   public void ToNokiaUI()
    {
        if(mNokia != null)
            Display.getDisplay(this).setCurrent(mNokia);
    }

    public void ToNokiaUI_Setting()
    {
        if(mSetting != null)
            Display.getDisplay(this).setCurrent(mSetting);
    }

    public void ToNokiaUI_HiScore()
    {
        mHiScore = null;
        mHiScore = new NokiaUI_hiscore(this, mLogic);
        if(mHiScore != null)
            Display.getDisplay(this).setCurrent(mHiScore);
    }

    public void ToNokiaUI_Help()
    {
        if(mHelp != null)
            Display.getDisplay(this).setCurrent(mHelp);
    }

    public void ToNokiaUI_About()
    {
        if(mAbout != null)
            Display.getDisplay(this).setCurrent(mAbout);
    }


    public void ToNokiaUI_Pause()
    {
        if(mPause != null)
            Display.getDisplay(this).setCurrent(mPause);
    }

    public void ReturnUI()
    {
        if(mLogic != null)
            Display.getDisplay(this).setCurrent(mLogic);
    }

    private RollerLogic mLogic;
    private NokiaUI_menu mNokia;
    private NokiaUI_setting mSetting;
    private NokiaUI_hiscore mHiScore;
    private NokiaUI_help mHelp;
    private NokiaUI_about mAbout;
    private NokiaUI_pause mPause;
}

RollerLogic.java


//中控部分
//游戲的驅動所在,線程控制生命循環。


public class RollerLogic extends FullCanvas
    implements Runnable
{

    RollerLogic(RollerMIDlet pMidlet)
    {
        lThread = null;
        maxsprite = 5;
        sprite = new Image[maxsprite];
        lMidlet = pMidlet;
        lWidth = getWidth();
        lHeight = getHeight();
        lCanvas = new RollerCanvas(this);
        Stage = 0;
        m_bSoundOn = true;
        m_bVibrationOn = true;
    }

    public synchronized void start()
    {
        if(lThread == null)
        {
            lThread = new Thread(this);
            lThread.start();
        }
    }

    public synchronized void stop()
    {
        lThread = null;
    }

    public void run()
    {
        Thread pThreadTemp = Thread.currentThread();
        do
        {
            if(pThreadTemp != lThread)
                break;
            long lTimeStart = System.currentTimeMillis();
            System.out.println("run");
            repaint(0, 0, lWidth, lHeight);
            serviceRepaints();
            long lTimeTaken = System.currentTimeMillis() - lTimeStart;
            if(lTimeTaken < (long)60)
                try
                {
                    synchronized(this)
                    {
                        Thread.sleep((long)60 - lTimeTaken);
                    }
                }
                catch(InterruptedException e)
                {
                    System.out.println("error=".concat(String.valueOf(String.valueOf(e))));
                }
        } while(true);
    }


    public void paint(Graphics g)
    {
        lCanvas.paint(g);
        System.out.println("paint");
    }

    public void keyPressed(int iKeyCode)
    {
        int _tmp = Stage;
        lCanvas.keyPressed(iKeyCode);
    }

    public void keyReleased(int iKeyCode)
    {
        lCanvas.keyReleased(iKeyCode);
    }

    static int rand_no(int iRange)
    {
        int r = rand.nextInt() % iRange;
        if(r < 0)
            r = -r;
        return r;
    }

    public void NewGame()
    {
        start();
        lCanvas.InitStage(1);
    }

    public void UI_Newgame()
    {
        NewGame();
    }

    public void UI_Title()
    {
        start();
        lCanvas.reset();
    }

    public void UI_Game()
    {
        start();
    }

    public void menu()
    {
        lMidlet.ToNokiaUI();
        stop();
    }

    public void pause_menu()
    {
        lMidlet.ToNokiaUI_Pause();
        stop();
    }

    public int getCanvas()
    {
        return lCanvas.showtitle;
    }

    public void ExitGame()
    {
        lMidlet.QuitGame();
    }

    private void Execute()
    {
        int _tmp = Stage;
    }

    public static int Stage;
    public static final int Stage_Loading = 0;
    public static final int Stage_Logo = 10;
    public static final int Stage_Play = 50;
    private static final int up = -1;
    private static final int down = -2;
    private static final int left = -3;
    private static final int right = -4;
    private static final int center = -5;
    private static final int left_key = -6;
    private static final int right_key = -7;
    private static final int num_up = 50;
    private static final int num_down = 56;
    private static final int num_left = 52;
    private static final int num_right = 54;
    private static final Random rand = new Random(System.currentTimeMillis());
    private Thread lThread;
    private RollerMIDlet lMidlet;
    private RollerCanvas lCanvas;
    private GameRecord lRecord;
    public GameEffect m_pEffect;
    private static final int NO_SAVEDATA = 4;
    private static final int LENGTH_SAVEDATA = 40;
    public static int lWidth;
    public static int lHeight;
    private static String RecordValue;
    public boolean m_bSoundOn;
    public boolean m_bVibrationOn;
    public int m_iTopScore;
    int showsplash;
    int maxsprite;
    Image sprite[];


}

從截取代碼來看 ,這個游戲的框架更為清晰,更便于修改。

       總結就是
      
各個單元分別是1個單獨的類!

每個類都擁有自己的初始化內容和按鍵控制

由一個主類控制(RollerLogic),此類也是游戲循環的動力。

顯示部分交給Midlet(RollerMIDlet)。

 

這種方法可以方便的把低級UI和高級UI結合使用,雖然用不到1張圖中,但交替使用效果也不錯。
唯一的不足是類太多,占用了不必要的空間。

(出處:http://m.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滦平县| 徐闻县| 太仆寺旗| 奉化市| 稻城县| 九龙城区| 大港区| 莱芜市| 泸水县| 贡山| 凤冈县| 宁夏| 鹿泉市| 肥西县| 兴宁市| 嵩明县| 福海县| 黎平县| 凌云县| 铜鼓县| 晋江市| 昌都县| 武城县| 林西县| 三穗县| 宝坻区| 土默特左旗| 前郭尔| 阳东县| 蛟河市| 荣昌县| 周至县| 通道| 噶尔县| 平远县| 枞阳县| 平山县| 五华县| 嘉峪关市| 和平县| 马关县|