【IT168 技术文档】游戏设计中的状态转换是一个既是非常重要又十分复杂的部分。我前几天做移植的时候看到一个public void keyPressed(int keyCode)中间竟然有着近2000千行的代码,这样的话冗长的代码不仅不符合OO的思更加严重的是会打扰我们的程序思路。这两天运用State模式做了一个J2ME游戏中的状态转换由于只是一个实验的例子所以只做了开始菜单和帮助的转换,在这个框架下再加入其他的状态实际就是十分的简单的了。
现在DEMO中只有两个状态,两种状态下keyPressed和paint的功能是不同的,我们用状态模式来实现各种状态下的keyPressed和paint的功能功能。
首先定义抽象状态State接口,这个接口指定了keyPressed和paint的行为
public interface State { public void onKeyPressed(int keyCode,StateManage _current); public void paint(Graphics g); }
State接口keyPressed和paint方法,我们实现两个子类HelpMenuState和MainMenuState,分别表示主菜单状态和HELP菜单的状态,并实现具体的onKeyPressed和paint方法:
public class MainMenuState implements State { /**省略变量的声明和初使化部分*/ public void onKeyPressed(int keyCode, StateManage _current) { switch (whichMenu) { case 0: mainMenuKeyPressed(keyCode,_current); break; case 1: break; } } private void mainMenuKeyPressed(int keyCode,StateManage _current) { switch (keyCode) { case 1: //KEY_UP = 1; menuIndex = menuIndex <= 0 ? menuMain.length - 1 : --menuIndex; break; case 6://KEY_DOWN = 6; menuIndex = menuIndex >= menuMain.length - 1 ? 0 : ++menuIndex; break; case 8://KEY_MIDDLE = 8; if (menuIndex == 2) _current.setState(new HelpMenuState()); break; } } public void paint(Graphics g) { switch (whichMenu) { case 0: drawMainMenu(g); break; } } private void drawMainMenu(Graphics g) { g.drawImage(imgMenu, 0, 0, 0); g.setColor(0, 0, 0); g.setFont(GameInfo.FONT_LARGE); for (int i = 0; i < menuMain.length; i++) g.drawString(menuMain[i], 120, 70 + i * 20, Graphics.TOP | Graphics.HCENTER); g.drawImage(imgIndex, 60, 65 + 20 * menuIndex, 0); //画index图 g.setColor(255, 0, 0); g.drawString(menuMain[menuIndex], 120, 70 + 20 * menuIndex, Graphics.TOP | Graphics.HCENTER); } } public class HelpMenuState implements State { /**省略变量的声明和初使化部分*/ public void onKeyPressed(int keyCode, StateManage _current) { switch (whichMenu) { case 0: helpMenuKeyPressed(keyCode, _current); break; case 1: break; } } public void helpMenuKeyPressed(int keyCode, StateManage _current) { switch (keyCode) { case 1: //KEY_UP = 1; menuIndex = menuIndex <= 0 ? helpMenuString.length - 1 : --menuIndex; break; case 6://KEY_DOWN = 6; menuIndex = menuIndex >= helpMenuString.length - 1 ? 0 : ++menuIndex; break; case 8://KEY_MIDDLE = 8; if (menuIndex == 4) { _current.setState(new MainMenuState()); } break; } } public void paint(Graphics g) { drawHelp(g); } private void drawHelp(Graphics g) { for (int i = 0; i <= 15; i++) { for (int j = 0; j <= 10; j++) { g.drawImage(img, 0 + 24 * i, 0 + 24 * j, 0); } } g.setColor(251, 220, 48); g.setFont(GameInfo.FONT_LARGE); g.drawString("<< HELP >>", 120, 40, Graphics.TOP | Graphics.HCENTER); for (int i = 0; i < helpMenuString.length; i++) g.drawString(helpMenuString[i], 120, 80 + i * 25, Graphics.TOP | Graphics.HCENTER); g.setColor(255, 0, 0); g.drawString(helpMenuString[menuIndex], 120, 80 + 25 * menuIndex, Graphics.TOP | Graphics.HCENTER); } } 最后,定义StateManage,StateManage的实例作为状态转换的控制器: public class StateManage { State _current; public StateManage(){ _current = new MainMenuState(); //以主菜单开始 }