⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hunt_midlet.java

📁 类似于推箱子的手机游戏源码
💻 JAVA
字号:
/*
 * Hunt_Midlet.java
 *
 * Created on 2007年10月19日, 下午2:41
 */


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;

public class Hunt_Midlet extends MIDlet implements Runnable{
    private Hunt_Canvas gamecanvas;
    private boolean initDone = false;//标记初始化状态
    private Splash splash;
    private MenuList menuList;
    private Instruction intro;
    private static final String RS_NAME = "MANRecord";
    private boolean hasBestRecord = false;
    private int bestRecord;
    
    
    public Hunt_Midlet(){
        splash = new Splash(this);//初始化开始界面
    }
    
    public void startApp() {
        Displayable current = Display.getDisplay(this).getCurrent();  //获取当前的显示屏幕
        if(current == null){  //如果没有任何屏幕被显示
            Display.getDisplay(this).setCurrent(splash);  //显示开始画面  
        }else{
            if(current == gamecanvas){
                gamecanvas.start();
            }
            Display.getDisplay(this).setCurrent(current);            
        }   
        
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }

    public void splashScreenPainted(){
        new Thread(this).start();//要实现Runnable接口,
    }

    public void run() {
        init();             //初始化
    }
    
     private synchronized void init(){
        if(!initDone){           
            readRecordStore();
            menuList = new MenuList(this);    //创建游戏菜单
            gamecanvas = new Hunt_Canvas(this);  //创建游戏画布
            initDone = true;                   //初始化完毕,设置初始化标志位
        }
    }

    public void splashScreenDone() {
        init();
        Display.getDisplay(this).setCurrent(menuList);
    }
    
    public void MenuListQuit(){
       destroyApp(false);//非强制销毁
       notifyDestroyed();//回收系统资源
    }
    
    public void MenuListContinue(){
         Display.getDisplay(this).setCurrent(gamecanvas);       
         gamecanvas.continuegame();
    }
    
    public void MenuListNewGame(){
        //gamecanvas = new Hunt_Canvas(this);
        Display.getDisplay(this).setCurrent(gamecanvas);
        gamecanvas.start();
    }
    
    public void MenuListInstruction(){
        intro = new Instruction(this);
        Display.getDisplay(this).setCurrent(intro);
    }
      
    public void IntroBack() {
        Display.getDisplay(this).setCurrent(menuList);
    }
    
    public void GameCanvasMenu(){                                        
        menuList.setGameActive(true);                     //位菜单添加继续游戏选项
        Display.getDisplay(this).setCurrent(menuList);   //显示游戏菜单
    }
    
    public void CanvasNewGame(){
          gamecanvas.init();
          Display.getDisplay(this).setCurrent(gamecanvas);
          gamecanvas.continuegame();
     
     }
    
     public int getBestRecord(){
        return hasBestRecord? bestRecord:-1;        //返回最高成绩
    }
    
    public boolean checkBestRecord(int record){
        if( !hasBestRecord || (record > bestRecord) ){
            hasBestRecord = true;
            bestRecord = record;
            writeRecordStore();
            return true;
        }else{
            return false;
        }
    }
    private void readRecordStore(){
        hasBestRecord = false;
        RecordStore rs = null;
        ByteArrayInputStream bais = null;
        DataInputStream dis = null;
        try{
            rs = RecordStore.openRecordStore(RS_NAME,false); //如果不存在,新建
            byte[] data = rs.getRecord(1);
            bais = new ByteArrayInputStream(data);
            dis = new DataInputStream(bais);
            bestRecord = dis.readInt();
            hasBestRecord = true;
        }catch(IOException e){}
        catch(RecordStoreException ex){}
        finally{
            if(dis != null){
                try{
                    dis.close();
                }catch(IOException e){}
            }
            if(bais != null){
                try{
                    bais.close();                    
                }catch(IOException e){}
            }
            if(rs != null){
                try{
                    rs.closeRecordStore();
                }catch(RecordStoreException e){}
            }
        }
    }    
    private void writeRecordStore(){
        RecordStore rs = null;
        ByteArrayOutputStream baos = null;
        DataOutputStream dos = null;
        try{
            rs = RecordStore.openRecordStore(RS_NAME,true);
            baos = new ByteArrayOutputStream();
            dos  = new DataOutputStream(baos);
            dos.writeInt(bestRecord);
            byte[] data = baos.toByteArray();
            if(rs.getNumRecords() == 0){
                rs.addRecord(data, 0,data.length);
            }else{
                rs.setRecord(1,data,0,data.length);                
            }
        }catch(IOException e){}
        catch(RecordStoreException e){}
        finally{
            if(dos != null){
                try{
                    dos.close();
                }catch(IOException e){}
            }
            if(baos != null){
                try{
                    baos.close();
                }catch(IOException e){}
            }
            if(rs != null){
                try{
                    rs.closeRecordStore();
                }catch(RecordStoreException e){}
            }
        }
    }
    
    void menuListHighScore(){
        Display.getDisplay(this).setCurrent(new HighScoreScreen(this));
    }
    
    void highScoreBack(){
        Display.getDisplay(this).setCurrent(menuList);
    }
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -