功能实现

  1. 方块交互
  2. 按钮交互
  3. 胜利判断

鼠标按键

左键单击

void Game::lButtonDown(Vector2i mPoint) {
    int i, j;
    i = (mPoint.x - mCornPoint.x) / gridSize;    //获取当前点击块的位置
    j = (mPoint.y - mCornPoint.y) / gridSize;
    if (i >= 0 && i < stageWidth && j >= 0 && j < stageHeight) {    //点击了舞台范围
        if (!isGameBegin) {
            isGameBegin = true;    //游戏开始
            mineSet(j, i);                //点击后随机布雷
        }
        if (mGameData[j][i].mState != ncFLAG) {    //点击的不是旗子
            if (!mGameData[j][i].isPress) {
                mGameData[j][i].isPress = true;    //块被点击
                if (mGameData[j][i].mState == ncMINE) {    //点击的是雷
                    isGameBegin = false;
                    isGameOverState = ncLose;
                    mGameData[j][i].mState = ncBOOM;
                    unCover();                //揭开剩下未被找到的雷
                }
            }
        }
        if (mGameData[j][i].mState == ncNull) {        //点击的块为空
            nullCick(j, i);                //查找未被点击的空块
        }
    }
}//鼠标左击

查找空块

void Game::nullCick(int j, int i) {    //查找空块
    int k, l;
    for (k = j - 1; k < j + 2; k++) {
        for (l = i - 1; l < i + 2; l++) {
            if (k >= 0 && k < stageHeight && l >= 0 && l < stageWidth) {
                if (!mGameData[k][l].isPress) {
                    mGameData[k][l].isPress = true;
                    if (mGameData[k][l].mState == ncNull) {
                        nullCick(k, l);                //递归调用
                    }
                }
            }
        }
    }
}

揭开剩下的雷

void Game::unCover() {
    int i, j;
    for (j = 0; j < stageHeight; j++) {
        for (i = 0; i < stageWidth; i++) {
            if (!mGameData[j][i].isPress) {
                if (mGameData[j][i].mState == ncMINE) {
                    mGameData[j][i].isPress = true;
                    mGameData[j][i].mState = ncUNFOUND;
                }
            }
        }
    }
}

左键双击

void Game::lButtonDoubleClick(Vector2i mPoint) {
    int i, j, k, l, lvl;
    i = (mPoint.y - mCornPoint.y) / gridSize;
    j = (mPoint.x - mCornPoint.x) / gridSize;
    if (i >= 0 && i < stageWidth && j >= 0 && j < stageHeight) {
        if (mGameData[j][i].isPress) {
            if (mGameData[j][i].mState != ncFLAG) {
                for (k = j - 1; k < j + 2; k++) {
                    for (l = i - 1; l < i + 2; l++) {
                        if (k >= 0 && k < stageHeight && l >= 0 && l < stageWidth) {
                            if (mGameData[j][i].mState == ncFLAG) {
                                if (mGameData[j][i].mStateBackUp != ncMINE) {
                                    isGameOverState = ncLose;
                                    isGameBegin = false;
                                    unCover();
                                }
                            } else {
                                if (!mGameData[k][l].isPress) {
                                    mGameData[k][l].isPress = true;
                                    if (mGameData[k][l].mState == ncMINE) {
                                        isGameOverState = ncLose;
                                        isGameBegin = false;
                                        mGameData[k][l].mState = ncBOOM;
                                        unCover();
                                    }
                                    if (mGameData[k][l].mState == ncNull) {
                                        nullCick(k, l);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}    //鼠标左键双击

右键单击

void Game::rButtonDown(Vector2i mPoint) {
    int i, j;
    i = (mPoint.x - mCornPoint.x) / gridSize;
    j = (mPoint.y - mCornPoint.y) / gridSize;
    if (i>=0&&i<stageWidth&&j>=0&&j<stageHeight) {
        if (!mGameData[j][i].isPress) {
            mGameData[j][i].isPress = true;
            mGameData[j][i].mStateBackUp = mGameData[j][i].mState;
            mGameData[j][i].mStateBackUp = ncFLAG;
            mFlagCalc++;
        } else {
            if (mGameData[j][i].mState==ncFLAG) {
                mGameData[j][i].isPress = true;
                mGameData[j][i].mState = ncQ;
                mFlagCalc--;
            } else if (mGameData[j][i].mState==ncQ) {
                mGameData[j][i].isPress = false;
                mGameData[j][i].mState = mGameData[j][i].mStateBackUp;

            }
        }
    }
}    //鼠标右击

按钮交互

修改Input函数

if (event.type == sf::Event::EventType::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left) {
    if (isGameOverState == ncNo) {
        mouseClickTimer.restart();
        if (!isGameBegin) {
            if (buttonRectEasy.contains(event.mouseButton.x, event.mouseButton.y)) {
                gameLVL = 1;
            }
            if (buttonRectNormal.contains(event.mouseButton.x, event.mouseButton.y)) {
                gameLVL = 2;
            }
            if (buttonRectHard.contains(event.mouseButton.x, event.mouseButton.y)) {
                gameLVL = 3;
            }
            Initial();        //刷新舞台
        }
        if (buttonRectBG.contains(event.mouseButton.x, event.mouseButton.y)) {
            imgBGNo++;
            if (imgBGNo > 7) {        //背景图轮换
                imgBGNo = 1;
            }
            loadMediaData();
        }
        if (buttonRectSkin.contains(event.mouseButton.x, event.mouseButton.y)) {
            imgSkinNo++;
            if (imgSkinNo > 6) {        //皮肤轮换
                imgSkinNo = 1;
            }
            loadMediaData();
        }
    }
    if (buttonRectRestart.contains(event.mouseButton.x, event.mouseButton.y)) {
        Initial();
    }
    if (buttonRectQuit.contains(event.mouseButton.x, event.mouseButton.y)) {
        window.close();
        gameQuit = true;
    }
}

胜利判断

void Game::Logic() {
    isWin();
}
void Game::isWin() {
    int i, j, c = 0;
    if (mFlagCalc == mMineNum) {        //判断插的旗是否都是雷
        for (i = 0; i < stageWidth; i++) {
            for (j = 0; j < stageHeight; j++) {
                if (mGameData[j][i].mState == ncFLAG) {
                    if (mGameData[j][i].mStateBackUp == ncMINE) {
                        c++;
                    }
                }
            }
        }
    } else {
        if (isGameOverState != ncLose) {
            for (i = 0; i < stageWidth; i++) {
                for (j = 0; j < stageHeight; j++) {
                    if (!mGameData[j][i].isPress || mGameData[j][i].mState == ncFLAG) {
                        c++;
                    }
                }
            }
        }
    }
    if (c == mMineNum) {
        isGameBegin = false;
        isGameOverState = ncWin;
    }
}
Last modification:July 12th, 2020 at 01:06 am