游戏结构

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

    }

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值