游戏框架之心得体会(2)
2007-12-23 12:29:25 来源:WEB开发网 游戏框架之心得体会(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.cncms.com)
- ››游戏定位成牵引Android发展的火车头
- ››游戏场景烘焙贴图制作
- ››游戏植物大战僵尸 iPhone 版9天销售额超100万美元...
- ››游戏开发包 iPhone cocos2d 初体验
- ››游戏三维怪物施法特效教程
- ››游戏人生Silverlight(2) - 趣味钢琴[Silverlight ...
- ››游戏人生Silverlight(3) - 打苍蝇[Silverlight 2....
- ››游戏人生Silverlight(4) - 连连看[Silverlight 2....
- ››游戏人生Silverlight(5) - 星际竞技场[Silverligh...
- ››游戏《封神榜》人物纣王3DMAX制作全过程
- ››游戏加速器中的领头羊-网际速递
- ››游戏引擎Valve Source奔向Linux
赞助商链接