功能实现

  1. 完善卡牌绘制
  2. 完善随机抽牌

卡牌加载

我们使用循环将文件夹里的图片依次加载到纹理之中,因为素材有点多,所以我们加载需要一些时间,我们先加载十张,看看是不是从十张的卡池中随机抽牌

void Game::loadCards() {
    stringstream ssNormalCard;
    stringstream ssHoverCard;
    stringstream ssClickCard;
    for (int j = 0; j < 10; j++) {
        ssNormalCard << "data/card1/card" << j << ".png";
        tCard1[0][j].loadFromFile(ssNormalCard.str());
        ssHoverCard << "data/hoverCard1/card" << j << ".png";
        tCard1[1][j].loadFromFile(ssHoverCard.str());
        ssClickCard << "data/clickCard1/card" << j << ".png";
        tCard1[2][j].loadFromFile(ssClickCard.str());
        ssNormalCard.str("");
        ssHoverCard.str("");
        ssClickCard.str("");
        cards[j].setTextures(tCard1[0][j], tCard1[1][j], tCard1[2][j]);
    }
}

在input中绑定鼠标事件

    for (int i = 0; i < humanPlayer.handCardNums; i++) {
        if (humanPlayer.handCards[i]->cardState != humanPlayer.handCards[i]->null) {
            humanPlayer.handCards[i]->checkMouse(mousePosition, event);        //卡片事件
        }
    }

卡牌绘制

绘制也一样,写成循环

void Game::Draw() {
    switch (gameSceneState) {        //场景判断
        case SCENE_START:
            drawStart();
            break;
        case SCENE_FIGHT:
            if (humanPlayer.getCardState) {
                humanPlayer.getCard(cards, humanPlayer.getCardNum, sizeof(cards) / sizeof(cards[0]));                    //抽牌  获取抽牌数量
                for (int i = 0; i < humanPlayer.handCardNums; i++) {
                    humanPlayer.handCards[i]->setState(0);        //设置贴图为普通状态
                }
                humanPlayer.getCardState = false;
            }
            drawFight();
            break;
        default:
            break;
    }
}
void Game::drawCard() {
    for (int i = 0; i < humanPlayer.handCardNums; i++) {
        humanPlayer.handCards[i]->setPosition(200 + i * 200, 600);
        window.draw(*humanPlayer.handCards[i]);
    }
    window.display();                        //展示屏幕
}

随机抽牌

我们重写之前写的抽牌函数,因为有一些bug

洗牌

void Player::randCardPool(Card card[], int cardsLength) {
    srand(time(0));
    for (int j = 0; j < cardsLength; j++) {
        int index = rand() % cardsLength;
        if (index != j) {
            Card temp = card[j];
            card[j] = card[index];
            card[index] = temp;
        }                                        //洗牌
    }
}

抽牌

void Player::getCard(Card card[], int getNum, int cardLength) {
    randCardPool(card, cardLength);
    for (int i = 0; i < getNum; i++) {        //从 handCards[手牌数] 到 handCards[手牌数+抽牌数]给handCards赋值
        for (int k = 0; k < cardLength; k++) {
            if (card[k].cardState == card[k].cardPool) {                                    //如果卡牌在卡池里就抽牌
                card[k].cardState = 1;                                        //卡牌移到手牌区
                handCardNums += 1;                                                //手牌数加一
                handCards[handCardNums - 1 + i] = &card[k];                            //手牌数组赋值
                getNum--;                        //抽牌数-1
            }
            if (!getNum) {                    //抽牌数减为0
                return;
            }
        }
    }
}

最后来看一下效果

Last modification:February 12th, 2021 at 06:14 am