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

📄 dragon.java

📁 一个基于JAVA的手机射击类游戏``有过关模式
💻 JAVA
字号:
package jsky;

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class Dragon extends MIDlet implements Runnable {
    Display display;// 声明一个Display对象
    
    public static int MISSIONCOUNTS = 0;// 游戏关卡计数器
    
    public static final int MISSIONONE = 0; // 第一关计数标记
    
    public static final int MISSIONTWO = 1; // 第二关计数标记
    
    public static final int MISSIONTHREE = 2; // 第三关计数标记
    
    public static final int MISSIONBOSS = 3; // 第四关计数标记
    
    public static final int MISSIONOVER = 4; // 游戏结束计数标记
    
    public static final int ABOUTCANVAS = 5; // showAbout计数标记
    public static final int GAMEASSISTANT = 6;
    
    public static final int MENULIST = -1; // showMenu计数标记
    
    private boolean isThisGameOver; // 声明一个整个游戏的状态变量
    private boolean init;
    //==============================================================
    private boolean gateOneIsFirstCreate = true;
    private boolean gateTwoIsFirstCreate = true; // 声明一个第二关创建标记
    private boolean gateThreeIsFirstCreate = true; //声明一个第三关创建标记
    private boolean gateFourIsFirstCreate = true; //声明一个第四关创建标记
    private boolean gameOverIsFirstCreate = true; //声明一个游戏结束创建标记
    private boolean gameAboutIsFirstCreate = true; //声明一个游戏关于创建标记
    private boolean gameAssistentIsFirstCreate = true; //声明一个游戏帮助创建标记
    //==============================================================
    
    public Thread mainThread = null; // 声明一个游戏线程对象
    
    boolean stopping; // 主线程控制器
    
    public static boolean showMenu; // 菜单显示标记
    
    private AuthorLogoSplashCanvas authorLogo;// 闪屏
    
    private GameMenuList gameMenuList;// 声明系统菜单对象
    
    private GameAbout gameAbout; // 声明关于画布的对象
    
    private GameAssistant gameAssistant; // 声明玩家帮助的对象
    
    private DragonCanvas dragonCanvas;// 声明一个DragonCanvas对象(第一关)
    
    
    private DesertCanvas desertCanvas;// 声明一个DesertCanvas对象(第二关)
    
    private OceanCanvas oceanCanvas; // 声明一个OceanCanvas对象(第三关)
    
    private BOSSCanvas bossCanvas; // 声明一个BOSSCanvas对象(boss关)
    
    private GameOverCanvas gameOverCanvas;// 声明一个最后的gameOver对象
    
    public Dragon() {
        super();
        // 由于采取了专用的初始化函数,所以没有在这里添加代码.
        System.out.println("创建游戏实例");
    }
    
    /**
     * 创建关卡实例
     *
     */
    public  void init() {
        if(!init){
            // 预加载菜单
            System.out.println("create menu!!!");
            gameMenuList = new GameMenuList(this);
            // 预加载第一关
            System.out.println("create gate one!!!");
            dragonCanvas = new DragonCanvas(Display.getDisplay(this));
            init = true;
            System.out.println("前期资源加载完毕");
        }
    }
    
    public void startApp() {
        
        Displayable current = Display.getDisplay(this).getCurrent();
        if (current == null) {
            authorLogo = new AuthorLogoSplashCanvas(this);
            Display.getDisplay(this).setCurrent(authorLogo);
        } else {
            if (current == dragonCanvas) {
                dragonCanvas.start();
            } else if (current == desertCanvas) {
                desertCanvas.start();
            } else if (current == oceanCanvas) {
                oceanCanvas.start();
            } else if (current == bossCanvas) {
                bossCanvas.start();
            } else if (current == gameOverCanvas) {
                gameOverCanvas.start();
            }
            Display.getDisplay(this).setCurrent(current);
        }
        
    }
    
    // =============系统菜单选项的处理===============
    /**
     * 开始新游戏时进行游戏初始化 启动游戏主线程!
     */
    public void menuListGameStart() {
        
        // 初始化每一关
        if(dragonCanvas != null){
            dragonCanvas.init();
        }
        if(desertCanvas != null){
            desertCanvas.init();
        }
        if(oceanCanvas != null){
            oceanCanvas.init();
        }
        if(bossCanvas != null){
            bossCanvas.init();
        }
        
        if (dragonCanvas != null) {
            start();
        }
        
    }
    
    /**
     * 显示关于
     *
     */
    void showAboutCanvas() {
        gameAbout = new GameAbout(this);
        Display.getDisplay(this).setCurrent(gameAbout);
    }
    
    /**
     * 添加对玩家帮助的调用。
     *
     */
    void showGameAssistant() {
        System.out.println("create GameAssistant!!!");
        gameAssistant = new GameAssistant(this);
        Display.getDisplay(this).setCurrent(gameAssistant);
    }
    
    /**
     * 与闪屏同步时调用 加载游戏资源
     */
    void splashScreenDone() {
        init();
        System.out.println("go to menu");
        gameMenuList = new GameMenuList(this);
        Display.getDisplay(this).setCurrent(gameMenuList);
    }
    
    /**
     * 返回菜单
     *
     */
    void backToMenu() {
        System.out.print("back to menu");
        isThisGameOver = true;
        gameMenuList = new GameMenuList(this);
        Display.getDisplay(this).setCurrent(gameMenuList);
    }
    
    /**
     * 定义线程启动
     *
     */
    public void start() {
        isThisGameOver = true;
        MISSIONCOUNTS = 0;
        mainThread = new Thread(this);
        Thread currendThread = Thread.currentThread();
        if (currendThread != mainThread) {
            currendThread = mainThread;
            currendThread.start();
        }
        
    }
    
    public void stop() {
       // mainThread = null;
        isThisGameOver = false;
        
    }
    
    /*public void resume(){
        isThisGameOver = true;
    }*/
    public void run() {
        long startTime = 0, endTime = 0, difTime = 0;
        int rate = 40;// 刷新速度40/50/70/30/100/300/500
        Thread currentThread = Thread.currentThread();
        while (isThisGameOver) {
            startTime = System.currentTimeMillis();
            Displayable currentCanvas = Display.getDisplay(this).getCurrent();
            System.out.println("currentCanvas: " + currentCanvas);
            System.out.println("currentThread: " + currentThread);
            
            if (MISSIONCOUNTS == MISSIONONE) {
                if (currentCanvas != dragonCanvas || currentCanvas == null) {
                    currentCanvas = dragonCanvas;
                    System.out.println("gate one start!");
                    if (currentThread != DragonCanvas.dragonThread) {
                        currentThread = DragonCanvas.dragonThread;
                        System.out.println("********************************");
                        Display.getDisplay(this).setCurrent(dragonCanvas);
                        System.out.println("************************************");
                        dragonCanvas.start();
                    }
                }
            } else if (MISSIONCOUNTS == MISSIONTWO) {
                try {
                    // 预加载第二关
                    if(gateTwoIsFirstCreate){
                        System.out.println("create gate two!!!");
                        desertCanvas = new DesertCanvas(Display.getDisplay(this));
                        gateTwoIsFirstCreate = false;
                    }
                    Thread.sleep(1000);// 关卡之间切换用时1s
                } catch (InterruptedException e) {
                    
                }
                if (currentCanvas != desertCanvas ||currentCanvas == null) {
                    currentCanvas = desertCanvas;
                    System.out.println("gate two start!");
                    if (currentThread != DesertCanvas.desertThread
                            || currentThread == null) {
                        currentThread = DesertCanvas.desertThread;
                        Display.getDisplay(this).setCurrent(desertCanvas);
                        desertCanvas.start();
                    }
                    
                }
            }
            
            else if (MISSIONCOUNTS == MISSIONTHREE) {
                try {
                    // 预加载第三关
                    if(gateThreeIsFirstCreate){
                        System.out.println("create gate three!!!");
                        oceanCanvas = new OceanCanvas(Display.getDisplay(this));
                        gateThreeIsFirstCreate = false;
                    }
                    Thread.sleep(1000);// 关卡之间切换用时1s
                } catch (InterruptedException e) {
                }
                if (currentCanvas != oceanCanvas || currentCanvas == null) {
                    currentCanvas = oceanCanvas;
                    System.out.println("gate three start!");
                    if (currentThread != OceanCanvas.oceanThread
                            || currentThread == null) {
                        currentThread = OceanCanvas.oceanThread;
                        Display.getDisplay(this).setCurrent(oceanCanvas);
                        oceanCanvas.start();
                    }
                }
            } else if (MISSIONCOUNTS == MISSIONBOSS) {
                try {
                    if(gateFourIsFirstCreate){
                        System.out.println("create gate four!!!");
                        bossCanvas = new BOSSCanvas(Display.getDisplay(this));
                        gateFourIsFirstCreate = false;
                    }
                    Thread.sleep(1000);// 关卡之间切换用时1s
                } catch (InterruptedException e) {
                }
                if (currentCanvas != bossCanvas || currentCanvas == null) {
                    currentCanvas = bossCanvas;
                    System.out.println("gate four start!");
                    if (currentThread != BOSSCanvas.bossThread
                            || currentThread == null) {
                        currentThread = BOSSCanvas.bossThread;
                        Display.getDisplay(this).setCurrent(bossCanvas);
                        bossCanvas.start();
                    }
                }
            } else if (MISSIONCOUNTS == MISSIONOVER) {
                try {
                    if(gameOverIsFirstCreate){
                        System.out.println("create gate gameOver!!!");
                        gameOverCanvas = new GameOverCanvas(this);
                        gameOverIsFirstCreate = false;
                    }
                    Thread.sleep(1000);// 关卡之间切换用时1s
                } catch (InterruptedException e) {
                }
                if (currentCanvas != gameOverCanvas || currentCanvas == null) {
                    currentCanvas = gameOverCanvas;
                    System.out.println("gameOver canvas show!");
                    if (currentThread != GameOverCanvas.gameOverThread
                            || currentThread == null) {
                        currentThread = GameOverCanvas.gameOverThread;
                        Display.getDisplay(this).setCurrent(gameOverCanvas);
                        gameOverCanvas.start();
                    }
                }
            } else if (MISSIONCOUNTS == MENULIST) {
                System.out.println("5555555555555555555555555555555555555555");
                try {
                    Thread.sleep(1000);// 关卡之间切换用时1s
                } catch (InterruptedException e) {
                }
                if (showMenu) {
                    if (currentCanvas != gameMenuList) {
                        currentCanvas = gameMenuList;
                        System.out.println("gate menu start!");
                        BOSSCanvas.bossCanvasRunning = true;
                        Display.getDisplay(this).setCurrent(gameMenuList);
                        isThisGameOver = false;
                    }
                }
            }
        }
    }
    
    public void pauseApp() {
        stop();
    }
    
    public void destroyApp(boolean arg0) {
        
    }
    
    public void quitApp() {
          System.gc();
        if (isThisGameOver == true) {
            isThisGameOver = false;
        }
        if (mainThread != null) {
            mainThread = null;
        } else if (dragonCanvas != null) {
            dragonCanvas = null;
        } else if (desertCanvas != null) {
            desertCanvas = null;
        } else if (oceanCanvas != null) {
            oceanCanvas = null;
        } else if (bossCanvas != null) {
            bossCanvas = null;
        } else if (gameOverCanvas != null) {
            gameOverCanvas = null;
        } else if (gameMenuList != null) {
            gameMenuList = null;
        } else if (gameAbout != null) {
            gameAbout = null;
        } else if (gameAssistant != null) {
            gameAssistant = null;
        } else if (authorLogo != null) {
            authorLogo = null;
        }
        System.gc();
        System.out.print("destory the rubbish");
        destroyApp(true);
        notifyDestroyed();
        
    }
}

⌨️ 快捷键说明

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