日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

J2ME游戏开发中时钟的简单实现

發布時間:2025/3/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 J2ME游戏开发中时钟的简单实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在java.util包中有一個TimerTask類,你可以擴展這個類并且實現他的run()方法,在run()方法中編寫我們的邏輯代碼。如果我們想制作一個游戲時鐘,那么非常簡單我們編寫一個GameClock類擴展TimerTask,GameClock需要維持一個實例變量timeLeft,這樣我們就可以記錄游戲剩余的時間了,在每次run()運行的時候把timeLeft減1就可以了。有時候我們需要始終暫停以及重新啟動,這并不復雜,在GameClock中添加一個boolean類型的標記就可以了。下面給出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;//時鐘的默認時間
??? 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;
??? }
}

  當我們使用這個時鐘的時候,只需要把它的一個實例作為參數傳給Timer的schedule()方法即可。例如

??????clock = new GameClock(30);
??????timer.schedule(clock,0,1000);

  接下來我們編寫一個簡單的游戲界面測試一下時鐘。我們在程序啟動的時候開始計時,每隔一秒鐘timeLeft會減少1,并且在手機屏幕上顯示當前剩余的時間。如果timeLeft為0的時候代表游戲已經結束了。因此我們需要這樣判斷游戲的狀態。

??? public void verifyGameState(){
??????? timeLeft = clock.getTimeLeft();
??????? if(timeLeft == 0){
??????????? going = false;
??????? }
??? }

  為了測試時鐘的暫停功能,我們接收用戶的按鍵行為,如果左鍵被按下,那么調用clock的pause()方法,如果右鍵被按下則調用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("游戲結束", this.getWidth()/2, this.getHeight()/4, Graphics.HCENTER|Graphics.BOTTOM);
??????? }else{
??????????? g.drawString("游戲剩余時間:"+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) {
??? }
}

  程序運行的截圖如下:

javascript:resizepic(this) border=0>

   總結:本文實現了一個游戲開發中可能用到的時鐘程序,代碼并不復雜。希望能對大家有所幫助。

轉載于:https://www.cnblogs.com/java123/archive/2009/06/18/1505670.html

總結

以上是生活随笔為你收集整理的J2ME游戏开发中时钟的简单实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。