游戏开发入门 |
| 作者:不详 来源:本站整理 发布时间:2005-11-24 17:09:17 |
|
经常看到许多学习手机游戏开发,对于线程一直不知道该如何控制,下面是我写的一个例子,希望对开发游戏的兄弟有所帮助,有何不妥之处也请指正! GameMIDlet.java import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.lcdui.Display; public class GameMIDlet extends MIDlet { Game game; Display display; public GameMIDlet() { game = new Game(this); display = Display.getDisplay(this); } protected void startApp() throws MIDletStateChangeException { game.setGameState(game.LOGO, 30); game.start(); display.setCurrent(game); } protected void pauseApp() { notifyPaused(); } protected void destroyApp(boolean _boolean) throws MIDletStateChangeException { notifyDestroyed(); } } Game.java import javax.microedition.lcdui.*; import com.nokia.mid.ui.*; /** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2005</p> * * <p>Company: </p> * * @author not attributable * @version 1.0 */ public class Game extends FullCanvas implements Runnable ,CommandListener{ private GameMIDlet gameMIDlet; private boolean runGame;//游戏是否启动 private int fps;//频率 private int gameState = -1; //当前游戏状态 /** * 游戏状态参数 */ public final byte LOGO = 0; public final byte MENU = 1; public final byte ABOUT = 2; public final byte HELP = 3; public final byte WAIT = 4; public final byte LOGINFORM = 5; public final byte RESULT = 6; public final byte LOADING = 7; public final byte REGIST = 8; public final byte GAME = 9; private Image bufferedImage;//双缓冲图片 private Form form; private Command cmdReg, cmdBack; // 注册、登陆、返回、修改按钮对象 private TextField tfAccount, tfPassWord; // 昵称、密码,TextField对象 /** * 默认构造方法 */ public Game(GameMIDlet midlet) { gameMIDlet = midlet; int width = 128; int height = 128; // create image buffer: bufferedImage = Image.createImage(width, height); } protected void keyPressed(int keyCode) { if (keyCode == -5) { setGameState(LOGINFORM, 30); } } /** *设置当前状态 * @param bGameState byte 状态值 * @param newFPS byte 当前状态下的频率 */ public void setGameState(int bGameState,int newFPS){ this.release(); this.gameState = bGameState; this.fps = newFPS; this.init(); } /** * 设置当前状态 * @param bGameState int 状态值 */ public void setGameState(int bGameState){ this.release(); this.gameState = bGameState; this.fps = 30; this.init(); } /** * 得到当前状态 * @return int */ public int getGameState(){ return this.gameState; } /** * 游戏主线程启动 */ public void start() { runGame = true; Thread t = new Thread(this); t.start(); } /** * 游戏主线程停止 */ public void stop() { runGame = false; } /** * 实现Runnable接口 */ public void run() { Graphics g = bufferedImage.getGraphics();//得到Graphics绘图对象 long startTime; int interval = 1000 / this.fps; byte count = 2;//垃圾回收次数控制 byte n = count; Thread currentThread = Thread.currentThread(); while (runGame && currentThread == Thread.currentThread()) { interval = 1000 / this.fps; startTime = System.currentTimeMillis(); this.input(); this.work(); drawScreen(g); if (gameState != LOGINFORM) { repaint();//请求刷新屏幕 serviceRepaints(); } waitSomething(startTime, interval); if (n == 0) { Runtime.getRuntime().gc(); n = count; } n--; } } /** * 线程休眠一段时间 * @param loopStartTime long * @param interval long */ protected void waitSomething(long loopStartTime, long interval) { long time = System.currentTimeMillis() - loopStartTime; if (time < interval) { try { Thread.sleep(interval - time); } catch (InterruptedException e) {} } else { Thread.yield(); } } public void paint(Graphics g) { g.drawImage(this.bufferedImage, 0, 0, Graphics.TOP | Graphics.LEFT); } /** * 当前状态初始化 */ protected void init() { switch (getGameState()) { case LOGINFORM: initLoginForm(); break; default: break; } } /** * 当前状态释放资源 */ protected void release() { switch (getGameState()) { case LOGINFORM: releaseLoginForm(); break; default: break; } Runtime.getRuntime().gc(); } /** * 当前状态下的用户输入 */ protected void input() { } /** * 当前状态下的逻辑处理 */ protected void work() { } /** * 当前状态下的绘制 * @param g Graphics */ protected void drawScreen(Graphics g) { switch (getGameState()) { case LOG g.drawString("aaa",10,50,0); break; default: break; } S |
| [] [返回上一页] [打 印] |
|
文章评论 |
