游戲框架之心得體會(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://www.49028c.com)
新聞熱點
疑難解答