您的位置:首页 > 其它

J2ME 2D小游戏入门之游戏的框架

2008-05-01 05:43 507 查看
一、游戏的框架
我们的游戏需要一个通用的游戏框架,也方便以后的开发,但实现一个引擎是复杂的。作为初学者如果要你考虑太多的问题,恐怕会让你偏离主线,这里只给出canvas的代码,不理解可以参看本站的另外一篇系列文章《使用MIDP2.0开发游戏》。

public class MyGaMECanvas extends GaMECanvas
impleMEnts Runnable, CommandListener{
private static MyGaMECanvas instance;
Graphics g;
boolean running;
Thread t;
Command startcmd,exitcmd,restartcmd;
int keystate;
boolean keyevent;
boolean key_up,key_down,key_left,key_right,key_fire;
private boolean allowinput;
public int screenwidth;
public int screenheight;
boolean gaMEover;
//define your variable here
//define your variable end
protected MyGaMECanvas() {
super(true);
g=getGraphics();
running=false;
t=null;
addCommand(startcmd=new Command("start",Command.OK,1));
addCommand(exitcmd=new Command("exit",Command.EXIT,1));
setCommandListener(this);
screenwidth=getWidth();
screenheight=getHeight();
//put your init once code here
//put your init once code end
}
synchronized public static MyGaMECanvas getInstance() {
if (instance == null) {
instance = new MyGaMECanvas();
System.out.println("new MyGaMECanvas");
}
return instance;
}
public void run(){
System.out.println("MyGaMECanvas run start");
long st=0,et=0,diff=0;
int rate=50;//16-17 fraME per second
while(running){
st=System.currentTiMEMillis();
gaMEinput();
gaMEMain();
et=System.currentTiMEMillis();
diff=et-st;
if(diff
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: