功能实现

  1. 轮流出牌
  2. 游戏结束(胜利,失败)

轮流出牌

在Game.h中定义变量来控制回合轮转

enum e_whosTurn {                    //谁的回合
    ePlayerTurn, eEnemyTurn
};
int whosTurn = 0;                //默认玩家回合

按下回合结束按钮地方抽牌,出牌

void Game::fightInput(Vector2i mousePosition, Event event) {
    switch (turnEnd.checkMouse(mousePosition, event, 0, 0)) {
        case 1:
            hoverSd.play();
            break;
        case 3:
            releaseSd.play();
            whosTurn = eEnemyTurn;                                    //敌人回合
            enemyCarpente.getCardState = true;                        //可摸牌
            enemyCarpente.useCardNum = enemyCarpente.getCardNum;    //设定可用牌数量
            turnEnd.setState(turnEnd.NORMAL);
            break;
    }
}

敌人出牌的绘制

void Game::drawEnemy() {
    if (whosTurn == eEnemyTurn && !gameLose) {
        enemyCarpente.showCard(&window, &humanPlayer);
        if (enemyCarpente.cardShow.cardState != enemyCarpente.cardShow.null) {
            enemyCarpente.cardShow.setPosition(enemyCarpente.cardPosition);
            window.draw(enemyCarpente.cardShow);
        }
    }
}

为了方便管理,我把这部分写在logic函数中

void Game::Logic() {
    switch (gameSceneState) {
        case SCENE_FIGHT:
            if (humanPlayer.hp <= 0) {
                gameLose = true;
                enemyCarpente.cardShow = enemyCarpente.nullCard;
            }
            if (enemyCarpente.hp <= 0) {
                gameWin = true;
            }
            if (whosTurn == eEnemyTurn) {
                if (enemyCarpente.getCardState) {
                    enemyCarpente.getCard(enemyCards, enemyCarpente.getCardNum, sizeof(enemyCards) / sizeof(enemyCards[0]));                //抽牌  获取抽牌数量
                    enemyCarpente.getCardState = false;
                }
                if (enemyCarpente.useCardNum == 1) {
                    enemyCarpente.lastCardClock.restart();                                                    //启动计时器
                }
                if (enemyCarpente.useCardNum == 0) {
                    if (enemyCarpente.lastCardClock.getElapsedTime().asMilliseconds() > 1600) {                //获取最后一张牌展示时间
                        whosTurn = ePlayerTurn;                                                                //玩家回合
                        humanPlayer.getCardState = true;                                                    //玩家可抽牌
                        enemyCarpente.cardShow = enemyCarpente.nullCard;                                    //设定展示卡牌为null
                    }
                }
            } else if (whosTurn == ePlayerTurn && !gameLose) {
                if (humanPlayer.getCardState) {
                    humanPlayer.getCard(playerCards, humanPlayer.getCardNum, sizeof(playerCards) / sizeof(playerCards[0]));                //抽牌  获取抽牌数量
                    humanPlayer.getCardState = false;
                    for (int i = 0; i < humanPlayer.handCardNums; i++) {
                        humanPlayer.handCards[i]->setState(0);        //设置贴图为普通状态
                    }
                }
            }
            break;
    }
}

效果如下

游戏结束

首先肯定是Texture和Sprite以及Button初始化了~

做完这些后,我们进行游戏胜利与游戏失败画面的绘制

在对话框绘制的函数中增加判断

void Game::drawDialog() {
    if (showBackDialog) {
        window.draw(sDialog);
        window.draw(yesBtn);
        window.draw(noBtn);
    }
    if (gameLose) {
        window.draw(gmLoseDialog);
        window.draw(gmOverReplayBtn);
        window.draw(gmOvertoMenuBtn);
    }
    if (gameWin) {
        window.draw(gmWinDialog);
    }
}

在fightInput函数中增加失败或胜利界面绘制出来后的交互,此时只允许进行与失败/胜利对话框的交互

void Game::fightInput(Vector2i mousePosition, Event event) {

    if (gameWin) {
        if (event.type == Event::EventType::MouseButtonPressed && event.mouseButton.button == Mouse::Left) {
            gameSceneState = SCENE_START;                //切换场景
            gameWin = false;                            //
        }
    } else if (gameLose) {
        switch (gmOverReplayBtn.checkMouse(mousePosition, event)) {
            case 3:
                initFightData();
                gmOverReplayBtn.setState(gmOverReplayBtn.NORMAL);
        }
        switch (gmOvertoMenuBtn.checkMouse(mousePosition, event)) {
            case 3:
                gameLose = false;
                gameSceneState = SCENE_START;                                    //切换场景
                gmOvertoMenuBtn.setState(gmOvertoMenuBtn.NORMAL);                //设定为普通状态
                loadMusic();                                                    //切换音乐
        }
    } else{
    ///
    }
}

效果如下

Bug修复

当敌方出牌时我们不能出牌,所以要在玩家出牌的所有事件处理加上一个前提条件

回合结束按钮的处理也一样放到这个判断里

if (whosTurn == ePlayerTurn) {
    ///
}

当敌人还没出完牌我们就死掉的话,虽然会弹出失败的对话框,但是此时判定敌人已经出完牌了,又到了我们的回合,又会继续抽牌,在logic中给玩家判断一下是否已经gamelose

if (whosTurn == ePlayerTurn && !gameLose) {
                if (humanPlayer.getCardState) {
                    humanPlayer.getCard(playerCards, humanPlayer.getCardNum, sizeof(playerCards) / sizeof(playerCards[0]));                //抽牌  获取抽牌数量
                    humanPlayer.getCardState = false;
                    for (int i = 0; i < humanPlayer.handCardNums; i++) {
                        humanPlayer.handCards[i]->setState(0);        //设置贴图为普通状态
                    }
                }
            }

属性显示优化

之前不知道怎么想的,居然用图片来绘制数字,,,忘了还有Text类,把之前的属性绘制的部分改一改

Player.h中

Font textFont;            //文字的字体
Text hpText, mpText, moveText;        //文字

构造函数中

Player::Player() {
    textFont.loadFromFile("./data/font/simsun.ttc");
    hpText.setFont(textFont);
    hpText.setCharacterSize(20);
    hpText.setPosition(620, 864);
    mpText.setFont(textFont);
    mpText.setCharacterSize(20);
    mpText.setPosition(920, 864);
    moveText.setFont(textFont);
    moveText.setCharacterSize(20);
    moveText.setPosition(1028, 870);
}

draw函数中

void Player::drawState(RenderWindow* window) {
    hpText.setString(to_string(hp));
    mpText.setString(to_string(mp));
    moveText.setString(to_string(movePower));
    window->draw(hpText);
    window->draw(mpText);
    window->draw(moveText);
}
Last modification:February 12th, 2021 at 06:15 am