qt拼图游戏

来源:互联网 发布:淘宝达人电脑端入口 编辑:程序博客网 时间:2024/06/05 06:27

拼图效果图

#ifndef WIDGET_H
#define WIDGET_H
#include <QMainWindow>
#include <QMenuBar>
#include <QFrame>
class Frame;
enum Level{
    noLevel = 0,
    low = 1,
    mid = 2,
    high = 3
};
class Widget : public QMainWindow
{
    Q_OBJECT
public:
    Widget(QWidget *parent = 0);
    ~Widget();
    void initWidget();
public slots:
    void openFile();
    void lowGameLevel();
    void midGameLevel();
    void hightGameLevel();
private:
    QString path;
    Frame *frame;
};
class Frame : public QFrame
{
    Q_OBJECT
public:
    Frame(QWidget *parent = 0);
    ~Frame();
    //加载图片
    void loadImage(QString fileName);
    //设置游戏水平
    void setLevel(Level level);
    //分割图片
    void pixSplit(int row, int col);
    void paintEvent(QPaintEvent *event);  
    void dragEnterEvent(QDragEnterEvent *event);   
    void dropEvent(QDropEvent *event);
    void mousePressEvent(QMouseEvent *event);
    int targetIndex(const QPoint &pos);
private:
    QList<QPixmap> pixList;
    QList<QRect> rects;
    Level l;
    QPixmap pix;
    int pixW, pixH;
    int row, col;
    QPixmap src;
    int srcIndex;
};
#endif // WIDGET_H


#include "widget.h"
#include <QVBoxLayout>
#include <QFileDialog>
#include <QPainter>
#include "rectpos.h"
#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QDragLeaveEvent>
#include <QDrag>
#include <QMimeData>
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QMainWindow(parent)
{
    setAcceptDrops(true);
    initWidget();
    resize(800,600);
}
Widget::~Widget()
{
}
void Widget::initWidget()
{
    QMenu *fileMenu = new QMenu("文件管理");
    QMenu *gameLevel = new QMenu("游戏水平");
    menuBar()->addMenu(fileMenu);
    menuBar()->addMenu(gameLevel);
    QAction *lowAction = new QAction("低级水平");
    connect(lowAction,SIGNAL(triggered(bool)),this,SLOT(lowGameLevel()));
    QAction *midAction = new QAction("中级水平");
    connect(midAction,SIGNAL(triggered(bool)),this,SLOT(midGameLevel()));
    QAction *highAction = new QAction("高级水平");
    connect(highAction,SIGNAL(triggered(bool)),this,SLOT(hightGameLevel()));
    gameLevel->addAction(lowAction);
    gameLevel->addAction(midAction);
    gameLevel->addAction(highAction);
    QAction *openAction = new QAction("加载图片");
    connect(openAction,SIGNAL(triggered(bool)),this,SLOT(openFile()));
    fileMenu->addAction(openAction);
    frame = new Frame(this);
    setCentralWidget(frame);
}
void Widget::openFile()
{
    path = QFileDialog::getOpenFileName(this, tr("Open File"),"F:/",tr("Images (*.png *.xpm *.jpg)"));
    if(path.isEmpty())
    {
        return;
    }
    frame->loadImage(path);
}
void Widget::lowGameLevel()
{
    frame->setLevel(Level::low);
}
void Widget::midGameLevel()
{
    frame->setLevel(Level::mid);
}
void Widget::hightGameLevel()
{
    frame->setLevel(Level::high);
}
Frame::Frame(QWidget *parent):QFrame(parent)
{
    setAcceptDrops(true);
    pixH = 0;
    pixW = 0;
}
Frame::~Frame()
{
}
void Frame::setLevel(Level level)
{
    l = level;
    rects.clear();
    pixList.clear();
    switch (level) {
    case 0:
        break;
    case 1:
        pixSplit(3,3);
        break;
    case 2:
        pixSplit(4,4);
        break;
    case 3:
        pixSplit(5,5);
        break;
    default:
        break;
    }
    update();
}
void Frame::pixSplit(int row, int col)
{
    this->row = row;
    this->col = col;
    pixW = pix.size().width()/col;
    pixH = pix.size().height()/row;
    int w = this->width()/col;
    int h = this->height()/row;
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        {
            QRect rect = QRect(j*w,i*h,w,h);
            rects<<rect;
            QPixmap p = pix.copy(j*pixW,i*pixH,pixW,pixH);
            pixList<<p;
        }
    }
//    pixList.removeLast();
}
void Frame::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    if(0==pixH || 0==pixW)
    {
        painter.drawPixmap(0,0,this->width(),this->height(),pix);
    }else
    {
        for(int i=0; i<pixList.count(); i++)
        {
            painter.drawPixmap(rects[i],pixList[i]);
        }
    }
}
void Frame::loadImage(QString fileName)
{
    bool success = pix.load(fileName);
    if(!success)
    {
        qDebug()<<"Image load failure.";
    }
    setLevel(l);
    update();
}
void Frame::dragEnterEvent(QDragEnterEvent *event)
{
    event->accept();
}
void Frame::dropEvent(QDropEvent *event)
{
    int index = targetIndex(event->pos());
    QPixmap target = pixList[index];
    pixList.removeAt(index);
    pixList.insert(index,src);
    pixList.removeAt(srcIndex);
    pixList.insert(srcIndex,target);
    update();
}
void Frame::mousePressEvent(QMouseEvent *event)
{
    if(!pixList.count())
    {
        return;
    }
    srcIndex = targetIndex(event->pos());
    QMimeData *data = new QMimeData;
    src = pixList[targetIndex(event->pos())];
    QDrag *drag = new QDrag(this);
    drag->setMimeData(data);
    drag->setPixmap(src);
    drag->start();
}
int Frame::targetIndex(const QPoint &pos)
{
    int w = this->width()/col;
    int h = this->height()/row;
    int index = pos.y()/h*col+pos.x()/w;
    return index;
}

可运行源码文件链接如下:

http://download.csdn.net/download/qq_33200959/9927179

原创粉丝点击