动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   网页设计 → 阅读文章

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

作者:无从考证 来源:网易学院 
阅读 2713 人次 , 2006-2-15 14:35:00 

  在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) {
    }
}

  程序运行的截图如下:

   总结:本文实现了一个游戏开发中可能用到的时钟程序,代码并不复杂。希望能对大家有所帮助。

  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:深入浅出Java的访问者模式
· 下一篇:使程序的标题条闪烁
· [css]简明教程 样式表的书写格式
· 表格特效代码全集中
· JavaScript对象与数组参考大全(9)
· 在Frontpage2000中定义CSS样式
· 网页配色之黄金分割法


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077