全球滚动:Qt学习之视图框架下的简单的碰撞检测
【资料图】
由于最近要写一个东西,所以跳过了一些内容,学到了视图框架和一些简单
的碰撞检测。
对于视图框架,我的理解还是不够深,等以后做项目深入了解后,我会回来更新。
我是写了一个自己的场景和两个图形项。先看main.cpp的代码
#include\"myscene.h\"#include #include#include\"playplany.h\"int main(int argc, char *argv[]){QApplication a(argc, argv);QGraphicsView view;//创myscene scene;scene.addRect(0,0,800,800);场景的范围坐标view.setScene(&scene);view.show();return a.exec();}
然后是我的图形项,本来准备用照片的,最后每找到,不过原理都是一样的
#ifndef ENEMYPLANY_H#define ENEMYPLANY_H#include#include#includeclass enemyplany:public QGraphicsObject{public:enemyplany();QRectFboundingRect() const;void paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr);void timerEvent(QTimerEvent *event);QPainterPath shape() const;int x;int y;};#endif // ENEMYPLANY_H#include \"enemyplany.h\"enemyplany::enemyplany(){startTimer(1000);x=500;y=100;}QRectF enemyplany::boundingRect() const//它们画的位置是基于场景的{return QRectF(0,0,800,800);}void enemyplany::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){painter->drawRect(x,y,40,50);update();}void enemyplany::timerEvent(QTimerEvent *event){x-=30;}QPainterPath enemyplany::shape() const{QPainterPath path;path.addRect(x,y,40,50);//这个函数就是对于进入这个范围的图形项就算碰撞return path;}
再看下一个图形项,其实与这个差不多
#ifndef PLAYPLANY_H#define PLAYPLANY_H#include#include#includeclass playplany:public QGraphicsObject{public:playplany();QRectFboundingRect() const;void paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr);void timerEvent(QTimerEvent *event);QPainterPath shape() const;int x=100;int y=100;};#endif // PLAYPLANY_H#include \"playplany.h\"#includeplayplany::playplany(){startTimer(1000);}QRectF playplany::boundingRect() const{return QRectF(0,0,800,800);}void playplany::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){painter->drawRect(x,y,40,50);update();}void playplany::timerEvent(QTimerEvent *event){qsrand((unsigned)time(NULL));x+=30;}QPainterPath playplany::shape() const{QPainterPath path;path.addRect(x,y,40,50);//qDebug()<
最后就是场景的设置了,看代码
#ifndef MYSCENE_H#define MYSCENE_H#include#include\"enemyplany.h\"#include\"playplany.h\"#include#includeclass myscene:public QGraphicsScene{public:myscene();QRectFboundingRect() const;private:enemyplany *enem_plany;playplany *play_plany;void timerEvent(QTimerEvent *e);};#endif // MYSCENE_H#include \"myscene.h\"myscene::myscene(){enem_plany=new enemyplany;play_plany=new playplany;this->addItem(enem_plany);this->addItem(play_plany);startTimer(50);}QRectF myscene::boundingRect() const{return QRectF(0,0,800,800);}void myscene::timerEvent(QTimerEvent *e)//每50秒判断一次是否碰撞{if(enem_plany->collidesWithItem(play_plany)){play_plany->x-=30;}}
【领 QT开发教程 学习资料, 点击下方链接莬费领取↓↓ ,先码住不迷路~】
点击这里:
上一篇:【古诗词鉴赏】清代诗人王泰偕《吴门竹枝词四首·其四·小满》诗赏析
下一篇:最后一页
-
粤水电(002060.SZ):巴楚能源公司拟123.03亿元投建粤水电巴楚县200万千瓦光储一体化项目格隆汇12月7日丨粤水电公布,为拓展清洁能源发电业务,扩大清洁能源发电业务规模,提高利润水平,公司拟由全资子公司新疆粤水电的全资子公司巴
-
浙江震元:公司互联网医院尚在建设中,未来将根据业务情况配置医生|今日关注(原标题:浙江震元:公司互联网医院尚在建设中,未来将根据业务情况配置医生)同花顺(300033)金融研究中心12月6日讯,有投资者向浙江震元(
-
妇炎洁广告被指侮辱女性上热搜 官方致歉来了!5月17日,妇炎洁广告被指侮辱女性登上热搜。据了解,近日,妇炎洁电商官方旗舰店一款女性私处用品广告被指侮辱女性,广告写道:中日韩三国
-
花8000多元买的商品,白等两个月却没收到货?亚马逊回应“花8000多元买的商品,白等了两个月却没收到货。”近日,消费者赵先生向《中国消费者报》投诉,称他通过亚马逊购物APP下了两笔订单,在等待两个月后订单均被强制取消。
-
广东省消委会发布二手车买卖合同示范文本为配合广东省推进“阳光二手车”工作,促进二手车流通行业健康发展,推动经营主体树立诚信经营理念,规范二手车交易行为,切实保护消费者的合法权益。
X 关闭
资讯
X 关闭
聚焦