国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

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

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

  一、游戲的框架

  我們的游戲需要一個通用的游戲框架,也方便以后的開發(fā),但實現(xiàn)一個引擎是復雜的。作為初學者假如要你考慮太多的問題,恐怕會讓你偏離主線,這里只給出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實現(xiàn),因為每個gamecanvas都需要很多的內存空間。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邵阳市| 宽城| 福泉市| 郁南县| 贺兰县| 昭通市| 浏阳市| 山西省| 息烽县| 赣榆县| 博乐市| 凌源市| 贡山| 吐鲁番市| 芦山县| 临洮县| 大方县| 两当县| 屯门区| 阳西县| 仁怀市| 开封县| 会泽县| 石嘴山市| 资源县| 兴义市| 恩平市| 阿尔山市| 讷河市| 格尔木市| 图木舒克市| 县级市| 宁陵县| 华坪县| 荣成市| 弥勒县| 平和县| 阳高县| 格尔木市| 嵊州市| 玛纳斯县|