?? gamemidlet.java
字號(hào):
package javagapi;import java.io.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;/** NOTE: Since this game is called Game, this class in not abstract * for this particular game. *//** Remains to implement: progress bar for loading images. * should have last_menu reference for back_cmd. * In order to use this abstract class, a subclass that implements * createCanvas() must be created. The purpose of createCanvas() is * to return a new object that has the type of an appropriate * subclass of GameShell. This means that for each game, there must * be one subclass of GameShell as well as one subclass of GameMIDlet. */public class GameMIDlet extends MIDlet implements CommandListener{ // constants static final Command BACK_CMD = new Command("Back", Command.BACK, 2); static final Command OK_CMD = new Command("OK", Command.OK, 1); GAEvent curEvent; Display display; GameShell canvas; List mainMenu, newGameMenu, difficultyMenu, optionsMenu, highscoreMenu; GAMenuSelectObject_t mso = null; /** Main Game Menu */ int RESUME = -1; int NEW_GAME = -1; int SET_BIRTH_DATE = -1; int OPTIONS = -1; int HIGHSCORE = -1; int HELP = -1; /** Difficulty Menu */ int VERY_EASY = -1; int EASY = -1; int MEDIUM = -1; int HARD = -1; int VERY_HARD = -1; /** New Game Menu */ int SINGLE = -1; int TWO_ON_SAME = -1; /** Options Menu */ // Not supported: //int SOUND = -1; int FORBIDDEN_RULES = -1; int PLAYER_SIDE_BLACK = -1; /** Highscore Menu */ int VIEW_HIGHSCORE = -1; int CLEAR_HIGHSCORE = -1; // Not supported: //int SUBMIT_SCORES = -1; public GameMIDlet() { display = Display.getDisplay(this); curEvent = new GAEvent(); } public void startApp() { doInitMenus(); display.setCurrent(mainMenu); } /** * Returns a new instance of the appropriate subclass of GameShell for * the game that corresponds to a specific subclass of GameMIDlet. * This is abstract, as is GameShell, so it must be implemented by * a non-abstract subclass of GameMIDlet. * * @returns a new instance of the appropriate subclass of GameShell. */ protected GameShell createCanvas() { return new GameGame(); } protected void doInitMenus() { /* there should be some message indicating to wait for images to load possibly even a progress bar. maybe this should be done when menus are finished. */ canvas = createCanvas(); canvas.setMIDlet(this); mso = canvas.GAGetMenuSetting(); if(canvas instanceof BiorhythmGame) mso.birthDate = new StringBuffer(); // Menus are created in startApp since they depend on // the game as well as the phone. // 1. send a GAGetMenuSetting event to game event loop // 2. look at GAMenuSelectObject to find out which menus to draw. // initialize first event (GAGetMenuSetting); curEvent.type = GameShell.GAMenuSettingsEvent; // send first event to game.main loop canvas.gameMain(curEvent); canvas.setCommandListener(this); canvas.addCommand(BACK_CMD); // MAIN MENU int i = 0; mainMenu = new List(canvas.getGameName(), Choice.IMPLICIT); if((mso.AllowStartMode & GameShell.GAM_START_RESUME) != 0) { mainMenu.append("Resume", null); RESUME = i++;} if((mso.AllowStartMode & GameShell.GAM_START_NEW) != 0) { mainMenu.append("New Game", null); NEW_GAME = i++;} if((mso.AllowStartMode & GameShell.GAM_START_SETBIRTHDATE) != 0) { mainMenu.append("Set Birth Date", null); SET_BIRTH_DATE = i++; } if(mso.AllowOptions != 0 && mso.AllowOptions != GameShell.GAM_OPT_SOUND) { mainMenu.append("Options", null); OPTIONS = i++;} if(mso.AllowHighScore != 0) { mainMenu.append("Highscore", null); HIGHSCORE = i++;} mainMenu.append("Help", null); HELP = i++; mainMenu.setCommandListener(this); // DIFFICULTY MENU // nb: The options in the submenus must be generated // depending on the settings retrieved. // one or both of 1. choose players, 2. choose difficulty // ????????? consider AllowSinglelevels and AllowTwoonSameLevels i = 0; difficultyMenu = new List("Difficulty", Choice.IMPLICIT); if((mso.AllowSingleLevels & GameShell.GAM_LEVEL_VERYEASY) != 0) { difficultyMenu.append("Very Easy", null); VERY_EASY = i++;} if((mso.AllowSingleLevels & GameShell.GAM_LEVEL_EASY) != 0) { difficultyMenu.append("Easy", null); EASY = i++;} if((mso.AllowSingleLevels & GameShell.GAM_LEVEL_MEDIUM) != 0) { difficultyMenu.append("Medium", null); MEDIUM = i++;} if((mso.AllowSingleLevels & GameShell.GAM_LEVEL_HARD) != 0) { difficultyMenu.append("Hard", null); HARD = i++;} if((mso.AllowSingleLevels & GameShell.GAM_LEVEL_VERYHARD) != 0) { difficultyMenu.append("Very Hard", null); VERY_HARD = i++;} difficultyMenu.addCommand(BACK_CMD); difficultyMenu.setCommandListener(this); // NEW GAME MENU // is inserted if not only GAM_OPP_PHONE is allowed // this could be changed to "if two on same is allowd" i = 0; if(mso.AllowOpponents!=GameShell.GAM_OPP_PHONE ) { newGameMenu = new List("New Game", Choice.IMPLICIT); if((mso.AllowOpponents & GameShell.GAM_OPP_PHONE) != 0) { newGameMenu.append("Single", null); SINGLE = i++; } if((mso.AllowOpponents & GameShell.GAM_OPP_SAME_PHONE) != 0) { newGameMenu.append("Two On Same", null); TWO_ON_SAME = i++; } newGameMenu.addCommand(BACK_CMD); newGameMenu.setCommandListener(this); } // OPTIONS MENU if(mso.AllowOptions != 0 && mso.AllowOptions != GameShell.GAM_OPT_SOUND) { i = 0; optionsMenu = new List("Options", Choice.IMPLICIT); // GAM_OPT_SOUND: sound is not supported in MIDP 1.0 /*if((mso.AllowOptions & GameShell.GAM_OPT_SOUND) != 0) { newGameMenu.append("Sound", null); SOUND = i++;}*/ //System.out.println("In the beginning: "+(mso.AllowOptions & GameShell.GAM_OPT_FORBIDDEN_RULES)); if((mso.AllowOptions & GameShell.GAM_OPT_FORBIDDEN_RULES)!=0) { String str; if((mso.SelOptions & GameShell.GAM_OPT_FORBIDDEN_RULES)==0) str = "Forbidden Rules Off"; else str = "Forbidden Rules On"; optionsMenu.append(str,null); FORBIDDEN_RULES = i++; } if((mso.AllowOptions & GameShell.GAM_OPT_PLAYER_SIDE_BLACK)!=0 || (mso.AllowOptions & GameShell.GAM_OPT_PLAYER_SIDE_WHITE)!=0) { String str = "Player Side ???"; if((mso.SelOptions & GameShell.GAM_OPT_PLAYER_SIDE_BLACK)!=0) str = "Player Side Black"; else if((mso.SelOptions & GameShell.GAM_OPT_PLAYER_SIDE_WHITE)!=0) str = "Player Side White"; optionsMenu.append(str,null); PLAYER_SIDE_BLACK = i++; } optionsMenu.addCommand(BACK_CMD); optionsMenu.setCommandListener(this); } // HIGHSCORE MENU if(mso.AllowHighScore != 0) { i = 0; highscoreMenu = new List("Highscore", Choice.IMPLICIT); if((mso.AllowHighScore & GameShell.GAM_HIGHSCORE_VIEW) != 0) { highscoreMenu.append("View Highscore", null); VIEW_HIGHSCORE = i++;} if( (mso.AllowHighScore & GameShell.GAM_HIGHSCORE_CLEAR) != 0 && (mso.AllowHighScore & GameShell.GAM_HIGHSCORE_LIST_EMPTY) == 0) { highscoreMenu.append("Clear Highscore", null); CLEAR_HIGHSCORE = i++;} /*if((mso.AllowHighScore & GameShell.GAM_HIGHSCORE_SEND) != 0) { highscoreMenu.append("Submit Scores", null); SUBMIT_SCORES = i++;}*/ highscoreMenu.addCommand(BACK_CMD); highscoreMenu.setCommandListener(this); } } public void commandAction(Command c, Displayable s) { if(s instanceof TextBox) { TextBox tebo = (TextBox) s; if(c == BACK_CMD) { display.setCurrent(mainMenu); } else if(c == OK_CMD) { mso.birthDate.delete(0,mso.birthDate.length()); mso.birthDate.append(tebo.getString()); canvas.loadImages(); mso.SelStartMode = GameShell.GAM_START_SETBIRTHDATE; curEvent.type = GameShell.GAStartEvent; display.setCurrent(canvas); canvas.gameMain(curEvent); canvas.startThread(); } } else if(s instanceof List) { List l = (List) s; if(l == mainMenu){ if(c == List.SELECT_COMMAND) { int idx = l.getSelectedIndex(); //System.out.println(idx); if( idx == RESUME) { canvas.loadImages(); mso.SelStartMode = GameShell.GAM_START_RESUME; curEvent.type = GameShell.GAStartEvent; display.setCurrent(canvas); canvas.gameMain(curEvent); canvas.startThread(); } else if( idx == NEW_GAME ) { if( mso.AllowOpponents == GameShell.GAM_OPP_PHONE ) display.setCurrent(difficultyMenu); else display.setCurrent(newGameMenu); } else if( idx == SET_BIRTH_DATE ) { TextBox tb = new TextBox("Please enter your birth date", mso.birthDate.toString(), 10, TextField.ANY); tb.addCommand(BACK_CMD); tb.addCommand(OK_CMD); tb.setCommandListener(this); display.setCurrent(tb); } else if( idx == OPTIONS ) { display.setCurrent(optionsMenu); } else if( idx == HIGHSCORE ) { display.setCurrent(highscoreMenu); } else if( idx == HELP ) {} } } else if(l == difficultyMenu){ if(c == List.SELECT_COMMAND) { int idx = l.getSelectedIndex(); if(idx == VERY_EASY) { mso.SelLevel = GameShell.GAM_LEVEL_VERYEASY; } else if(idx == EASY) { mso.SelLevel = GameShell.GAM_LEVEL_EASY; } else if( idx == MEDIUM ) { mso.SelLevel = GameShell.GAM_LEVEL_MEDIUM; } else if( idx == HARD ) { mso.SelLevel = GameShell.GAM_LEVEL_HARD; } else if( idx == VERY_HARD ) { mso.SelLevel = GameShell.GAM_LEVEL_VERYHARD; } canvas.loadImages(); mso.SelStartMode = GameShell.GAM_START_NEW; curEvent.type = GameShell.GAStartEvent; display.setCurrent(canvas); canvas.gameMain(curEvent); canvas.startThread(); } else if(c == BACK_CMD) { // will have to modify this, if nr of players menu is available. if(newGameMenu!=null) display.setCurrent(newGameMenu); else display.setCurrent(mainMenu); } } else if(l == optionsMenu){ if(c ==List.SELECT_COMMAND) { int idx = l.getSelectedIndex(); if(idx == FORBIDDEN_RULES) { //System.out.println(mso.SelOptions & GameShell.GAM_OPT_FORBIDDEN_RULES); if((mso.SelOptions & GameShell.GAM_OPT_FORBIDDEN_RULES) ==0) { mso.SelOptions |= GameShell.GAM_OPT_FORBIDDEN_RULES; l.set(idx,"Forbidden Rules On",null); } else { mso.SelOptions &= ~GameShell.GAM_OPT_FORBIDDEN_RULES; l.set(idx,"Forbidden Rules Off",null); } } else if(idx == PLAYER_SIDE_BLACK) { if((mso.SelOptions & GameShell.GAM_OPT_PLAYER_SIDE_BLACK) != 0) { mso.SelOptions &= ~GameShell.GAM_OPT_PLAYER_SIDE_BLACK; mso.SelOptions |= GameShell.GAM_OPT_PLAYER_SIDE_WHITE; l.set(idx,"Player Side White",null); } else if((mso.SelOptions & GameShell.GAM_OPT_PLAYER_SIDE_WHITE) != 0) { mso.SelOptions |= GameShell.GAM_OPT_PLAYER_SIDE_BLACK; mso.SelOptions &= ~GameShell.GAM_OPT_PLAYER_SIDE_WHITE; l.set(idx,"Player Side Black",null); } } } else if(c == BACK_CMD) { display.setCurrent(mainMenu); } } else if(l == highscoreMenu){ if(c == List.SELECT_COMMAND) { int idx = l.getSelectedIndex(); if(idx == VIEW_HIGHSCORE) { curEvent.ClearHighScore = false; curEvent.ViewHighScore = true; } else if(idx == CLEAR_HIGHSCORE) { curEvent.ClearHighScore = true; curEvent.ViewHighScore = false; } curEvent.type = GameShell.GAHighScoreEvent; if(canvas.gameMain(curEvent)) { display.setCurrent(canvas); canvas.startThread(); } else { curEvent.type = GameShell.GATerminateEvent; canvas.gameMain(curEvent); doInitMenus(); display.setCurrent(highscoreMenu); } } else if(c == BACK_CMD) { display.setCurrent(mainMenu); } } else if(l == newGameMenu){ if(c == List.SELECT_COMMAND) { int idx = l.getSelectedIndex(); if(idx == SINGLE) { mso.SelOpponents = GameShell.GAM_OPP_PHONE; if(difficultyMenu!=null) display.setCurrent(difficultyMenu); } else if(idx == TWO_ON_SAME) { mso.SelOpponents = GameShell.GAM_OPP_SAME_PHONE; if(mso.AllowTwoOnSameLevels==GameShell.GAM_LEVEL_NONE) { canvas.loadImages(); mso.SelStartMode = GameShell.GAM_START_NEW; curEvent.type = GameShell.GAStartEvent; display.setCurrent(canvas); canvas.gameMain(curEvent); canvas.startThread(); } //else ????????????? } } else if(c == BACK_CMD) { display.setCurrent(mainMenu); } } } else if(s instanceof GameShell) { GameShell gs = (GameShell) s; if(c == BACK_CMD) { //System.out.println("!!!!!!!!!!!!!!CALLING gs.handleBackCommand"); gs.handleBackCommand(); //System.out.println("!!!!!!!!!!!!!!RETURNING FROM gs.handleBackCommand"); } } //System.out.println("GameMIDlet.commandAction() returns now!"); } public void pauseApp() { } public void destroyApp(boolean unconditional) { curEvent.type = GameShell.GATerminateEvent; canvas.gameMain(curEvent); display.setCurrent(null); } public void returnToMenu() { canvas = null; mso = null; System.gc(); doInitMenus(); display.setCurrent(mainMenu); }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -