游戏结构

2256人阅读 评论(0) 收藏 举报

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();

    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:823197次
    • 积分:9569
    • 等级:
    • 排名:第1868名
    • 原创:120篇
    • 转载:35篇
    • 译文:3篇
    • 评论:1121条
    新浪微博
    最新评论