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

📄 manmidlet.java

📁 是男人就撑20秒游戏j2me版
💻 JAVA
字号:
/*
 * ManMidlet.java
 *
 * Created on 2006年11月24日, 下午5:35
 *
 * 游戏的主类,继承MIDlet类,并实现Runnable接口。
 * 1、负责游戏中多个屏幕的切换
 * 2、分数的读取和存储
 */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;

/**
 *
 * @author  TOM
 * @version
 */
public class ManMidlet extends MIDlet implements Runnable{    
    private ManCanvas manCanvas;
    private MenuList menuList;
    private boolean initDone = false;
    private static final String RS_NAME = "MANRecord";
    private boolean hasBestRecord = false;
    private int bestRecord;
    
    void splashScreenPainted(){
        new Thread(this).start();
    }
    
    /** 构造方法 */
    public ManMidlet(){
    }
    
    /** Midlet启动方法 */
    public void startApp() {
        Displayable current = Display.getDisplay(this).getCurrent();  //获取当前的显示屏幕
        if(current == null){  //如果没有任何屏幕被显示
            Display.getDisplay(this).setCurrent(new SplashScreen(this));  //创建并显示闪屏   
        }else{
            if(current == manCanvas){
                manCanvas.start();
            }
            Display.getDisplay(this).setCurrent(current);            
        }   
    }   

    /** 启动线程 */
    public void run(){  
        init();             //初始化
    }
    
    /** 初始化程序数据 */
    private synchronized void init(){
        if(!initDone){
            readRecordStore();                //读取高分纪录
            menuList = new MenuList(this);    //创建游戏菜单
            manCanvas = new ManCanvas(this);  //创建游戏画布
            initDone = true;                   //初始化完毕,设置初始化标志位
        }
    }
    
    /** 绘制闪屏结束 */
    void splashScreenDone(){
        init();                                           //初始化
        Display.getDisplay(this).setCurrent(menuList);  //显示游戏菜单
    }
    
    void menuListNewGame(){
        manCanvas.init();                                   //初始化游戏数据
        Display.getDisplay(this).setCurrent(manCanvas);   //显示游戏画布  
        manCanvas.start();                                  //启动游戏线程
    }
    
    void GameCanvasMenu(){
        manCanvas.stop();                                   //停止游戏循环
        menuList.setGameActive(true);                     //位菜单添加继续游戏选项
        Display.getDisplay(this).setCurrent(menuList);   //显示游戏菜单
    }
    
    void menuListContinue(){
        Display.getDisplay(this).setCurrent(manCanvas); //显示画布
        manCanvas.start();                                //启动游戏线程
    }
    
    void menuListHighScore(){
        Display.getDisplay(this).setCurrent(new HighScoreScreen(this));
    }
    
    void highScoreBack(){
        Display.getDisplay(this).setCurrent(menuList);
    }
    
    void menuListInstructions(){
        Display.getDisplay(this).setCurrent(new InstructionsScreen(this));
    }   
    
    void instructionBack(){
        Display.getDisplay(this).setCurrent(menuList);  //显示游戏菜单
    }
    
    void GameCanvasGameOver(long time, int record){
        manCanvas.stop();
        menuList.setGameActive(false);
        Display.getDisplay(this).setCurrent(new GameOverScreen(this,time,record));//显示游戏结束屏幕
    }
    
    void gameOverDone(){
        Display.getDisplay(this).setCurrent(menuList);      //显示游戏菜单
    }
    
    void menuListQuit(){
        quit();
    }
    
    private void quit(){
        destroyApp(false);         //销毁程序
        notifyDestroyed();         //告知销毁
    }
    
    public void destroyApp(boolean unconditional) {
        if(manCanvas != null){      //如果存在游戏画布对象
            manCanvas.stop();
        }
    } 
    
    public void pauseApp() {
        Displayable current = Display.getDisplay(this).getCurrent();
        if(current == manCanvas){
            manCanvas.stop();
        }
    }  
    
    int getBestRecord(){
        return hasBestRecord? bestRecord:-1;        //返回最高成绩
    }
    
    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){}
            }
        }
    }
}

⌨️ 快捷键说明

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