QT 图形视图框架

学习QT已有一个月了,既写过媒体播放器也写过数据库方面的,突然萌发了写写游戏的想法

然后随便写了一个俄罗斯方块游戏,发现挺麻烦,然后无意中看到图形视图框架自带碰撞检测

所以,今天就来学习这框架,关于这框架的介绍以及使用方法网上有很多就不一一说了

首先建个项目。。。

GameView.h

 
 
#ifndef GAMEVIEW_H
#define GAMEVIEW_H
#include <QGraphicsView>
class GameView : public QGraphicsView
{
    Q_OBJECT
public:
    GameView(QGraphicsScene *Scene = 0);
    ~GameView();
};
#endif // GAMEVIEW_H


GameView.cpp

 
 

#include "gameview.h"
#include <QGraphicsPixmapItem>
GameView::GameView(QGraphicsScene *Scene):QGraphicsView(Scene)
{
}
GameView::~GameView()
{
}


main.cpp

#include "gameview.h"
#include <QTime>
#include <QApplication>
#include <QGraphicsScene>
int main(int argc, char *argv[])
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));//后期用
    QApplication a(argc, argv);
    QGraphicsScene *Scene = new QGraphicsScene;
    GameView w(Scene);
    w.show();
    return a.exec();
}

嗯嗯,挺好,现在运行一下~~呀?白色的底!神马都木有,,,,这很正常嘛~来添加个小Item

SingleAnimation.h

#ifndef SINGLEANIMATION_H
#define SINGLEANIMATION_H
#include <QtWidgets>  //测试中为了方便就用这个了
class SingleAnimation
        :public QObject
        ,public QGraphicsItem
{
    Q_OBJECT
public:
    SingleAnimation();
    SingleAnimation(const QList<QPixmap> &PixmapList, const int &PlaySpeed);
    SingleAnimation(const QString &FileName);
    QSize getSize()const{return QSize(m_PixmapList[0].width(),m_PixmapList[0].height());}
    void start();
    bool isEmpty();
    void setloop(const bool &loop);
    void setPixmapList(const QList<QPixmap> &PixmapList, const int &PlaySpeed);
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
signals:
    void finished();
private:
    bool m_loop;
    int m_Index;
    int m_PlaySpeed;
    QBasicTimer *m_Time;
    QList<QPixmap> m_PixmapList;
protected:
    void timerEvent(QTimerEvent *event);
};
#endif // SINGLEANIMATION_H


SingleAnimation.cpp

#include "singleanimation.h"
SingleAnimation::SingleAnimation()
    :m_Index(0)
{
}
SingleAnimation::SingleAnimation(const QList<QPixmap> &PixmapList, const int &PlaySpeed)
    :m_Index(0)
{
    setPixmapList(PixmapList,PlaySpeed);
}
SingleAnimation::SingleAnimation(const QString &FileName)
    :m_Index(0)
{
    QMovie *Movie = new QMovie(FileName);
    int &&Count = Movie->frameCount();
    for(int i=0;i<Count;i++)
    {
        Movie->jumpToFrame(i);
        m_PixmapList <<Movie->currentPixmap();
    }
    m_PlaySpeed = Movie->speed();
}
void SingleAnimation::start()
{
    m_Time = new QBasicTimer;
    m_Time->start(m_PlaySpeed,this);
}
bool SingleAnimation::isEmpty()
{
    return m_PixmapList.isEmpty();
}
void SingleAnimation::setloop(const bool &loop)
{
    m_loop = loop;
}
void SingleAnimation::setPixmapList(const QList<QPixmap> &PixmapList, const int &PlaySpeed)
{
    m_PixmapList = PixmapList;
    m_PlaySpeed = PlaySpeed;
}
QRectF SingleAnimation::boundingRect() const
{
    if(m_PixmapList.isEmpty())
        return QRectF();
    qreal penWidth = 1;
    return QRectF(0 - penWidth / 2, 0 - penWidth / 2,
                  m_PixmapList[0].width() + penWidth, m_PixmapList[0].height() + penWidth);
}
void SingleAnimation::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);
    Q_UNUSED(option);
    if(m_PixmapList.isEmpty())
        return;
    painter->drawPixmap(0,0
                        ,m_PixmapList[m_Index].width()
                        ,m_PixmapList[m_Index].height()
                        ,m_PixmapList[m_Index]);
    this->update();
}
void SingleAnimation::timerEvent(QTimerEvent *event)
{
    if(m_PixmapList.isEmpty())
        return;
    if(event->timerId() == m_Time->timerId())
    {
        if(m_Index == m_PixmapList.count()-1)
        {
            if(m_loop)
            {
                m_Index = ++m_Index % m_PixmapList.count();
            }
            else
            {
                m_Time->stop();
                emit finished();
                delete this;
                return;
            }
        }
        m_Index++;
    }
}


 
 

嗯。。。这类是播放一组图片用的

 
 

来看看效果~

在GameView构造加上这段

for(int i=0;i<5;i++)
{
     SingleAnimation *Anima = new SingleAnimation(u8"E:/素材/_826553c5d55dea50/img/blow.gif");
     Scene->addItem(Anima);
     Anima->setloop(true);
     Anima->start();
     Anima->setPos(i*Anima->getSize().width(),0);
}


QT 图形视图框架_第1张图片

(●'◡'●)。。。今天就这样啦

顺便这是一个游戏对象类的UML求协作者

你可能感兴趣的:(qt,俄罗斯方块,QGraphicsView,图形视图框架)