在java.util包中有一個(gè)TimerTask類,你可以擴(kuò)展這個(gè)類并且實(shí)現(xiàn)他的run()方法,在run()方法中編寫我們的邏輯代碼。如果我們想制作一個(gè)游戲時(shí)鐘,那么非常簡(jiǎn)單我們編寫一個(gè)GameClock類擴(kuò)展TimerTask,GameClock需要維持一個(gè)實(shí)例變量timeLeft,這樣我們就可以記錄游戲剩余的時(shí)間了,在每次run()運(yùn)行的時(shí)候把timeLeft減1就可以了。有時(shí)候我們需要始終暫停以及重新啟動(dòng),這并不復(fù)雜,在GameClock中添加一個(gè)boolean類型的標(biāo)記就可以了。下面給出GameClock的代碼:
/* * GameClock.java * * Created on 2005年7月18日, 上午11:00 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package com.j2medev.gameclock; import java.util.TimerTask; /** * * @author Administrator */ public class GameClock extends TimerTask{ private int timeLeft = 60;//時(shí)鐘的默認(rèn)時(shí)間 private boolean pause = false; /** Creates a new instance of GameClock */ public GameClock() { } public GameClock(int value){ timeLeft = value; } public void run(){ if(!pause){ timeLeft--; } } public void pause(){ pause = true; } public void resume(){ pause = false; } public int getTimeLeft(){ return timeLeft; } public void setTimeLeft(int _value){ this.timeLeft = _value; } } 當(dāng)我們使用這個(gè)時(shí)鐘的時(shí)候,只需要把它的一個(gè)實(shí)例作為參數(shù)傳給Timer的schedule()方法即可。例如
clock = new GameClock(30); timer.schedule(clock,0,1000); 接下來我們編寫一個(gè)簡(jiǎn)單的游戲界面測(cè)試一下時(shí)鐘。我們?cè)诔绦騿?dòng)的時(shí)候開始計(jì)時(shí),每隔一秒鐘timeLeft會(huì)減少1,并且在手機(jī)屏幕上顯示當(dāng)前剩余的時(shí)間。如果timeLeft為0的時(shí)候代表游戲已經(jīng)結(jié)束了。因此我們需要這樣判斷游戲的狀態(tài)。
public void verifyGameState(){ timeLeft = clock.getTimeLeft(); if(timeLeft == 0){ going = false; } }
為了測(cè)試時(shí)鐘的暫停功能,我們接收用戶的按鍵行為,如果左鍵被按下,那么調(diào)用clock的pause()方法,如果右鍵被按下則調(diào)用clock的resume()方法。
public void userInput(){ int keyStates = this.getKeyStates(); if((keyStates & GameCanvas.LEFT_PRESSED) != 0){ clock.pause(); }else if((keyStates & GameCanvas.RIGHT_PRESSED) != 0){ clock.resume(); } } 下面給出MIDlet和Canvas的代碼:
/* * ClockCanvas.java * * Created on 2005年7月18日, 上午11:04 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package com.j2medev.gameclock; import java.util.Timer; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.game.*; /** * * @author Administrator */ public class ClockCanvas extends GameCanvas implements Runnable { private Timer timer = new Timer(); private GameClock clock = null; private boolean going = true; int timeLeft = 0; /** Creates a new instance of ClockCanvas */ public ClockCanvas() { super(false); } public void run(){ clock = new GameClock(30); timer.schedule(clock,0,1000); while(going){ verifyGameState(); userInput(); repaint(); try{ Thread.sleep(100); }catch(Exception e){ e.printStackTrace(); } } } public void userInput(){ int keyStates = this.getKeyStates(); if((keyStates & GameCanvas.LEFT_PRESSED) != 0){ clock.pause(); }else if((keyStates & GameCanvas.RIGHT_PRESSED) != 0){ clock.resume(); } } public void paint(Graphics g){ int color = g.getColor(); g.setColor(0xffffff); g.fillRect(0,0, this.getWidth(), this.getHeight()); g.setColor(color); if(timeLeft == 0){ g.drawString("游戲結(jié)束", this.getWidth()/2, this.getHeight()/4, Graphics.HCENTER|Graphics.BOTTOM); }else{ g.drawString("游戲剩余時(shí)間:"+timeLeft, this.getWidth()/2, this.getHeight()/4, Graphics.HCENTER|Graphics.BOTTOM); } } public void verifyGameState(){ timeLeft = clock.getTimeLeft(); if(timeLeft == 0){ going = false; } } public void start(){ Thread t = new Thread(this); t.start(); } public void stop(){ going = false; } }
/* * TestMidlet.java * * Created on 2005年7月18日, 上午11:00 */ package com.j2medev.gameclock; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * * @author Administrator * @version */ public class TestMidlet extends MIDlet { private Display display = null; public void startApp() { display = Display.getDisplay(this); ClockCanvas canvas = new ClockCanvas(); canvas.start(); display.setCurrent(canvas); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
程序運(yùn)行的截圖如下:
 總結(jié):本文實(shí)現(xiàn)了一個(gè)游戲開發(fā)中可能用到的時(shí)鐘程序,代碼并不復(fù)雜。希望能對(duì)大家有所幫助。
|