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

📄 minecanvas.java

📁 扫雷...不用介绍了吧. 压缩包里面有截图直接在WTK里面使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        {
            int x = nx * cellWidth;
            for(int ny = 0; ny < curNumCellsY; ny++)
            {
                int y = ny * cellHeight;
                boardImageGraphics.drawImage(button, x, y, 20);
            }

        }

        frameBufferGraphics.setFont(font);
    }

    public void repaintHud()
    {
        frameBufferGraphics.setColor(255, 255, 255);
        frameBufferGraphics.fillRect(hudStartX, hudStartY, hudWidth, hudHeight);
        frameBufferGraphics.setColor(0, 0, 0);
        frameBufferGraphics.drawString(String.valueOf(String.valueOf((new StringBuffer("")).append(bombsLeft).append(" left"))), hudStartX, hudStartY, 20);
        frameBufferGraphics.drawString(String.valueOf(String.valueOf((new StringBuffer("")).append(timeElapsed).append(" secs"))), hudStartX + hudWidth, hudStartY, 24);
    }

    public synchronized void repaintAll()
    {
        frameBufferGraphics.setClip(viewStartX, viewStartY, viewWidth, viewHeight);
        frameBufferGraphics.drawImage(boardImage, boardStartX, boardStartY, 20);
        frameBufferGraphics.drawRect(selectionX * cellWidth + boardStartX + 1, selectionY * cellHeight + boardStartY + 1, cellWidth - 2, cellHeight - 2);
        frameBufferGraphics.setClip(0, 0, canvasWidth, canvasHeight);
        repaintHud();
        if(currentMode == 6)
        {
            String s = "Game Over!";
            short w = (short)(font.stringWidth(s) + 4);
            short h = (short)(font.getHeight() + 2);
            short x = (short)((canvasWidth - w) / 2);
            short y = (short)((canvasHeight - h) / 2);
            if(x < 0)
                x = 0;
            if(y < 0)
                y = 0;
            frameBufferGraphics.setColor(255, 255, 255);
            frameBufferGraphics.fillRect(x, y, w, h);
            frameBufferGraphics.setColor(0, 0, 0);
            frameBufferGraphics.drawString(s, canvasWidth / 2, y + 2, 17);
            frameBufferGraphics.drawRect(x, y, w - 1, h);
        } else
        if(currentMode == 7)
        {
            String s1 = "CLEARED!";
            String s2 = String.valueOf(String.valueOf((new StringBuffer("in ")).append(timeElapsed).append(" secs")));
            short w = (short)(font.stringWidth(s1) + 4);
            short h = (short)(font.getHeight() * 2 + 2);
            short x = (short)((canvasWidth - w) / 2);
            short y = (short)((canvasHeight - h) / 2);
            if(x < 0)
                x = 0;
            if(y < 0)
                y = 0;
            frameBufferGraphics.setColor(255, 255, 255);
            frameBufferGraphics.fillRect(x, y, w, h);
            frameBufferGraphics.setColor(0, 0, 0);
            frameBufferGraphics.drawString(s1, canvasWidth / 2, y + 2, 17);
            frameBufferGraphics.drawString(s2, canvasWidth / 2, y + font.getHeight() + 1, 17);
            frameBufferGraphics.drawRect(x, y, w - 1, h);
        }
        repaint();
    }

    public void paint(Graphics g)
    {
        g.drawImage(frameBuffer, 0, 0, 20);
    }

    protected void keyRepeated(int key)
    {
        keyPressed(key);
    }

    protected void keyPressed(int key)
    {
        switch(key)
        {
        case 50: // '2'
            inputBuffer = 1;
            break;

        case 52: // '4'
            inputBuffer = 2;
            break;

        case 54: // '6'
            inputBuffer = 5;
            break;

        case 56: // '8'
            inputBuffer = 6;
            break;

        case 49: // '1'
            inputBuffer = 10;
            break;

        case 55: // '7'
            inputBuffer = 11;
            break;

        case 51: // '3'
        case 53: // '5'
        default:
            inputBuffer = getGameAction(key);
            break;
        }
    }

    protected void pointerPressed(int x, int y)
    {
        if(x >= boardStartX && x < boardEndX && y >= boardStartY && y < boardEndY)
        {
            selectionX = (byte)((x - boardStartX) / cellWidth);
            selectionY = (byte)((y - boardStartY) / cellHeight);
            inputBuffer = 8;
        }
    }

    protected boolean processPlayerInput(int i)
    {
        if(currentMode == 5)
            switch(i)
            {
            case 1: // '\001'
                if(selectionY > 0)
                    selectionY--;
                inputBuffer = -1;
                return true;

            case 6: // '\006'
                if(selectionY < curNumCellsY - 1)
                    selectionY++;
                inputBuffer = -1;
                return true;

            case 2: // '\002'
                if(selectionX > 0)
                    selectionX--;
                inputBuffer = -1;
                return true;

            case 5: // '\005'
                if(selectionX < curNumCellsX - 1)
                    selectionX++;
                inputBuffer = -1;
                return true;

            case 8: // '\b'
            case 9: // '\t'
                if(minePosition[selectionX][selectionY] == 4)
                {
                    for(int x = 0; x < curNumCellsX; x++)
                    {
                        for(int y = 0; y < curNumCellsY; y++)
                            if(minePosition[x][y] == 4)
                            {
                                flagPosition[x][y] = 0;
                                boardImageGraphics.drawImage(mineImage, cellWidth * x, cellHeight * y, 20);
                            }

                    }

                    boardImageGraphics.drawImage(boomImage, cellWidth * selectionX, cellHeight * selectionY, 20);
                    currentMode = 6;
                    gameOver();
                    inputBuffer = -1;
                    return true;
                }
                played++;
                uncoverCell(selectionX, selectionY);
                for(int x = 0; x < curNumCellsX; x++)
                {
                    for(int y = 0; y < curNumCellsY; y++)
                        if(flagPosition[x][y] == -1 && minePosition[x][y] != 4)
                        {
                            inputBuffer = -1;
                            return true;
                        }

                }

                currentMode = 7;
                gameOver();
                inputBuffer = -1;
                return true;

            case 10: // '\n'
                if(flagPosition[selectionX][selectionY] == -1)
                {
                    flagPosition[selectionX][selectionY] = 16;
                    boardImageGraphics.drawImage(flagImage, cellWidth * selectionX, cellHeight * selectionY, 20);
                    bombsLeft--;
                    updateHud = true;
                } else
                if(flagPosition[selectionX][selectionY] == 16)
                {
                    flagPosition[selectionX][selectionY] = -1;
                    boardImageGraphics.drawImage(button, cellWidth * selectionX, cellHeight * selectionY, 20);
                    bombsLeft++;
                    updateHud = true;
                }
                inputBuffer = -1;
                return true;

            case 11: // '\013'
                if(flagPosition[selectionX][selectionY] != -1 && flagPosition[selectionX][selectionY] != 16 && minePosition[selectionX][selectionY] != 4 && countNumCloseMines(selectionX, selectionY) <= countNumCloseFlags(selectionX, selectionY))
                    uncoverNeighbors(selectionX, selectionY);
                inputBuffer = -1;
                return true;
            }
        inputBuffer = -1;
        return false;
    }

    protected byte countNumCloseMines(byte x, byte y)
    {
        byte numCloseMines = 0;
        for(byte dx = -1; dx <= 1; dx++)
        {
            byte cx = (byte)(dx + x);
            if(cx < 0 || cx >= curNumCellsX)
                continue;
            for(byte dy = -1; dy <= 1; dy++)
            {
                if(dx == 0 && dy == 0)
                    continue;
                byte cy = (byte)(dy + y);
                if(cy >= 0 && cy < curNumCellsY && minePosition[cx][cy] == 4)
                    numCloseMines++;
            }

        }

        return numCloseMines;
    }

    protected byte countNumCloseFlags(byte x, byte y)
    {
        byte num = 0;
        for(byte dx = -1; dx <= 1; dx++)
        {
            byte cx = (byte)(dx + x);
            if(cx < 0 || cx >= curNumCellsX)
                continue;
            for(byte dy = -1; dy <= 1; dy++)
            {
                if(dx == 0 && dy == 0)
                    continue;
                byte cy = (byte)(dy + y);
                if(cy >= 0 && cy < curNumCellsY && flagPosition[cx][cy] == 16)
                    num++;
            }

        }

        return num;
    }

    protected void uncoverCell(byte x, byte y)
    {
        if(flagPosition[x][y] == -1)
        {
            byte numNeighbors = countNumCloseMines(x, y);
            flagPosition[x][y] = numNeighbors;
            boardImageGraphics.drawImage(numberImages[numNeighbors], x * cellWidth, y * cellHeight, 20);
            if(numNeighbors == 0)
            {
                uncoverNeighbors(x, y);
                return;
            }
        }
    }

    protected void uncoverNeighbors(byte x, byte y)
    {
        for(byte dx = -1; dx <= 1; dx++)
        {
            byte cx = (byte)(dx + x);
            if(cx < 0 || cx >= curNumCellsX)
                continue;
            for(byte dy = -1; dy <= 1; dy++)
            {
                if(dx == 0 && dy == 0)
                    continue;
                byte cy = (byte)(dy + y);
                if(cy >= 0 && cy < curNumCellsY)
                    uncoverCell(cx, cy);
            }

        }

    }

    protected void gameOver()
    {
        minesMidlet.animator.disable();
        for(byte x = 0; x < curNumCellsX; x++)
        {
            for(byte y = 0; y < curNumCellsY; y++)
                if(minePosition[x][y] == 4 && flagPosition[x][y] == -1)
                    flagPosition[x][y] = 16;

        }

        addCommand(restartCommand);
        if(currentMode == 7)
        {
            repaintAll();
            minesMidlet.highScore.processNewScore(timeElapsed, this);
        } else
        {
            repaintAll();
        }
        lastEventTime = System.currentTimeMillis();
    }

//    static 
//    {
//        UNKNOWN = -1;
//        FLAGGED = 16;
//        UNCOVERED = 0;
//        CLEAR = 3;
//        MINE = 4;
//        PLAYING = 5;
//        GAMEOVER = 6;
//        COMPLETED = 7;
//    }
}

⌨️ 快捷键说明

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