服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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


  在游戏开发中,有时候我们需要一个时钟来记录游戏的时间,如果时间结束则结束游戏。本文介绍如何在j2me中使用timer和timertask来实现这样一个时钟,并给出具体代码实例。
      幸运好时机,注册赢手机  
      2005 三星yepp夏季数码旅游风

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

  程序运行的截图如下:

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

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

扫描关注微信公众号