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

📄 mysubmarinecanvas.java~204~

📁 j2me源代码
💻 JAVA~204~
📖 第 1 页 / 共 2 页
字号:
        }

        //对所有已经存在的敌人潜艇进行tick触发
        Image fire = null;
        for (int j = 0; j < this.enemyVector.size(); j++) {
            EnemySub enemySub = (EnemySub) enemyVector.elementAt(j);
            //当生命周期结束
            if (!enemySub.getLifeState()) {
                fire = ResObj.createImage("/res/fire.png");
                enemySub.setImage(fire, fire.getWidth(),
                        fire.getHeight());
                enemyVector.removeElementAt(j);

                //消灭一艘敌人潜艇,  同时更新监听数组
                SpriteChanged spriteChanged = new SpriteChanged(enemySub,
                        layerManager);
                spriteChanged.start();

                spriteChanged = null;
                enemySub = null;

                this.nCurrentEnemy--;
                this.nMaxEnemy--;
            } else {
                enemySub.tick();
            }
        }
        fire = null;
    }


    /**
     * tickShip
     */
    private void tickShip() {
        if (!ship.getLifeState()) {
            nState = GAME_OVER;
        }
    }

    /**
     * tickTorpedo
     */
    protected void tickTorpedo() {
        //鱼雷 图形运动
        //如果某个鱼雷元素已经结束生命周期, 则置null, 并从数组中删除
        for (int j = 0; j < this.torpedoVector.size(); j++) {
            Torpedo torpedo = (Torpedo) torpedoVector.elementAt(j);

            //当生命周期结束
            if (!torpedo.getLifeState()) {
                torpedoVector.removeElementAt(j);
                this.layerManager.remove(torpedo);
                torpedo = null;
            } else {
                torpedo.tick();
            }
        }

        for (int j = 0; j < this.enemyTorpedoVector.size(); j++) {
            Torpedo torpedo = (Torpedo) enemyTorpedoVector.elementAt(j);

            //当生命周期结束
            if (!torpedo.getLifeState()) {
                enemyTorpedoVector.removeElementAt(j);
                this.layerManager.remove(torpedo);
                torpedo = null;
            } else {
                torpedo.tick();
            }
        }
    }

    private void clear() {
        this.nLevel = 1;
        this.nMaxEnemy = this.nLevel * 10;
        this.nCurrentEnemy = 0;
        TRIGGER_COUNT = 0;
        SEABACK_DENSITY = 0;
        this.nCurrentEnemyLimit = this.nLevel * 2;
    }

    /**重新设置图层显示区域
     * @param x          玩家潜艇位置x
     * @param y          玩家潜艇位置y
     * @param width      玩家潜艇宽
     * @param height     玩家潜艇高
     */
    public void adjustViewWindow(int xShip,  int width) {
        if (this.userViewWindow) {
            xPosOfView = xShip + (width / 2) - (nViewWidth / 2);
            if (xPosOfView < 0) {
                xPosOfView = 0;
            }
            if (xPosOfView > (WORLD_WIDTH - nViewWidth)) {
                xPosOfView = WORLD_WIDTH - nViewWidth;
            }

            layerManager.setViewWindow(xPosOfView, yPosofView,
                                       nViewWidth, nViewHeight);
        }
    }


    /**
     *  画布重画事件,替代Canvas中的paint()事件, 根据不同的游戏状态(gameState)画出游戏画面
     *  本方法由thread每次重新启动, 最后执行flushGraphics()重画缓冲区
     */
    public synchronized void paintCanvas(Graphics g) {
        switch(nState){
        case GAME_INIT://游戏第一次启动
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, mainWidth, mainHeight);

            //重新绘制图层
            if (this.layerManager != null) {
                this.layerManager = null;
                this.layerManager = new LayerManager();

                if (userViewWindow) {
                    this.layerManager.setViewWindow(xPosOfView, yPosofView,
                            nViewWidth, nViewHeight);
                }
            }

            //重新绘制玩家潜艇
            if (ship != null) {
                ship = null;
                ship = new Ship(this, ResObj.createImage("/res/ship.png"),
                                      getWidth() / 3, 0, layerManager);
            }

            //创建背景图层
            this.createSandBackground();
            this.createShip();
            this.createSeaBackground();
            nState = GAME_RUN;
            break;
        case GAME_RUN:
            //游戏处于运行状态
            //在性能过耗(可用内存不到当前内存总量的4/5时),进行垃圾回收GC
            g.setColor(255,255,255);
            g.fillRect(0,0,mainWidth,mainHeight);
            g.setColor(255, 0, 0);
            StringBuffer str = new StringBuffer("Life State:" + this.ship.getHpLife());
            int x = ship.x + ship.w;
            int y = ship.y - ship.h / 2;
            g.drawString(str.toString(), 0, 0, Graphics.LEFT | Graphics.TOP);
            if (rt.freeMemory() < (rt.totalMemory() * 4 / 5)) {
                rt.gc();
            }
            break;
        case GAME_NEXTROUND:
            //下一轮游戏
            //更新数据
            ship.setHpLife(15);
            ship.setPosition(mainWidth / 3, 0);
            if (this.nLevel >= 4) {
                nState = GAME_OVER;
            } else {
                this.nLevel++;
                controller.handleEvent(UIController.EventID.EVENT_NEXTROUND);
                //目前游戏只设计了四级关卡
                this.nMaxEnemy = this.nLevel * 10;
                this.nCurrentEnemy = 0;
                TRIGGER_COUNT = 0;
                TICK_COUNT = 0;
                this.nCurrentEnemyLimit = this.nLevel * 2;

                //暂时删除layerManager中所有文件,清空鱼雷与敌人潜艇数据
                //为更新图层做准备
                this.torpedoVector.removeAllElements();
                this.enemyVector.removeAllElements();

                //更新海底图层数据
                this.SEABACK_DENSITY = (SEABACK_DENSITY + 1) % 4;

                this.hideNotify();
                nState = GAME_INIT;
            }
            break;
        case GAME_OVER:
            //游戏结束
            threadAlive = false;
            g.setColor(0, 0, 0);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(255, 0, 0);
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
                                   Font.SIZE_LARGE));
            g.drawString("Game Over", getWidth() / 2, getHeight() / 2,
                         Graphics.BASELINE | Graphics.HCENTER);
            this.flushGraphics();
            try {
                thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            this.layerManager = null;
            this.removeCommand(this.pauseCommand);
            this.addCommand(startCommand);
            controller.handleEvent(UIController.EventID.EVENT_EXIT);
            return;
        default:
            break;
        }
        //在缓冲区重画
        this.layerManager.paint(g, 0, 0);
        this.flushGraphics();
    }

    /**
     * createSeaBackground
     */
    private void createSeaBackground() {
        if (this.SEABACK_DENSITY >= 4) {
            SEABACK_DENSITY = 0;
        }

        //创建海水背景图
        Image image = ResObj.createImage("/res/layer" +
                                         this.SEABACK_DENSITY + ".png");

        layerSeaback = new TiledLayer(WIDTH_IN_TILES, HEIGHT_IN_TILES,
                                      image, TILE_WIDTH, TILE_HEIGHT);

        for (int row = 0; row < HEIGHT_IN_TILES; row++) {
            for (int column = 0; column < WIDTH_IN_TILES; column++) {
                layerSeaback.setCell(column, row,
                                     ResObj.createRandom(
                                             NUM_DENSITY_LAYER_TILES) +
                                     1);
            }
        }
        layerSeaback.setPosition(0, SHIP_HEIGHT);
        layerManager.append(layerSeaback);

        image = null;
    }

    /**
     * createSandBackground
     */
    private void createSandBackground() {

        Image bottomTitles = ResObj.createImage("/res/bottom.png");

        //将图片bottomTitles切成指定大小(TILE_WIDTH, TILE_HEIGHT)
        //创建一个指定维数(1, WIDTH_IN_TILES)的背景数组
        TiledLayer layer = new TiledLayer(WIDTH_IN_TILES, 1,
                                          bottomTitles, TILE_WIDTH, TILE_HEIGHT);

        for (int column = 0; column < WIDTH_IN_TILES; column++) {
            //将海底图层数组中的每个小格用原始图片的第i块来填充
            int i = ResObj.createRandom(NUM_DENSITY_LAYER_TILES) + 1;
            layer.setCell(column, 0, i);
        }
        layer.setPosition(0, mainHeight - bottomTitles.getHeight());
        layerManager.append(layer);

        bottomTitles = null;
    }

    /**
     * createMysub
     */
    protected void createShip() {
        this.layerManager.append(this.ship);
    }

    public Ship getShip() {
        return this.ship;
    }

    public void setActive(){
        this.showNotify();
    }
}

⌨️ 快捷键说明

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