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

📄 spellpic.java

📁 手机拼图游戏:J2ME游戏
💻 JAVA
字号:
/*
 * Author:wangfeng
 * Version:1.0
 * CreateDate:2007-02-23
 * Description:the class is the main class for Spell picture
 */

import util.ParseXML;
import util.Amusement;

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordEnumeration;
import java.io.InputStream;
import java.util.Hashtable;

public class SpellPic extends MIDlet implements CommandListener {
    public static Display display = null;
    public static MenuList mainMenu = null;
    public static Description about = null;
    public static Game game = null;
    public static PlaceInCompetition pace = null;
    public static SelectLevelMenu selectLevel = null;
    public static PassGate pass = null;
    public static Amusement amusement = null;

    public static String RecordStore_Easy="GameLevelLog_Easy";
    public static String RecordStore_Normal="GameLevelLog_Normal";
    public static String RecordStore_Difficulty="GameLevelLog_Difficulty";

    protected void startApp() {
        display = Display.getDisplay(this);

        /* show menu items */
        mainMenu = new MenuList();
        mainMenu.setCommandListener(this);
        display.setCurrent(mainMenu);
    }

    protected void pauseApp() {

    }

    protected void destroyApp(boolean unconditional) {
        SpellPic.display = null;
        SpellPic.mainMenu = null;
        SpellPic.about = null;
        SpellPic.game = null;
        System.gc();
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
        /* main menu control */
        if (null != mainMenu)
            if (MenuList.comdConfirm == c) {
                if (mainMenu.isSelected(MenuList.SELECT_NEWGAME)) {
                    try {

                        selectLevel = new SelectLevelMenu();
                        selectLevel.setCommandListener(this);
                        display.setCurrent(selectLevel);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
                if (mainMenu.isSelected(MenuList.SELECT_PACEIN)) {
                    pace = new PlaceInCompetition();
                    display.setCurrent(pace);

                }
                if (mainMenu.isSelected(MenuList.SELECT_DESC)) {
                    about = new Description();
                    display.setCurrent(about);
                }
            } else if (MenuList.comdBack == c) {
                destroyApp(true);
            }

        /* game level selected menu control */
        if (null != selectLevel)
            if (SelectLevelMenu.comdComit == c) {
                Game.startPicIndex = 0;
                Game.gameLevelNum = 0;

                if (selectLevel.isSelected(SelectLevelMenu.LEVEL_EASY)) {
                    try {
                        Game.GameLevelName = selectLevel.getString(SelectLevelMenu.LEVEL_EASY);
                        game = new Game();
                        game.setCommandListener(this);
                        game.start();
                        display.setCurrent(game);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
                if (selectLevel.isSelected(SelectLevelMenu.LEVEL_NORMAL)) {
                    try {
                        Game.GameLevelName = selectLevel.getString(SelectLevelMenu.LEVEL_NORMAL);
                        game = new Game();
                        game.setCommandListener(this);
                        game.start();
                        display.setCurrent(game);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
                if (selectLevel.isSelected(SelectLevelMenu.LEVEL_DIFFICULTY)) {
                    try {
                        Game.GameLevelName = selectLevel.getString(SelectLevelMenu.LEVEL_DIFFICULTY);
                        game = new Game();
                        game.setCommandListener(this);
                        game.start();
                        display.setCurrent(game);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
            } else if (SelectLevelMenu.comdBack == c) {
                display.setCurrent(mainMenu);
            }

        /* game control */
        if (null != game)
            if (Game.comdCancel == c) {
                try {
                    game.exit();
                    display.setCurrent(mainMenu);
                    try
                    {
                        game.player.stop();
                        game.player.close();
                    }catch(Exception e){}

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (Game.comdContinue == c) {
                try {
                    Game.startPicIndex++;
                    Game.gameLevelNum++;
                    game.exit();
                    try
                    {
                        game.player.stop();
                        game.player.close();
                    }catch(Exception e){}

                    pass = new PassGate();
                    pass.setImage(PassGate.ImageNextGate);
                    pass.setCommandListener(this);
                    pass.setString("     恭 喜 过 关 ,欢 迎 进 入 下 一 关!");
                    display.setCurrent(pass);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (Game.comdEnd == c) {
                try {
                    Game.startPicIndex++;
                    Game.gameLevelNum++;
                    game.exit();
                    try
                    {
                        game.player.stop();
                        game.player.close();
                    }catch(Exception e){}

                    pass = new PassGate();
                    pass.setImage(PassGate.ImageFinish);
                    PassGate.TITLE="恭喜过通关";
                    pass.setString("     恭   喜   过   通   关   !!!");
                    pass.setCommandListener(this);
                    pass.removeCommand(PassGate.comdComit);
                    display.setCurrent(pass);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        /* game pass a gate */
        if (null != pass)
            if (PassGate.comdCancel == c) {
                saveLog();
                display.setCurrent(mainMenu);
            } else if (PassGate.comdComit == c) {
                try {
                    saveLog();
                    if (null == game)
                        game = new Game();
                    else
                    {
                        game.removeCommand(Game.comdContinue);
                        game.drawGraphic();
                    }
                    game.conti=true;
                    game.start();
                    display.setCurrent(game);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }else if(PassGate.comdAmusement == c)
            {
                amusement = new Amusement(Game.gameLevelNum);
                amusement.nextPic();
                amusement.setCommandListener(this);
                display.setCurrent(amusement);
            }

        if(null != amusement)
            if(Amusement.comdBack == c)
            {
                saveLog();
                amusement = null;
                display.setCurrent(pass);
            }else if(Amusement.comdNext == c)
            {
                amusement.nextPic();
            }

    }

    private void saveLog() {
        String recordIDName = "";
        try {
            int levelNum = 0;
            if(game.GameLevelName.equals(SelectLevelMenu.LEVEL_EASY_NAME))
                 recordIDName = RecordStore_Easy;
            else if(game.GameLevelName.equals(SelectLevelMenu.LEVEL_NORMAL_NAME))
                 recordIDName = RecordStore_Normal;
            else if(game.GameLevelName.equals(SelectLevelMenu.LEVEL_DIFFICULTY_NAME))
                recordIDName = RecordStore_Difficulty;

            RecordStore rec  = RecordStore.openRecordStore(recordIDName,false);
            RecordEnumeration en = rec.enumerateRecords(null,null,false);
            int m=0;
            while(en.hasPreviousElement())
            {
                if(m == 0)
                   en.previousRecord();
                
                if(m == 1)
                {
                    try
                    {
                        levelNum = Integer.parseInt(new String(en.previousRecord()));
                    }catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                    break;
                }
                m++;
            }
            rec.closeRecordStore();
            if(levelNum < Game.gameLevelNum)
            {
                RecordStore.deleteRecordStore(recordIDName);

                rec = RecordStore.openRecordStore(recordIDName, true);
                rec.addRecord(game.GameLevelName.getBytes(), 0, game.GameLevelName.getBytes().length);
                rec.addRecord((String.valueOf(Game.gameLevelNum)).getBytes(), 0, (String.valueOf(Game.gameLevelNum)).getBytes().length);
                rec.closeRecordStore();

            }

        } catch (Exception e) {
            try
            {
                RecordStore rec = RecordStore.openRecordStore(recordIDName, true);
                rec.addRecord(game.GameLevelName.getBytes(), 0, game.GameLevelName.getBytes().length);
                rec.addRecord((String.valueOf(Game.gameLevelNum)).getBytes(), 0, (String.valueOf(Game.gameLevelNum)).getBytes().length);
                rec.closeRecordStore();
            }catch(Exception ex)
            {ex.printStackTrace();}
        }

    }
}

⌨️ 快捷键说明

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