QGraphic学习笔记.docx

上传人:b****1 文档编号:2423399 上传时间:2022-10-29 格式:DOCX 页数:26 大小:932.24KB
下载 相关 举报
QGraphic学习笔记.docx_第1页
第1页 / 共26页
QGraphic学习笔记.docx_第2页
第2页 / 共26页
QGraphic学习笔记.docx_第3页
第3页 / 共26页
QGraphic学习笔记.docx_第4页
第4页 / 共26页
QGraphic学习笔记.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

QGraphic学习笔记.docx

《QGraphic学习笔记.docx》由会员分享,可在线阅读,更多相关《QGraphic学习笔记.docx(26页珍藏版)》请在冰豆网上搜索。

QGraphic学习笔记.docx

QGraphic学习笔记

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

分解实例1

实例描述:

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

头文件:

classmyButton:

publicQGraphicsObject

{

Q_OBJECT

public:

myButton(int,int,QString,QString,classMainWindow*);

QRectFboundingRect()const;

voidpaint(QPainter*,constQStyleOptionGraphicsItem*,QWidget*);

protected:

voidhoverEnterEvent(QGraphicsSceneHoverEvent*);

voidhoverLeaveEvent(QGraphicsSceneHoverEvent*);

voidmousePressEvent(QGraphicsSceneMouseEvent*);

private:

QPixmappix;

QStringimg;

QStringhoverImg;

classMainWindow*m;

Phonon:

:

MediaObject*clickMic;

signals:

voidclickSig();

};

 

源文件:

myButton:

:

myButton(intx,inty,QStringimg,QStringhoverImg,classMainWindow*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(":

/sound/click.mp3"));

}

QRectFmyButton:

:

boundingRect()const{

returnQRectF(0,0,pix.width(),pix.height());

}

voidmyButton:

:

paint(QPainter*painter,constQStyleOptionGraphicsItem*option,QWidget*widget){

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

}

voidmyButton:

:

hoverEnterEvent(QGraphicsSceneHoverEvent*){

setFocus(Qt:

:

MouseFocusReason);

pix.load(hoverImg);

clickMic->play();

update();

}

voidmyButton:

:

hoverLeaveEvent(QGraphicsSceneHoverEvent*){

setFocus(Qt:

:

MouseFocusReason);

pix.load(img);

update();

}

voidmyButton:

:

mousePressEvent(QGraphicsSceneMouseEvent*){

emitclickSig();

}

引用文件:

cjScene=newQGraphicsScene;//新建场景指针

cjScene->clear();

myButton*sbtn=newmyButton(width/2,height/2-200,":

images/startBtn1.png",":

images/startBtn2.png",this);

connect(sbtn,SIGNAL(clickSig()),this,SLOT(guanKaScene()));

cjScene->addItem(sbtn);

myButton*soundBtn=newmyButton(width/2,height/2,":

images/soundBtn1.png",":

images/soundBtn2.png",this);

cjScene->addItem(soundBtn);

myButton*quitBtn=newmyButton(width/2,height/2+200,":

images/quitBtn1.png",":

images/quitBtn2.png",this);

connect(quitBtn,SIGNAL(clickSig()),this,SLOT(close()));

cjScene->addItem(quitBtn);

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

cjScene->setBackgroundBrush(QPixmap(":

/images/panelBg.bmp").scaled(width,height));

this->setScene(cjScene);

绘制动画精灵1:

voidninjia:

:

play(){

QTimeLine*timeline=newQTimeLine(200);

timeline->setFrameRange(1,4);

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

timeline->start();

}

voidninjia:

:

move_play(inti){

qDebug()<

if(i==4){

QStringpath=":

images/ninjia/1.png";

pix.load(path);

return;

}

QStringpath=":

images/ninjia/"+QString:

:

number(i)+".png";

pix.load(path);

}

绘制动画精灵2:

timeline=newQTimeLine(1000);

timeline->setFrameRange(1,maxFrame);

timeline->setLoopCount(0);connect(timeline,SIGNAL(frameChanged(int)),this,

SLOT(move_play(int)));

timeline->start();

voidtarget:

:

move_play(inti){

qDebug()<

QStringpath=imgPath+QString:

:

number(i)+".png";

pix.load(path);

}

 

精灵的碰撞检测:

booltarget:

:

isColliding(){

QListitem_list=collidingItems();

QGraphicsItem*item;

foreach(item,item_list){

if(item->data

(1)=="property"){

item->stackBefore(this);

}

if((item->data

(1)=="ninijia"||item->data

(1)=="target")&&(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)=="projectile"&&item->data

(2)!

=false){

life=life-((projectile*)item)->gePower();

m->deleteItem((projectile*)item);

item->setData(2,false);

if(life>0){returntrue;}

m->updateScore(10);

this->setData(2,false);

returntrue;

}

}

returnfalse;

}

 

子弹的发射

voidmyScene:

:

mousePressEvent(QGraphicsSceneMouseEvent*e){

projectile*p=newprojectile(x,y,m,angleValue,6,"projectile",m->getAttack());

this->addItem(p);

pewMic->pause();

pewMic->play();

QGraphicsScene:

:

mousePressEvent(e);

}

 

子弹的自动移动与移除:

voidprojectile:

:

advance(inti){

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);

}

}

子项目自动运行函数:

voidbasePropertys:

:

advance(int){

isColliding();

}

场景中设置文字:

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

setScene(gameScene);

game_score=newQGraphicsTextItem(0,gameScene);

game_score->setHtml("分数:

"+QString:

:

number(score)+"");

game_score->setFont(QFont("Times",20,QFont:

:

Bold));

game_score->setPos(10,10);

game_attack=newQGraphicsTextItem(0,gameScene);

game_attack->setHtml("攻击力:

"+QString:

:

number(attackPower)+"");

game_attack->setFont(QFont("Times",20,QFont:

:

Bold));

game_attack->setPos(1

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 面试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1