QGraphic学习笔记
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
一个小的简单的游戏引出的心得体会
分解实例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(":/sound/click.mp3"));
}
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,":images/startBtn1.png",":images/startB tn2.png",this);
connect(sbtn,SIGNAL(clickSig()),this,SLOT(guanKaScene()));
cjScene->addItem(sbtn);
myButton*soundBtn=new
myButton(width/2,height/2,":images/soundBtn1.png",":images/soundBtn2. png",this);
cjScene->addItem(soundBtn);
myButton*quitBtn=new
myButton(width/2,height/2+200,":images/quitBtn1.png",":images/quitBtn 2.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(w idth,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()<<i;
if(i==4){
QString path=":images/ninjia/1.png";
pix.load(path);
return;
}
QString path=":images/ninjia/"+QString::number(i)+".png";
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()<<i;
QString path=imgPath+QString::number(i)+".png";
pix.load(path);
}
精灵的碰撞检测: