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 
#include 
#include 

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 
#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-"<

资源文件啥的在上面的链接上下载

你可能感兴趣的:(C/C++,Qt,小项目集合)