游戏结构

MIDlet

public class GameMidlet
    extends MIDlet
{
    private PlayCanvas play;

    public static GameMidlet  instance;

  public GameMidlet()
  {

      instance = this;
    play=new PlayCanvas(this);
      }

  protected void pauseApp()
  {
  }

  protected void startApp()
  {
    Displayable cc=Display.getDisplay(this).getCurrent();
    if(cc==null)
      Display.getDisplay(this).setCurrent(play);
    else
      Display.getDisplay(this).setCurrent(cc);
  }

  protected void destroyApp(boolean parm1)
  {
    if(play!=null)
      play.Release();
      play=null;
  }

  public static void quitApp() {

    instance.destroyApp(true);
    instance.notifyDestroyed();
    //System.gc();
  }

}  

 

canvas:

public class PlayCanvas extends Canvas implements Runnable{

   GameMidlet  parent;

   private boolean running;

  private Image imgBody;
  private Graphics gBody;

   private int TIME=100;

   private Thread thread=null;

  private width ,height;

   public PlayCanvas(GameMidlet parent) {

        this.parent = parent;

        width = getWidth();

        height =  getHeight();

       imgBody = Image.createImage(width, height); // 缓冲界面
       gBody = this.imgBody.getGraphics();

    }

   /*

    线程启动

   */

    public void start(){

      if(!running){

        thread = null;
        System.gc();

        running=true;

        thread=new Thread(this);

        thread.start();

      }

  }

  

   /*

    线程停止

   */

   public void stop(){

    if(running){

      running = false;

    }

  }

   /*

    按键处理

  */

    public void keyPressed(int keyCode) {

             switch (keyCode) {
                case KEY_UP_ARROW:

.                   .......................

                 break;

               case KEY_DOWN_ARROW:

              .          .............

                 break;

            .......................

   }

  public void paint(Graphics g){

           if (imgBody != null) {
             g.drawImage(imgBody, 0, 0, Graphics.LEFT | Graphics.TOP); //游戏中的   精灵背景都画在imgBody上。
             }

    }

    public void run(){

        while(running){

           long start = System.currentTimeMillis();

....... //运行一些逻辑和游戏界面的处理

           int time = (int)(System.currentTimeMillis() - start);

           if (time < TIME){
            
             try {

                Thread.sleep(TIME- time);

              }catch (InterruptedException ex) {}

        }else{

        }

   }

  }

    public void Release(){// 退出游戏 释放对象

       thread = null;

       imgBody = null;

       System.gc();

    }

}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=745628


你可能感兴趣的:(游戏)