📄 mygame.java
字号:
package myGame.main;
import MyGameMIDlet;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import myGame.gui.MenuCanvas;
import myGame.gui.PopupCanvas;
import myGame.gui.popup.Popup;
import myGame.gui.popup.PopupListener;
/**
* <p>
* The <code>MyGame</code> class represents the MyGame MIDlet game altogether.
* It coordinates the macro state of the application.
* </p>
* <p>
* 这个类收集了一些启动游戏线程和停止游戏,显示帮助菜单等静态方法, 用静态工厂方法来代替构造函数
* </p>
*
* @author YuBingxing
*/
public class MyGame implements PopupListener {
// public static MazeCanvas canvas3D;
private static MyGameMIDlet myMidlet;
public static MazeCanvas myCanvas3D;
/** Current canvas that is displayed */
protected static PopupCanvas m_currentCanvas;
/** Current listener to popups */
protected static PopupListener m_popupListener;
/** Popup instance cache */
protected static Popup m_popupCache;
/** Text box used for collecting user string input */
protected static TextBox m_textBox;
/** Flag of if the game has started */
public static boolean gameStarted = false;
/** Instance of this class, used as PopupListener or CommandListener */
protected static MyGame m_inst;
/** Canvas keycode for left softbutton */
public static final int KEYCODE_LEFT_SOFT = -6;
/** Canvas keycode for right softbutton */
public static final int KEYCODE_RIGHT_SOFT = -7;
/** Canvas keycode for back softbutton */
public static final int KEYCODE_BACK = -11;
// ********************************key*****************************
/** Key for saved game boolean flag (boolean) */
public static final int HAS_SAVED_LOCAL_GAME = 0;
/** Key for audio off (boolean) */
// Negative flag because default boolean = false
public static final int AUDIO_OFF = 1;
/** Key for easy off (boolean) */
public static final int EASY_OFF = 2;
/** Key for normal off (boolean) */
public static final int NORMAL_OFF = 3;
/** Key for hard off (boolean) */
public static final int HARD_OFF = 4;
/** Key for veryhard off (boolean) */
public static final int VERYHARD_OFF = 5;
/** Key for s60 off (boolean) */
public static final int S60_OFF = 6;
/** Key for s120 off (boolean) */
public static final int S120_OFF = 7;
/** Key for s300 off (boolean) */
public static final int S300_OFF = 8;
/** Key for s600 off (boolean) */
public static final int S600_OFF = 9;
/** Key for nolimit off (boolean) */
public static final int NOLIMIT_OFF = 10;
public static final int BGMUSIC1 = 11;
public static final int BGMUSIC2 = 12;
public static final int BGMUSIC3 = 13;
// ******************************key for setup******************************
/** Key for diffcult setup (byte) */
public static final int DIFFICULT_SET = 14;
/** Key for timelimit setup (byte) */
public static final int TIMELIMIT_SET = 15;
/** Key for background music setup (byte) */
public static final int BGMUSIC_SET = 16;
// ******************************key********************************
/** Max number of opponent scores that can be persistent */
public static final int GAMERECORDS_SIZE = 17;
/** First key in gamerecords, device ids (int) */
public static final int GAMEREC_OP_ID = 8;
/** First key in gamerecords, opponent name (char[]) */
public static final int GAMEREC_OP_NAME = GAMEREC_OP_ID + GAMERECORDS_SIZE;
/** First key in gamerecords, player score (int) */
public static final int GAMEREC_MY_SCORE = GAMEREC_OP_NAME
+ GAMERECORDS_SIZE;
/** First key in gamerecords, opponent score (int) */
public static final int GAMEREC_OP_SCORE = GAMEREC_MY_SCORE
+ GAMERECORDS_SIZE;
/** First key in gamerecords, number of games against this opponent (int) */
public static final int GAMEREC_GAME_COUNT = GAMEREC_OP_SCORE
+ GAMERECORDS_SIZE;
/** First key in gamerecords, timestamp (long) */
public static final int GAMEREC_TIMESTAMP = GAMEREC_GAME_COUNT
+ GAMERECORDS_SIZE;
/** First key in gamerecords, saved game data (byte[]) */
public static final int GAMEREC_SAVED_GAME_DATA = GAMEREC_TIMESTAMP
+ GAMERECORDS_SIZE;
/** Nbr of keys */
protected static final int NBR_OF_KEYS = 8 + 10 * GAMERECORDS_SIZE;
/** Represents a game that is only played on this device */
public static int GAME_TYPE_LOCAL = 0;
/** Represents a distributed game where this device acts server */
public static int GAME_TYPE_REMOTE_SERVER = 1;
/** Represents a distributed game where this device acts client */
public static int GAME_TYPE_REMOTE_CLIENT = 2;
/** Current game type */
protected static int m_gameType;
/**
* Returns an instance of <code>MyGame</code>, used internally only for
* listener implementations.
*
* @return A MyGame instance
*/
private static MyGame getInstance() {
return m_inst;
}
/**
* 初始化游戏
*
* @param midlet
* @param display
* @param canvas3D
* @param splash
* @param error
*/
public synchronized static void init(MyGameMIDlet midlet, Display display,
Image splash, Image error) {
myMidlet = midlet;
// myCanvas3D = canvas3D;
Displayable current = Display.getDisplay(midlet).getCurrent();
if (current == null) {
/** 如果查询不到m3g包的版本号,则视作不支持Mobile3D */
boolean isApiAvailable = (System
.getProperty("microedition.m3g.version") != null);
if (!isApiAvailable) {
/** 如果M3GAPI不可用,则退出程序 */
midlet.quitApp();
} else {
/** 否则正常开始游戏 */
Alert splashScreen = new Alert("Maze3D", null, splash,
AlertType.INFO);// 用闪屏图片创建Alert对象
splashScreen.setTimeout(3000);// 再闪屏之后显示游戏菜单
if (m_inst == null) {
/** singleton listeners */
m_inst = new MyGame();
}
/** 载入RMS中的记录 */
RmsFacade.init(NBR_OF_KEYS);
/** 初始化菜单 */
if (MyGame.isShowingPopup()) {
MyGame.getCurrentPopup().dispose();
}
MenuCanvas.getInstance().initShow();
Display.getDisplay(midlet).setCurrent(splashScreen,
MenuCanvas.getInstance());
}
} else {
/** 如果MIDlet是中途挂起,则继续游戏 */
if (current == myCanvas3D) {
myCanvas3D.start();
}
Display.getDisplay(midlet).setCurrent(current);
}
}
/** Prevent external construction */
private MyGame() {
// 打开RMS
// try{
// rs = RecordStore.openRecordStore("myGame", true);
// System.out.println("======" + rs.getNumRecords());
// if(rs.getSize() == 0){
// MyGame.setInt(MyGame.DIFFICULT_SET, 10);
// MyGame.setInt(MyGame.TIMELIMIT_SET, 120);
// MyGame.setInt(MyGame.BGMUSIC_SET, Audio.BGMUSIC1);
// MyGame.getInt(MyGame.DIFFICULT_SET);
// MyGame.getInt(MyGame.TIMELIMIT_SET);
// MyGame.getInt(MyGame.BGMUSIC_SET);
// }
// }catch(RecordStoreException rse){
// System.out.println(rse.toString());
// }
myCanvas3D = new MazeCanvas();
}
/** PopupListener implementation */
public void selectedChoice(byte choice, boolean timeOut) {
/** Just forward to registered listener and repaint */
if (m_popupListener != null)
m_popupListener.selectedChoice(choice, timeOut);
if (getCanvas() != null)
getCanvas().repaint();
}
/** starts a new ga,e */
public static void newGame() {
myCanvas3D.stop(); // 停止游戏线程
myCanvas3D.init(); // 调用游戏画布的初始化方法,重新初始化游戏内容
Display.getDisplay(myMidlet).setCurrent(myCanvas3D); // 将当前屏幕切换到游戏画布中
}
/** switches the canvas' view */
public static void switchView() {
myCanvas3D.switchView(); // 调用游戏画布里的切换视角方法
myCanvas3D.start(); // 重新开始游戏线程,因此俯瞰模式下,定时器也是开动的
Display.getDisplay(myMidlet).setCurrent(myCanvas3D); // 将当前屏幕切换到游戏画布中
}
/** shows the main canvas */
public static void showMain() {
myCanvas3D.start(); // 重新开始游戏线程
Display.getDisplay(myMidlet).setCurrent(myCanvas3D); // 将当前屏幕切换到游戏画布中
}
/**
* Sets current game type, one of <code>GAME_TYPE_LOCAL</code>,
* <code>GAME_TYPE_REMOTE_SERVER</code>,
* <code>GAME_TYPE_REMOTE_CLIENT</code>.
*
* @param gameType
* The current game type.
*/
protected static void setGameType(int gameType) {
m_gameType = gameType;
}
/**
* Returns current game type, one of <code>GAME_TYPE_LOCAL</code>,
* <code>GAME_TYPE_REMOTE_SERVER</code>,
* <code>GAME_TYPE_REMOTE_CLIENT</code>.
*
* @return The current game type.
*/
public static int getGameType() {
return m_gameType;
}
/**
* Returns true if there exists a saved phone game
*
* @return true if saved game exists, false otherwise
*/
public static boolean hasSavedLocalGame() {
return RmsFacade.getBoolean(HAS_SAVED_LOCAL_GAME);
}
/**
* Start a new game
*/
public synchronized static void startNewGame() {
Audio.stopSound(MenuCanvas.bgMusic);
// new maze
MazeCanvas.newMaze();
// new game
newGame();
// add more PageItems the first time the game runs
if (!gameStarted) {
gameStarted = true;
}
}
/**
* Resumes a saved game
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -