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

📄 mygamecanvas.java.bak

📁 游戏学院《天下武王》教学实例
💻 BAK
📖 第 1 页 / 共 4 页
字号:
                if (npc.startFrameIndex + 2 <= npc.endFrameIndex) {
                    npc.sprite.setFrame(npc.startFrameIndex + 2);
                }
                //英雄在NPC的上边
            } else if (dir == Canvas.UP) {
                if (npc.startFrameIndex + 1 <= npc.endFrameIndex) {
                    npc.sprite.setFrame(npc.startFrameIndex + 1);
                }
                //英雄在NPC右边

            } else if (dir == Canvas.RIGHT) {
                if (npc.startFrameIndex + 3 <= npc.endFrameIndex) {
                    npc.sprite.setFrame(npc.startFrameIndex + 3);
                }
            } else {
                npc.sprite.setFrame(npc.startFrameIndex);
            }
        }
    }

    private void handleCrashProperty(Property pro) {

    }

    /**
     * 往消息队列中添加消息
     *
     * @param message
     *            要添加到消息队列中的消息
     */
    public void pushMessage(String message) {
        if (messageQueue == null) {
            messageQueue = new Vector();
        }
        if (message != null && message.trim().length() > 0) {
            messageQueue.addElement(message);
        }

    }

    /**
     * 从消息队列中取消息
     *
     * @return 从消息队列中取第一条消息
     */
    public String popMessage() {
        String message = null;
        if (messageQueue != null && messageQueue.size() > 0) {
            message = (String) messageQueue.elementAt(0);
            messageQueue.removeElementAt(0);
        }
        return message;
    }

    /**
     * 游戏主循环
     */

    public void run() {
        while (isRunning && this.gameThread == Thread.currentThread()) {
            long start = System.currentTimeMillis();
            //只有MyGameCanvas是当前Displayable对象才进行
            if (this.isShown()) {
                //处理输入
                System.out.println("input");
                input();
                System.out.println("tick");
                //调整
                tick();
                System.out.println("render");
                
                //绘制
                render();
                
                 System.out.println("aaaa");
                //输出
                flushGraphics();
            }
            long end = System.currentTimeMillis();
            int duration = (int) (end - start);
            if (duration < GAME_LOOP_INTERVAL) {
                try {
                    Thread.sleep(GAME_LOOP_INTERVAL - duration);
                } catch (Exception e) {

                }
            }
        }
        //主线程结束,游戏退出
        MyRPGGameMIDlet.midlet.notifyDestroyed();
    }

    public MyGameCanvas(MIDlet _midlet) {
        super(false);
        this.midlet = _midlet;
        width = getWidth();
        height = getHeight();
        this.myRPGGameMIDlet = (MyRPGGameMIDlet) _midlet;
        this.setFullScreenMode(true);
        System.out.println("game store start");
        gs = new GameStore(this);
        new InnerThread().start();
    }

    public void constructMap() {
        Level level = this.curLevel;

        Map map = level.curMap;

        int hc = map.heroAppearCol;
        int hr = map.heroAppearRow;
        int tw = level.tileWidth;
        int th = level.tileHeight;
        if (myPlayer.row == -1 || myPlayer.col == -1) {
            myPlayer.getSprite(Player.STATUS_WALK)
                    .setPosition(hc * tw, hr * th);
        } else {
            myPlayer.getSprite(Player.STATUS_WALK).setPosition(
                    myPlayer.col * tw, myPlayer.row * th);
        }

        lm = new LayerManager();
        //添加英雄
        lm.append(myPlayer.getSprite(Player.STATUS_WALK));

        //添加NPC和道具
        Vector v = map.rpgObjects;

        for (int i = 0; i < v.size(); i++) {
            RPGObject obj = (RPGObject) v.elementAt(i);
            obj.sprite.setPosition(obj.colNo * tw, obj.rowNo * th);
            obj.sprite.setFrame(obj.startFrameIndex);
            lm.append(obj.sprite);
        }
        lm.append(map.collisionArea);
        for (int i = map.walkableArea.size() - 1; i >= 0; i--) {
            TiledLayer tl = (TiledLayer) map.walkableArea.elementAt(i);
            lm.append(tl);
        }
    }

    public void saveGame() {

        gs.saveGame();
    }

    public void handLevelLoaded() {
        curLevel.curMap = (Map) curLevel.maps
                          .get(new Integer(curLevel.curMapNo));

        constructMap();
        setGameStatus(MyGameCanvas.STATUS_WALKING);
        engine = new GameInnerThread(MyRPGGameMIDlet.mc);
        engine.start();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.gamecollege.j2me.rpg.RPGGameCanvas#loadGame()
     */
    public void loadGame() {
        myPlayer = new Player(ResourceLoader.StringResource[0]);
        if (gs.canLoad()) {
            gs.loadGame();
        }

    }


    /*
     * (non-Javadoc)
     *
     * @see org.gamecollege.j2me.rpg.RPGGameCanvas#stopGame()
     */

    public void sizeChanged(int w, int h) {

        width = w;
        height = h;
        hasFullScreen = true;

    }

    public void drawSubImg(Graphics g, Image img, int x, int y, int swidth,
                           int sheight) {
        g.setClip(x, y, swidth, sheight);
        g.drawImage(img, x - swidth, y - sheight, 0);
        g.setClip(0, 0, width, height);
    }

    private void drawConfirmMenu(Graphics g) {
        g.drawImage(ResourceLoader.menu[3], width / 2, height / 2,
                    Graphics.HCENTER | Graphics.VCENTER);
        g.setColor(255, 255, 255);
        g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                               Font.SIZE_SMALL));
        switch (this.saleType) {

        case 1:

            //确认是否买卖武器
            paintMenu(g, ResourceLoader.StringResource[1], width / 2, 80,
                      curMenuIndex == 0);
            paintMenu(g, ResourceLoader.StringResource[2], width / 2, 100,
                      curMenuIndex == 1);
            paintMenu(g, ResourceLoader.StringResource[3], width / 2, 120,
                      curMenuIndex == 2);

            break;
        case 2:

            //确认是否买卖药品
            paintMenu(g, ResourceLoader.StringResource[4], width / 2, 80,
                      curMenuIndex == 0);
            paintMenu(g, ResourceLoader.StringResource[5], width / 2, 100,
                      curMenuIndex == 1);
            paintMenu(g, ResourceLoader.StringResource[3], width / 2, 120,
                      curMenuIndex == 2);
            break;

        }

    }

    private void drawBuying(Graphics g) {
        g.drawImage(ResourceLoader.menu[0], width / 2, 0, Graphics.TOP
                    | Graphics.HCENTER);
        g.setColor(255, 255, 255);
        g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                               Font.SIZE_SMALL));
        switch (this.saleType) {
        case 1:
            int y1 = 10;
            g.drawImage(ResourceLoader.menu[8], width / 2, y1, Graphics.TOP
                        | Graphics.HCENTER);
            String type = ResourceLoader.StringResource[27];
            if (!isArm) {
                type = ResourceLoader.StringResource[28];
            }

            g.drawString(type, width / 2, y1 + 1, Graphics.TOP
                         | Graphics.HCENTER);

            y1 += 18;
            g.drawString(ResourceLoader.StringResource[29] + ":"
                         + myPlayer.money, width / 2, y1, Graphics.TOP
                         | Graphics.HCENTER);
            Vector ps = weaponVec;
            if (!isArm) {
                ps = jacketVec;
            }

            y1 += 18;

            //武器护具列表

            for (int i = 0; i < ps.size(); i++) {
                Property property = (Property) ps.elementAt(i);
                paintMenu(g, property.name, width / 2, y1, i == curMenuIndex);
                y1 += 18;
            }

            //退出按钮
            paintMenu(g, ResourceLoader.StringResource[3], width / 2, y1,
                      curMenuIndex == jacketVec.size());

            Property curP = null;
            if (isArm == false) {
                if (curMenuIndex < jacketVec.size()) {
                    curP = (Property) jacketVec.elementAt(curMenuIndex);
                }

            } else {
                if (curMenuIndex < weaponVec.size()) {
                    curP = (Property) weaponVec.elementAt(curMenuIndex);
                }

            }

            if (curP != null) {
                String allDesc =
                        curP.description + "    "
                        + ResourceLoader.StringResource[7] + ":" + curP.price;
                g.drawString(allDesc, width / 2, 208, Graphics.HCENTER
                             | Graphics.BOTTOM);
            }

            break;
        case 2:

            //药品
            int y = 10;
            g.drawString(ResourceLoader.StringResource[29] + ":"
                         + myPlayer.money, width / 2, y, Graphics.TOP
                         | Graphics.HCENTER);
            y += 18;
            for (int i = 0; i < this.medicVec.size(); i++) {
                Property property = (Property) medicVec.elementAt(i);

                paintMenu(g, property.name, width / 2, y, i == curMenuIndex);

                y += 18;
            }
            paintMenu(g, ResourceLoader.StringResource[3], width / 2, y,
                      curMenuIndex == medicVec.size());
            curP = null;
            if (medicVec.size() > curMenuIndex) {
                curP = (Property) medicVec.elementAt(curMenuIndex);
            }

            if (curP != null) {
                String allDesc = curP.description + "    "
                                 + ResourceLoader.StringResource[7] + ":" +
                                 curP.price;
                g.drawString(allDesc, width / 2, 208, Graphics.HCENTER
                             | Graphics.BOTTOM);
            }
            break;

        }

    }


    /**
     *
     */

    protected void keyPressed(int keycode) {
        switch (gameStatus) {
        case STATUS_WALKING:
            switch (keycode) {
            case Canvas.KEY_NUM7:

                this.setGameStatus(STATUS_PLAYER_INFO);
                break;
            case Canvas.KEY_NUM9:
                this.setGameStatus(STATUS_MISSION_INFO);
                break;
            case Canvas.KEY_NUM0:
                this.stopGame();
                setGameStatus(STATUS_MAIN_MENU);
                break;
            }
            break;

        case STATUS_MISSION_INFO:

            this.setGameStatus(STATUS_WALKING);
            break;
        case STATUS_MISSION_OVER:

            this.stopGame();
            this.setGameStatus(STATUS_MAIN_MENU);

            break;
        }
    }

    /**
     * 绘制一个菜单项
     * @param g Graphics 对象
     * @param menu 菜单内容
     * @param x x坐标位置
     * @param y y坐标位置
     * @param isCurrent 菜单项是否为当前菜单项
     */

    private void paintMenu(Graphics g, String menu, int x, int y,
                           boolean isCurrent) {
        g.drawImage(ResourceLoader.menu[1], x, y, Graphics.HCENTER
                    | Graphics.TOP);
        g.setColor(255, 255, 255);
        g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
                               Font.SIZE_SMALL));
        g.drawString(menu, x, y + 1, Graphics.HCENTER | Graphics.TOP);
        if (isCurrent) {
            g.drawImage(ResourceLoader.menu[2], x, y + 1, Graphics.HCENTER
                        | Graphics.TOP);
        }
    }

    /**
     * @param gameStatus
     * The gameStatus to set.
     */
    void setGameStatus(int gameStatus) {
        if (gameStatus == STATUS_BUYING || gameStatus == STATUS_POPMENU_CONFIRM
            || gameStatus == STATUS_SALING
            || gameStatus == STATUS_PLAYER_INFO) {
            curMenuIndex = 0;
        } else if (gameStatus == STATUS_MAIN_MENU) {
            hasStored = gs.canLoad();
        }
        this.gameStatus = gameStatus;
    }

    /**
     * @return Returns the gameStatus.
     */
    int getGameStatus() {
        return gameStatus;
    }

    class InnerThread extends Thread {
        public void run() {

            //资源装载器
            ResourceLoader rl = new ResourceLoader();
            //开始装载资源
            rl.start();
            //循环直到所有资源装载完毕
            while (true) {
                if (rl.loadOver) {
                    break;
                }
                if (MyGameCanvas.this.hasFullScreen) {
//					绘制Flash画面
                    drawFlash();
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            //资源装载完毕,出现主菜单画面
            setGameStatus(MyGameCanvas.STATUS_MAIN_MENU);
            //开启游戏主循环线程
            MyGameCanvas.this.startGame();
        }
    }

}

⌨️ 快捷键说明

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