功能实现

  1. 敌人绘制
  2. 属性绘制
  3. 出牌

敌人绘制

我们自定义一个敌人类,继承自玩家类

Enemy.h

#pragma once
#include "Player.h"

using namespace sf;
class Enemy :public Player {
public:
    int name;                        //名字
    Texture tShape;                    //敌人纹理
    Sprite shape;                    //敌人精灵
    void setPosition(Vector2f);        //设定绘制位置
    void setName(int);                //设定名字
};

Enemy.cpp

#include "Enemy.h"
void Enemy::setName(int num) {
    name = num;
    switch (name) {
        case carpente:
            tShape.loadFromFile("./data/enemy/carpente.png");
            shape.setTexture(tShape);
            break;
        case prisone:
            tShape.loadFromFile("./data/enemy/prisone.png");
            shape.setTexture(tShape);
            break;
        default:
            break;
    }
}

void Enemy::setPosition(Vector2f pos) {
    shape.setPosition(pos);
}

这样在实例化敌人对象的时候name不同加载不同的贴图

然后Game.cpp中

void Game::drawEnemy() {
    enemyCarpente.shape.setPosition(680, 10);
    window.draw(enemyCarpente.shape);
    sEnemyUI.setPosition(635, 300);
    window.draw(sEnemyUI);
}

最后在drawFight中调用

效果如下

属性绘制

接下来进行属性的绘制

在Player.h中添加一些属性

class Player {
public:
    Texture tNums[10];            //数字纹理
    int hp;                        //血量
    int fullHp;                    //满血血量
    int mp;                        //蓝量
    int movePower;                //行动力
    void drawState(RenderWindow*);            //更新状态
    int cardPoolNum;            //卡池中卡牌数量
    Sprite sMovePower;            //行动力
    Texture tHpBar;                //血量纹理
    Texture tMpBar;                //蓝量
    Sprite hpNum[2], hpBar;        //血量精灵
    Sprite mpNum[2], mpBar;        //蓝量
    void loadMedia();
    Player();
};

在Player.cpp中完善

Player::Player() {

    getCardState = true;                        //可否抽牌
    handCardNums = 0;                            //目前手牌数目
    getCardNum = 5;                                //抽牌数目
    cardSelect = -1;                            //没有进行卡牌交互
    fullHp = 20;
    hp = fullHp;
    mp = 5;
    movePower = 1;
    nullCard.cardState = nullCard.null;
    for (int i = 0; i < handMaxNum; i++) {        //填充手牌数组
        handCards[i] = &nullCard;
    }
    hpNum[0].setScale(0.7, 0.7);
    hpNum[1].setScale(0.7, 0.7);
    mpNum[0].setScale(0.7, 0.7);
    mpNum[1].setScale(0.7, 0.7);
    hpNum[0].setPosition(640, 868);
    hpNum[1].setPosition(620, 868);
    mpNum[0].setPosition(920, 868);
    mpNum[1].setPosition(900, 869);
    hpBar.setPosition(780, 870);
    mpBar.setPosition(1028, 870);
    sMovePower.setPosition(1080, 860);
}
void Player::loadMedia() {
    tHpBar.loadFromFile("./data/ui/kong.png");    //设定贴图
    tMpBar.loadFromFile("./data/ui/skong.png");
    for (int i = 0; i < 10; i++) {
        stringstream ss;
        ss << "./data/ui/" << i << ".png";
        tNums[i].loadFromFile(ss.str());
    }
    hpBar.setTexture(tHpBar);
    mpBar.setTexture(tMpBar);
}
void Player::drawState(RenderWindow* window) {
    //遮挡条在右上角
    mp <= 10 ? mpBar.setScale(-1 * (1 - mp / (float)10), 1) : mpBar.setScale(0, 1);            //mp增加,遮挡条变短
    hpBar.setScale(-1 * (1 - hp / (float)fullHp), 1);

    window->draw(mpBar);
    window->draw(hpBar);

    if (hp > 10) {
        hpNum[1].setTexture(tNums[hp / 10]);
        window->draw(hpNum[1]);
    }
    hpNum[0].setTexture(tNums[hp % 10]);
    window->draw(hpNum[0]);


    if (mp > 10) {
        mpNum[1].setTexture(tNums[mp / 10]);
        window->draw(mpNum[1]);
    }
    mpNum[0].setTexture(tNums[mp % 10]);
    window->draw(mpNum[0]);

}

Enemy类中增加

class Enemy :public Player {
public:
    void loadMedia();
    Enemy();
};

Enemy.cpp

#include "Enemy.h"
#include <iostream>
#include <sstream>
using namespace std;
Enemy::Enemy() {

    hpNum[0].setScale(0.7, 0.7);
    hpNum[1].setScale(0.7, 0.7);
    mpNum[0].setScale(0.7, 0.7);
    mpNum[1].setScale(0.7, 0.7);
    hpNum[0].setPosition(800, 330);
    hpNum[1].setPosition(780, 330);
    mpNum[0].setPosition(800, 350);
    mpNum[1].setPosition(780, 350);
    hpBar.setPosition(913, 334);
    mpBar.setPosition(867, 354);
    sMovePower.setScale(0.8, 0.8);
    sMovePower.setPosition(888, 346);
}
void Enemy::loadMedia() {
    tHpBar.loadFromFile("./data/ui/blong.png");    //设定贴图
    tMpBar.loadFromFile("./data/ui/bshort.png");
    for (int i = 0; i < 10; i++) {
        stringstream ss;
        ss << "./data/ui/" << i << ".png";
        tNums[i].loadFromFile(ss.str());
    }
    hpBar.setTexture(tHpBar);
    mpBar.setTexture(tMpBar);
    sMovePower.setTexture(tNums[1]);
}

最后在Game.cpp中调用

void Game::drawPlayer() {
    sPlayUI.setPosition(0, 700);
    window.draw(sPlayUI);
    humanPlayer.drawState(&window);
}
void Game::drawEnemy() {
    enemyCarpente.shape.setPosition(680, 10);
    window.draw(enemyCarpente.shape);
    sEnemyUI.setPosition(635, 300);
    window.draw(sEnemyUI);
    enemyCarpente.drawState(&window);
}

出牌

给敌人增加一个检测鼠标位置的函数

bool Enemy::checkMouse(Vector2f mouse) {
    return ((mouse.x > shape.getPosition().x && mouse.x < shape.getPosition().x + shape.getTexture()->getSize().x) &&
        (mouse.y > shape.getPosition().y && mouse.y < shape.getPosition().y + shape.getTexture()->getSize().y)) ? true : false;
}

出牌函数

void Player::useCardTo(Card* card, Player* player) {
    card->cardState = card->disCardPool;
    handCardNums--;
    player->hp--;
}

在交互事件中

for (int i = 0; i < humanPlayer.handCardNums; i++) {
    switch (humanPlayer.handCards[i]->checkMouse(mousePosition, event, cardOffset.x, cardOffset.y)) {        //卡片事件
        case 3:
            if (enemyCarpente.checkMouse(mousePosition)) {
                        attackSd.play();                                                    //播放攻击音乐
                        humanPlayer.useCardTo(humanPlayer.handCards[i], &enemyCarpente);    //对敌人使用卡牌
                        humanPlayer.handCards[i] = &humanPlayer.nullCard;                    //设定使用后的手牌为null
                    } else {
                        humanPlayer.handCards[i]->setState(humanPlayer.handCards[i]->NORMAL);        //释放后设定为普通状态
                    }
            break;
    }
}

目前为止的效果

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