QT学习之视图框架下的简单的碰撞检测

本文介绍了一个基于Qt的视图框架实现,包括自定义图形项和碰撞检测的实现细节。通过创建自定义的图形项类,如敌方飞机和玩家飞机,实现了定时更新位置和碰撞检测的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于最近要写一个东西,所以跳过了一些内容,学到了视图框架和一些简单

的碰撞检测。

对于视图框架,我的理解还是不够深,等以后做项目深入了解后,我会回来更新。

我是写了一个自己的场景和两个图形项。先看main.cpp的代码

#include"myscene.h"
#include <QApplication>
#include<QGraphicsView>
#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<QGraphicsObject>
#include<QTimerEvent>
#include<QPainter>
class enemyplany:public QGraphicsObject
{
public:
    enemyplany();
    QRectF
     boundingRect() 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<QGraphicsObject>
#include<QTimer>
#include<QTimerEvent>
class playplany:public QGraphicsObject
{
public:
    playplany();
    QRectF
     boundingRect() 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"
#include<QPainter>
playplany::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()<<path<<endl;
    return path;
}

最后就是场景的设置了,看代码

#ifndef MYSCENE_H
#define MYSCENE_H
#include<QGraphicsScene>
#include"enemyplany.h"
#include"playplany.h"
#include<QTime>
#include<QTimerEvent>

class myscene:public QGraphicsScene
{
public:
    myscene();
    QRectF
    boundingRect() 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;
    }
}

最后可以运行一下程序看一看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值