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

581 篇文章 121 订阅
36 篇文章 23 订阅

运行截图如下:



源码工程下载地址:

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

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

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值