服务热线:13616026886

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

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

j2me 2d小游戏入门之游戏的框架


  一、游戏的框架

  我们的游戏需要一个通用的游戏框架,也方便以后的开发,但实现一个引擎是复杂的。作为初学者如果要你考虑太多的问题,恐怕会让你偏离主线,这里只给出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<rate){
     //system.out.println("sleep "+(rate-diff));
     try {
      thread.sleep(rate - diff);
     }
     catch (interruptedexception ex) {}
    }else{
     //system.out.println("rush , and the frame using time: "+diff);
    }
   }
   system.out.println("mygamecanvas run end");
  }
  public void start(){
   if(!running){
    running=true;
    t=new thread(this);
    t.start();
   }
  }
  private void gamemain() {
   g.setcolor(0,0,0);//clear screen
   g.fillrect(0,0,getwidth(),getheight());
   flushgraphics();
  }
  private void gameinit() {
   gameover=false;
   allowinput=true;
   key_up=key_down=key_left=key_right=key_fire=false;
  }
  public void stop(){
   if(running){
    running = false;
   }
  }
  public void commandaction(command c, displayable d) {
   string cmdstr=c.getlabel();
   if(cmdstr.equals("start")){
    gameinit();
    start();
    removecommand(startcmd);
    addcommand(restartcmd=new command("restart",command.ok,1));
   }else if(cmdstr.equals("restart")){
    stop();
    while(t.isalive());
     gameinit();
    start();
   }else if(cmdstr.equals("exit")){
    stop();
    navigate.midlet.destroyapp(false);
    navigate.midlet.notifydestroyed();
   }
  }
  private void gameinput() {
   if(allowinput){
    keystate=getkeystates();
    keyevent=false;
    if((keystate & up_pressed)!=0){//up
     key_up=true;keyevent=true;
     //deal your unstop job code here
     //system.out.println("up press");
     //deal your unstop job code end
    }else if((keystate & up_pressed)==0){//release key
     if(key_up==true){
      key_up=false;
      //deal your one press-one job code here
      //system.out.println("up release");
      //deal your one press-one job code end
     }
    }
    if((keystate & down_pressed)!=0){//down
     key_down=true;keyevent=true;
     //deal your unstop job code here
     //system.out.println("down press");
     //deal your unstop job code end
    }else if((keystate & down_pressed)==0){//release key
     if(key_down==true){
      key_down=false;
      //deal your one press-one job code here
      //system.out.println("down release");
      //deal your one press-one job code end
     }
    }
   if((keystate & left_pressed)!=0){//left
    key_left=true;keyevent=true;
    //deal your unstop job code here
    //system.out.println("left press");
    //deal your unstop job code end
   }else if((keystate & left_pressed)==0){//release key
    if(key_left==true){
     key_left=false;
     //deal your one press-one job code here
     //system.out.println("left release");
     //deal your one press-one job code end
    }
   }
   if((keystate & right_pressed)!=0){//right
    key_right=true;keyevent=true;
    //deal your unstop job code here
    //system.out.println("right press");
    //deal your unstop job code end
   }else if((keystate & right_pressed)==0){//release key
    if(key_right==true){
     key_right=false;
     //deal your one press-one job code here
     //system.out.println("right release");
     //deal your one press-one job code end
    }
   }
  if((keystate & fire_pressed)!=0){//fire
   key_fire=true;keyevent=true;
   //deal your unstop job code here
   //system.out.println("fire press");
   //deal your unstop job code end
  }else if((keystate & fire_pressed)==0){//release key
   if(key_fire==true){
    key_fire=false;
    //deal your one press-one job code here
    //system.out.println("fire release");
    //deal your one press-one job code end
   }
  }
  if(!keyevent){
   //no keyevent here
   //system.out.println("no key press");
   //no keyevent end
  }
 }
}

public static void cleanjob(){
 instance=null;
}

}

  使用singlon实现,因为每个gamecanvas都需要很多的内存空间。另外对我们来说,只要改写gameinit(),gamemain(),一次性初始化的代码写在构造函数中。

扫描关注微信公众号