Qt 游戏之路(三) 战车和炮的移动时旋转

人物类

#ifndef PLAYER_H
#define PLAYER_H

#include "info.h"
#include "move_thing_base.h"

class Player : public Move_Thing_Base
{
    Q_OBJECT

public:
    Player();
    void Init(){}
    QPointF center();
    void fixDirect();
    void fixPao(qreal x, qreal y);
    void moveto(QPointF pos);
    void moveto(qreal x, qreal y);
    ~Player() {}


};

#endif // PLAYER_H

人物类实现


#include "info.h"
#include "player.h"

//Player
Player::Player()
{
    speed = 45;
    extern_info = 0; //表示主角

    item = new GraphicsItem;
    text = new QGraphicsSimpleTextItem;
    text->setBrush(QBrush(QColor(0,0,255,255)));
    text->setText("    闲云野鹤");
    text->setPos(0,-20);
    text->setParentItem(item);
    pi = new QGraphicsPixmapItem[2];
    pi[0].setPixmap(QPixmap(":/pic/tanke1/3.png"));
    pi[0].setParentItem(item);
    pi[1].setPixmap(QPixmap(":/pic/pao1/3.png"));
    pi[1].setParentItem(item);
    pi[1].setPos(center().x() - 23, center().y() - 19);
    scene->addItem(item);
    setPos(123, 345);

}

QPointF Player::center()
{
    return QPointF(cpos.x() + 57, cpos.y() + 23);
}

void Player::moveto(QPointF pos)
{
    moveto(pos.x(), pos.y());
}

void Player::moveto(qreal x, qreal y)
{
    Move_Thing_Base::moveto(x,y);
    fixDirect();
    fixPao(x,y);

}


void Player::fixDirect()
{
    qreal x = arrivepos.x() - center().x();
    qreal y = center().y() - arrivepos.y(); // -(arrivepos.y() - center().y())
    qreal yt = 100;  //qreal xt = 0;   正向量
    qreal angle = qAcos((y*yt)/(qSqrt(x*x+y*y)*yt)) * 53.7;
    int part = angle/22.5;
    if (x >= 0)
    {
        switch(part)
        {
        case 0:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/1.png"));
            break;
        case 1:
        case 2:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/2.png"));
            break;
        case 3:
        case 4:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/3.png"));
            break;
        case 5:
        case 6:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/4.png"));
            break;
        default:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/5.png"));
        }
    }
    else
    {
        switch(part)
        {
        case 0:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/1.png"));
            break;
        case 1:
        case 2:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/8.png"));
            break;
        case 3:
        case 4:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/7.png"));
            break;
        case 5:
        case 6:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/6.png"));
            break;
        default:
            pi[0].setPixmap(QPixmap(":/pic/tanke1/5.png"));
        }
    }
}

void Player::fixPao(qreal ax,qreal ay)
{
    qreal x = ax - center().x();
    qreal y = center().y() - ay; // -(arrivepos.y() - center().y())
    qreal yt = 100;  //qreal xt = 0;   正向量
    qreal angle = qAcos((y*yt)/(qSqrt(x*x+y*y)*yt)) * 53.7;
    int part = angle/22.5;
    if (x >= 0)
    {
        switch(part)
        {
        case 0:
            pi[1].setPixmap(QPixmap(":/pic/pao1/1.png"));
            break;
        case 1:
        case 2:
            pi[1].setPixmap(QPixmap(":/pic/pao1/2.png"));
            break;
        case 3:
        case 4:
            pi[1].setPixmap(QPixmap(":/pic/pao1/3.png"));
            break;
        case 5:
        case 6:
            pi[1].setPixmap(QPixmap(":/pic/pao1/4.png"));
            break;
        default:
            pi[1].setPixmap(QPixmap(":/pic/pao1/5.png"));
        }
    }
    else
    {
        switch(part)
        {
        case 0:
            pi[1].setPixmap(QPixmap(":/pic/pao1/1.png"));
            break;
        case 1:
        case 2:
            pi[1].setPixmap(QPixmap(":/pic/pao1/8.png"));
            break;
        case 3:
        case 4:
            pi[1].setPixmap(QPixmap(":/pic/pao1/7.png"));
            break;
        case 5:
        case 6:
            pi[1].setPixmap(QPixmap(":/pic/pao1/6.png"));
            break;
        default:
            pi[1].setPixmap(QPixmap(":/pic/pao1/5.png"));
        }
    }
}


Qt 游戏之路(三) 战车和炮的移动时旋转,布布扣,bubuko.com

Qt 游戏之路(三) 战车和炮的移动时旋转

上一篇:IOS 上传图片 ASIFormDataRequest


下一篇:Android中通过NTP服务器获取时间功能源码分析