麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

J2ME 2D小游戲入門之游戲的框架

2019-11-17 06:29:02
字體:
供稿:網(wǎng)友

  一、游戲的框架

  我們的游戲需要一個(gè)通用的游戲框架,也方便以后的開發(fā),但實(shí)現(xiàn)一個(gè)引擎是復(fù)雜的。作為初學(xué)者假如要你考慮太多的問題,恐怕會(huì)讓你偏離主線,這里只給出canvas的代碼,不理解可以參看本站的另外一篇系列文章《使用MIDP2.0開發(fā)游戲》。

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實(shí)現(xiàn),因?yàn)槊總€(gè)gamecanvas都需要很多的內(nèi)存空間。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美性生活网站 | 一区二区三区欧美在线 | 黄色毛片一级 | 日韩深夜视频 | 韩日黄色片 | 本站只有精品 | 毛片免费观看视频 | 二区视频 | 国产一区二区三区视频在线观看 | 91精品国产91| 18欧美性xxxx极品hd | 一级黄片毛片免费看 | 久久老司机 | 毛片a级毛片免费播放100 | 欧美在线中文字幕 | 久久精品视频免费观看 | 天天色图片| 日本精品网 | 三人弄娇妻高潮3p视频 | 伊人99re| chengrenzaixian| www视频免费在线观看 | 久久国产不卡 | 国产成人精品午夜视频' | hdhdhdhd19日本人 | 久久亚洲线观看视频 | 亚洲无毛av | 日韩精品一区二 | 日韩视频1 | 亚洲一区二区中文 | 黄污视频在线看 | 国产精品亚洲精品久久 | 欧美a视频在线观看 | 国产精品亚洲综合 | 久久久国产精品视频 | 久夜草| 操网| 欧美高清另类自拍视频在线看 | 1314成人网 | 久久骚| h视频免费观看 |