日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Qt学习笔记-基于QGraphicsScene的打地鼠游戏

發布時間:2025/3/15 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt学习笔记-基于QGraphicsScene的打地鼠游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

運行截圖如下:



源碼工程下載地址:

https://download.csdn.net/download/qq78442761/10366473


這里有幾個關鍵點:

當繼承QGraphicsScene時,至少要重寫:

QRectF boundingRect() const;


void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);


具體代碼如下:

hole.h

#ifndef HOLE_H #define HOLE_H#include <QObject> #include <QtWidgets> #include <time.h>class Hole : public QGraphicsObject {Q_OBJECT public:Hole(int w,int h,const QString &emptyIcon,const QString &checked,QGraphicsItem *parent=0);QRectF boundingRect() const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);signals:void stateChanged(bool checked);protected:void mousePressEvent(QGraphicsSceneMouseEvent *event);void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);void timerEvent(QTimerEvent *event);private:int m_timerld;int m_width;int m_height;bool m_checked;QPixmap m_checkedIcon;QPixmap m_emptyIcon;bool m_leftButtonDown;bool isMouse; };#endif // HOLE_H


holel.cpp

#include "hole.h"Hole::Hole(int w, int h, const QString &emptyIcon, const QString &checked, QGraphicsItem *parent):QGraphicsObject(parent),m_width(w),m_height(h),m_checked(false),m_checkedIcon(checked),m_emptyIcon(emptyIcon),m_leftButtonDown(false),isMouse(false) {m_timerld=startTimer(1000);srand((unsigned int)time(NULL)); }QRectF Hole::boundingRect()const{return QRectF(0,0,m_width,m_height); }void Hole::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){Q_UNUSED(option)Q_UNUSED(widget)int x=0;int y=m_checkedIcon.height();if(m_checked){y-=25;x-=10;}if(isMouse){painter->drawPixmap(x,y,m_checked?m_checkedIcon:m_emptyIcon);if(m_checked){m_checked=!m_checked;}} }void Hole::mousePressEvent(QGraphicsSceneMouseEvent *event){if(event->button()==Qt::LeftButton){m_leftButtonDown=true;event->accept();} }void Hole::timerEvent(QTimerEvent *event){if(event->timerId()==m_timerld){int num=rand()%10+1;if(num>5){isMouse=true;}else{isMouse=false;}} }void Hole::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){if(event->button()==Qt::LeftButton){m_leftButtonDown=false;m_checked=!m_checked;event->accept();update();emit stateChanged(m_checked);} }


main.cpp

#include <QApplication> #include "hole.h"int main(int argc, char *argv[]) {QApplication a(argc, argv);QGraphicsScene scene(0,0,500,300);QGraphicsView view(&scene);view.setMinimumSize(500,300);view.setWindowTitle(QObject::tr("打地鼠"));view.setBackgroundBrush(QBrush(QImage(":/img/background.jpg").scaled(500,300)));view.setSceneRect(0,0,500,300);auto hole1=new Hole(128,200,":/img/mouse.png",":/img/boom.png");hole1->setPos(20,70);hole1->setFlag(QGraphicsItem::ItemIsFocusable,true);auto hole2=new Hole(128,200,":/img/mouse.png",":/img/boom.png");hole2->setPos(140,70);hole2->setFlag(QGraphicsItem::ItemIsFocusable,true);auto hole3=new Hole(128,200,":/img/mouse.png",":/img/boom.png");hole3->setPos(260,70);hole3->setFlag(QGraphicsItem::ItemIsFocusable,true);auto hole4=new Hole(128,200,":/img/mouse.png",":/img/boom.png");hole4->setPos(380,70);hole4->setFlag(QGraphicsItem::ItemIsFocusable,true);//QObject::connect(hole1,&Hole::stateChanged,[](bool checked){qDebug()<<"checked-"<<checked;});scene.addItem(hole1);scene.addItem(hole2);scene.addItem(hole3);scene.addItem(hole4);view.show();return a.exec(); }

資源文件啥的在上面的鏈接上下載

總結

以上是生活随笔為你收集整理的Qt学习笔记-基于QGraphicsScene的打地鼠游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。