WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术程序开发一个基于MIDP的迷宫游戏

一个基于MIDP的迷宫游戏
作者:J2MEDEV  来源:J2MEDEV  发布时间:2006-7-25 9:08:00
() {
        return(myMaxSquareSize);
    }
   
    /**
     * @return the maximum number of columns the display can be divided into.
     */
    int getMaxNumCols() {
        return(myMaxGridWidth);
    }
   
    /**
     * @return the width of the maze walls.
     */
    int getColWidth() {
        return(mySquareSize);
    }
   
    /**
     * @return the number of maze columns the display is divided into.
     */
    int getNumCols() {
        return(myGridWidth);
    }
   
    //-----------------------------------------------------
    //    initialization and game state changes
   
    /**
     * Constructor performs size calculations.
     * @throws Exception if the display size is too
     *         small to make a maze.
     */
    public MazeCanvas(Display d) throws Exception {
        myDisplay = d;
        // a few calculations to make the right maze
        // for the current display.
        int width = getWidth();
        int height = getHeight();
        // tests indicate that 5 is a good default square size,
        // but the user can change it...
        mySquareSize = 5;
        myMinSquareSize = 3;
        myMaxGridWidth = width / myMinSquareSize;
        if(myMaxGridWidth % 2 == 0) {
            myMaxGridWidth -= 1;
        }
        myGridWidth = width / mySquareSize;
        if(myGridWidth % 2 == 0) {
            myGridWidth -= 1;
        }
        myGridHeight = height / mySquareSize;
        if(myGridHeight % 2 == 0) {
            myGridHeight -= 1;
        }
        myMinGridWidth = 15;
        myMaxSquareSize = width / myMinGridWidth;
        if(myMaxSquareSize > height / myMinGridWidth) {
            myMaxSquareSize = height / myMinGridWidth;
        }
        // if the display is too small to make a reasonable maze,
        // then we throw an Exception
        if(myMaxSquareSize < mySquareSize) {
            throw(new Exception("Display too small"));
        }
    }
   
    /**
     * This is called as soon as the application begins.
     */
    void start() {
        myDisplay.setCurrent(this);
        repaint();
    }
   
    /**
     * discard the current maze and draw a new one.
     */
    void newMaze() {
        myGameOver = false;
        // throw away the current maze.
        myGrid = null;
        // set the player back to the beginning of the maze.
        myPlayerX = 1;
        myPlayerY = 1;
        myOldX = 1;
        myOldY = 1;
        myDisplay.setCurrent(this);
        // paint the new maze
        repaint();
    }
   
    //-------------------------------------------------------
    //  graphics methods
   
    /**
     * Create and display a maze if necessary, otherwise just
     * move the player.  Since the motion in this game is
     * very simple, it is not necessary to repaint the whole
     * maze each time, just the player + erase the square
     * that the player just left..
     */
    protected void paint(Graphics g) {
        // If there is no current maze, create one and draw it.
        if(myGrid == null) {
            int width = getWidth();
            int height = getHeight();
            // create the underlying data of the maze.
            myGrid = new Grid(myGridWidth, myGridHeight);
            // draw the maze:
            // loop through the grid data and color each square the
            // right color
            for(int i = 0; i < myGridWidth; i++) {
                for(int j = 0; j < myGridHeight; j++) {
                    if(myGrid.mySquares[i][j] == 0) {
                        g.setColor(BLACK);
                    } else {
                        g.setColor(WHITE);
                    }
                    // fill the square with the appropriate color
                    g.fillRect(myStartX + (i*mySquareSize),
                            myStartY + (j*mySquareSize),
                            mySquareSize, mySquareSize);
                }
            }
            // fill the extra space outside of the maze
            g.setColor(BLACK);
            g.fillRect(myStartX + ((myGridWidth-1) * mySquareSize),
                    myStartY, width, height);
            // erase the exit path:
            g.setColor(WHITE);
            g.fillRect(myStartX + ((myGridWidth-1) * mySquareSize),
                    myStartY + ((myGridHeight-2) * mySquareSize), width, height);
            // fill the extra space outside of the maze
            g.setColor(BLACK);
            g.fillRect(myStartX,
                    myStartY + ((myGridHeight-1) * mySquareSize), width, height);
        }
        // draw the player (red):
        g.setColor(255, 0, 0);
        g.fillRoundRect(myStartX + (mySquareSize)*myPlayerX,
                myStartY + (mySquareSize)*myPlayerY,
                mySquareSize, mySquareSize,
                mySquareSize, mySquareSize);
        // erase the previous location
        if((myOldX != myPlayerX) || (myOldY != myPlayerY)) {
            g.setColor(WHITE);
            g.fillRect(myStartX + (mySquareSize)*myOldX,
                    myStartY + (mySquareSize)*myOldY,
                    mySquareSize, mySquareSize);}
        // if the player has reached the end of the maze,
        // we display the end message.
        if(myGameOver) {
            // perform some calculations to place the text correctly:
            int width = getWidth();
            int height = getHeight();
            Font font = g.getFont();
            int fontHeight = font.getHeight();
            int fontWidth = font.stringWidth("游戏过关");
            g.setColor(WHITE);
            g.fillRect((width - fontWidth)/2, (height - fontHeight)/2,
                    fontWidth + 2, fontHeight);
            // write in red
            g.setColor(255, 0, 0);
            g.setFont(font);
            g.drawString("游戏完成", (width - fontWidth)/2,
                    (height - fontHeight)/2,
                    g.TOP|g.LEFT);
        }
    }
   
    /**
     * Move the player.
     */
    public void keyPressed(int keyCode) {
        if(! myGameOver) {
            int action = getGameAction(keyCode);
            switch (action) {
                case LEFT:
                    if((myGrid.mySquares[myPlayerX-1][myPlayerY] == 1) &&
                            (myPlayerX != 1)) {
                        myOldX = myPlayerX;
                        myOldY = myPlayerY;
                        myPlayerX -= 2;
                        repaint();
                    }
                    break;
                case RIGHT:
                    if(myGrid.mySquares[myPlayerX+1][myPlayerY] == 1) {
                        myOldX = myPlayerX;
                        myOldY = myPlayerY;
                        myPlayerX += 2;
                        repaint();
                    } else

上一页  [1] [2] [3] [4] [5]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码