QGraphic学习笔记 - 图文

一个小的简单的游戏引出的心得体会

分解实例1 实例描述:

实现一个按钮,按钮继承自QGraphicObject,

头文件:

class myButton : public QGraphicsObject {

Q_OBJECT public:

myButton(int ,int,QString ,QString ,class MainWindow *); QRectF boundingRect() const; void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); protected:

void hoverEnterEvent ( QGraphicsSceneHoverEvent *) ; void hoverLeaveEvent ( QGraphicsSceneHoverEvent *) ; void mousePressEvent ( QGraphicsSceneMouseEvent *); private:

QPixmap pix; QString img;

QString hoverImg; class MainWindow * m;

Phonon::MediaObject *clickMic; signals:

void clickSig(); };

源文件:

myButton::myButton(int x,int y,QString img,QString hoverImg,class MainWindow * m){ this->img=img;

this->hoverImg=hoverImg; pix.load(img);

setPos(x-pix.width()/2,y-pix.height()/2); this->m=m;

setFlags(QGraphicsItem::ItemIsFocusable); setFlags(QGraphicsItem::ItemIsMovable); setAcceptHoverEvents(true);

clickMic=Phonon::createPlayer(Phonon::MusicCategory,

Phonon::MediaSource(\));

}

QRectF myButton::boundingRect() const{

return QRectF(0, 0, pix.width(), pix.height()); }

void myButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

painter->drawPixmap(0,0,pix.width(), pix.height(),pix); }

void myButton::hoverEnterEvent ( QGraphicsSceneHoverEvent *) { setFocus(Qt::MouseFocusReason);

pix.load(hoverImg); clickMic->play(); update(); }

void myButton::hoverLeaveEvent ( QGraphicsSceneHoverEvent *) { setFocus(Qt::MouseFocusReason); pix.load(img); update(); }

void myButton::mousePressEvent ( QGraphicsSceneMouseEvent *){ emit clickSig(); }

引用文件:

cjScene = new QGraphicsScene;//新建场景指针 cjScene->clear(); myButton *sbtn=new

myButton(width/2,height/2-200,\,\tn2.png\,this);

connect(sbtn,SIGNAL(clickSig()),this,SLOT(guanKaScene())); cjScene->addItem(sbtn); myButton *soundBtn=new

myButton(width/2,height/2,\,\png\,this);

cjScene->addItem(soundBtn); myButton *quitBtn=new

myButton(width/2,height/2+200,\,\2.png\,this);

connect(quitBtn,SIGNAL(clickSig()),this,SLOT(close())); cjScene->addItem(quitBtn);

cjScene->setSceneRect(0,0,width,height);

cjScene->setBackgroundBrush( QPixmap(\).scaled(width,height));

this->setScene(cjScene);

绘制动画精灵1:

void ninjia::play(){

QTimeLine* timeline=new QTimeLine(200); timeline->setFrameRange(1, 4);

connect(timeline,SIGNAL(frameChanged(int)),this,SLOT(move_play(int)));

timeline->start(); }

void ninjia::move_play(int i){ qDebug()<

QString path=\; pix.load(path); return; }

QString path=\+QString::number(i)+\; pix.load(path); }

绘制动画精灵2:

timeline=new QTimeLine(1000);

timeline->setFrameRange(1, maxFrame); timeline->setLoopCount(0);

connect(timeline,SIGNAL(frameChanged(int)),this, SLOT(move_play(int))); timeline->start();

void target::move_play(int i){ qDebug()<

QString path=imgPath+QString::number(i)+\; pix.load(path); }

精灵的碰撞检测:

bool target::isColliding() {

QList item_list = collidingItems(); QGraphicsItem *item;

foreach(item, item_list) {

if(item->data(1)==\){ item->stackBefore(this); }

if((item->data(1)==\||item->data(1)==\)&&(this->boundingRect().y()+this->pos().y()+this->boundingRect().height())<(item->boundingRect().y()+item->pos().y()+item->boundingRect().height())){ this->stackBefore(item); }

if(item->data(1)==\&&item->data(2)!=false){ life=life-((projectile*)item)->gePower(); m->deleteItem((projectile*)item); item->setData(2,false); if(life>0){return true;} m->updateScore(10); this->setData(2,false); return true; } }

return false; }

子弹的发射

void myScene::mousePressEvent( QGraphicsSceneMouseEvent * e ){ projectile *p=new

projectile(x,y,m,angleValue,6,\,m->getAttack()); this->addItem(p); pewMic->pause(); pewMic->play();

QGraphicsScene::mousePressEvent(e); }

子弹的自动移动与移除:

void projectile::advance(int i){

moveBy(cos(angleValue)*speed,sin(angleValue)*speed);

if(this->data(2)==false||pos().x()>this->m->scene()->width()||pos().y()>this->m->scene()->height()||pos().y()<0||pos().x()<0){ drop();//移除本身removeItem(this);

} }

子项目自动运行函数:

void basePropertys::advance(int ){ isColliding(); }

场景中设置文字:

gameScene->setSceneRect(0,0,width,height); setScene(gameScene);

game_score=new QGraphicsTextItem(0, gameScene); game_score->setHtml(\ color = red>分

数:\+QString::number(score)+\);

game_score->setFont(QFont(\, 20, QFont::Bold));

game_score->setPos(10, 10);

game_attack=new QGraphicsTextItem(0, gameScene); game_attack->setHtml(\ color = red>攻击

力:\+QString::number(attackPower)+\);

game_attack->setFont(QFont(\, 20, QFont::Bold)); game_attack->setPos(10, 70);

life_text =new QGraphicsTextItem(0, gameScene); life_text->setHtml(\ color = red>生命:

\+QString::number(attackPower)+\);

life_text->setFont(QFont(\, 20, QFont::Bold));

life_text->setPos(10, 40);

bB=new bloodBar(80,50,this);

gameScene->addItem(bB);

Animation 动画

方法一、items 继承QGraphicsObject,并联合使用QPropertyAnimation

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4