The simple realization of clock in J2ME game development

Source: Internet
Author: User
Tags end implement thread
Clock in the game development, sometimes we need a clock to record the game time, if the end of time to end the game. This article describes how to use timer and timertask to implement such a clock in J2ME and give specific code examples.

There is a TimerTask class in the Java.util package that you can extend this class and implement his run () method to write our logical code in the Run () method. If we want to make a game clock, then very simple we write a gameclock class extension timertask,gameclock need to maintain an instance variable timeleft so that we can record the rest of the game time, at each run () It is OK to reduce the timeleft by 1 when running. Sometimes we need to always pause and reboot, it's not complicated, it's OK to add a Boolean type tag to Gameclock. Here's the code for Gameclock:

/*
* Gameclock.java
*
* Created on July 18, 2005, 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 = default time for 60;//clock
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;
}
}

When we use this clock, we simply pass one instance of it to the timer's schedule () method. For example

Clock = new Gameclock (30);
Timer.schedule (clock,0,1000);

Next we write a simple game interface to test the clock. We start the timer at the start of the program, reduce the Timeleft by 1 every second, and display the current remaining time on the phone screen. If Timeleft is 0, the game is over. So we need to judge the state of the game in this way.

public void Verifygamestate () {
Timeleft = Clock.gettimeleft ();
if (Timeleft = = 0) {
going = false;
}
}

In order to test the pause function of the clock, we receive the user's button behavior, if the left button is pressed, then call the clock Pause () method, if the right button is pressed then call the clock Resume () method.

public void Userinput () {
int keystates = This.getkeystates ();
if ((Keystates & gamecanvas.left_pressed)!= 0) {
Clock.pause ();
}else if ((Keystates & gamecanvas.right_pressed)!= 0) {
Clock.resume ();
}

}

Here's the code for MIDlet and canvas:

/*
* Clockcanvas.java
*
* Created on July 18, 2005, 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 ("Game Over", This.getwidth ()/2, This.getheight ()/4, graphics.hcenter| Graphics.bottom);
}else{
g.DrawString ("Game remaining time:" +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 July 18, 2005, 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) {
}
}

The screenshot of the program running is as follows:

Summary: This article realizes a game development may use the clock program, the code is not complex. Hope to be helpful to everyone.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.