拼图游戏"/>
QT 简单拼图游戏
一、设计目标
本次项目的主要内容是设计开发一个趣味拼图游戏,其功能是对加载的图片进行任意分割(如分割成3X3矩阵或其他类型矩阵)并随机加载到图片框矩阵中,用户使用鼠标拖动图片框中的图片进行拼图,系统能够自动判别拼图是否成功并进行提示。
二、界面设计
三、源代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QMouseEvent>
#include"selectimage.h"
#include<QTimer>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTQPixmap pixmap;QList<QPixmap> pixmaps;SelectImage *selet;QTimer *timer;static int M_height;static int M_weight;static int o_x;static int o_y;int n;int *buf;int child_w;int child_h;int tag;int step;int num;bool is;public:Widget(QWidget *parent = nullptr);~Widget();void paintEvent(QPaintEvent *event);void mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);private slots:void on_spinBox_valueChanged(int arg1);void divide();void random(int*, int);void check();void change();void Enable();void Disable();void on_pushButton_begin_clicked();void on_pushButton_selet_clicked();void on_pushButton_ori_clicked();void on_pushButton_new_clicked();void on_pushButton_challenge_clicked();void on_pushButton_end_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include"newwidget.h"
#include<QPainter>
#include<QPixmap>
#include<QPainter>
#include<QMessageBox>
#include<QFileDialog>
#include<QTimer>
#include<QDebug>
int Widget::M_height = 800;
int Widget::M_weight = 800;
int Widget::o_x = 50;
int Widget::o_y = 50;Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);pixmap.load(":/res/01.jpg");selet = new SelectImage;timer = new QTimer(this);selet->setWindowTitle("选择图片");n = 3;buf = new int[n*n];step =0;num = 0;is = false;ui->label_time->setText("0");ui->spinBox->setValue(3);ui->label_show->setText(QString::number(step));ui->pushButton_end->setEnabled(false);connect(selet,SIGNAL(is_ok()),this,SLOT(change()));connect(timer,&QTimer::timeout,[=](){num++;ui->label_time->setText(QString::number(num));if(num == ui->lineEdit->text().toInt()){timer->stop();QMessageBox::critical(this,"警告","时间结束!没有完成挑战","确定");ui->label_time->setText("0");step = 0;ui->label_show->setText(QString::number(step));Enable();}});
}
Widget::~Widget()
{delete ui;delete selet;
}
void Widget::mousePressEvent(QMouseEvent *event)
{if(event->button() == Qt::LeftButton){int x = event->x();int y = event->y();if(x<=850&&x>=50&&y<=850&&y>=50){tag = (x-50)/child_w + ((y-50)/child_h)*n;is = true;}}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{if(event->button() == Qt::LeftButton){int x = event->x();int y = event->y();if(x<=850&&x>=50&&y<=850&&y>=50&&is){int tag1 = (x-50)/child_w + ((y-50)/child_h)*n;std::swap(buf[tag],buf[tag1]);update();is = false;if(tag!=tag1){step++;ui->label_show->setText(QString::number(step));}}}check();
}
void Widget::paintEvent(QPaintEvent *) //绘图
{QPainter p(this);int i=0;for(int y=0;y<n;y++)for(int x=0;x<n;x++){p.drawPixmap(o_x+x*child_w, o_y+y*child_h, child_w, child_h, pixmaps[buf[i++]]);}
}
void Widget::divide() //分割图片
{QPixmap pix;pixmaps.clear();int h = pixmap.height()/n;int w = pixmap.width()/n;for(int y=0;y<n;y++)for(int x=0;x<n;x++){pix=pixmap.copy(x*w,y*h,w,h);pixmaps.append(pix);}
}
void Widget::random(int *buf, int len) //图片随机化
{srand(time(NULL));for(int i=0; i<len; i++)std::swap(buf[i], buf[rand()%len]);
}
void Widget::on_spinBox_valueChanged(int arg1) //获取当前切割边数
{n = arg1;child_w = M_weight/n;child_h = M_height/n;delete [] buf;buf = new int[n*n];for(int i=0;i<n*n;i++)buf[i] = i;random(buf,n*n);divide();update();step = 0;ui->label_show->setText(QString::number(step));
}
void Widget::check() //检查是否成功
{for(int i=0;i<n*n;i++){if(buf[i]!=i)return;}timer->stop();QMessageBox::information(this,"通知","恭喜!你成功了!","确定");ui->label_time->setText("0");Enable();
}
void Widget::on_pushButton_begin_clicked() //重新开始
{random(buf,n*n);divide();update();step = 0;ui->label_show->setText(QString::number(step));
}
void Widget::on_pushButton_selet_clicked() //选择图片
{selet->show();
}
void Widget::change()
{pixmap.load(selet->path);ui->spinBox->setValue(3);on_spinBox_valueChanged(3);
}
void Widget::on_pushButton_ori_clicked() //查看原图
{newWidget *wi = new newWidget;wi->setWindowTitle("原图");wi->pixmap = pixmap;wi->show();
}
void Widget::on_pushButton_new_clicked() //打开自己图片
{QString name = QFileDialog::getOpenFileName(this,tr("打开新图片"),"./",tr("(*.png *.jpg *.bmp)"));if(name.isEmpty())return;pixmap.load(name);ui->spinBox->setValue(3);on_spinBox_valueChanged(3);
}
void Widget::on_pushButton_challenge_clicked() //开始挑战
{if(ui->lineEdit->text().isEmpty()){QMessageBox::critical(this,"警告","尚未设置挑战时间","确定");return;}num = 0;timer->start(1000);on_pushButton_begin_clicked();Disable();
}
void Widget::Enable() //设置按钮可用
{ui->pushButton_new->setEnabled(true);ui->pushButton_ori->setEnabled(true);ui->pushButton_begin->setEnabled(true);ui->pushButton_selet->setEnabled(true);ui->pushButton_challenge->setEnabled(true);ui->spinBox->setEnabled(true);ui->lineEdit->setEnabled(true);ui->pushButton_end->setEnabled(false);
}
void Widget::Disable() //设置按钮不可用
{ui->pushButton_new->setEnabled(false);ui->pushButton_ori->setEnabled(false);ui->pushButton_begin->setEnabled(false);ui->pushButton_selet->setEnabled(false);ui->pushButton_challenge->setEnabled(false);ui->spinBox->setEnabled(false);ui->lineEdit->setEnabled(false);ui->pushButton_end->setEnabled(true);
}
void Widget::on_pushButton_end_clicked() //停止挑战
{Enable();timer->stop();ui->label_time->setText("0");
}
selectimage.cpp
#include "selectimage.h"
#include "ui_selectimage.h"
#include<QPixmap>
#include<QPainter>SelectImage::SelectImage(QWidget *parent) :QWidget(parent),ui(new Ui::SelectImage)
{ui->setupUi(this);sum_path<<":/res/01.jpg"<<":/res/02.jpg"<<":/res/03.jpg";path = sum_path[0];s=0;
}SelectImage::~SelectImage()
{delete ui;
}
void SelectImage::paintEvent(QPaintEvent *event)
{QPainter p(this);pixmap.load(path);p.drawPixmap(50,50,500,500,pixmap);
}
void SelectImage::on_pushButton_pre_clicked()
{if(s>0){s = s-1;path = sum_path[s];update();}
}
void SelectImage::on_pushButton_next_clicked()
{if(s<2){s = s+1;path = sum_path[s];update();}
}
void SelectImage::on_pushButton_selet_clicked()
{emit is_ok();
}
newwidget.cpp
#include "newwidget.h"
#include<QPainter>
#include<QPixmap>
#include<QDebug>
newWidget::newWidget(QWidget *parent) : QWidget(parent)
{setFixedSize(540,540);
}
void newWidget::paintEvent(QPaintEvent *event)
{QPainter p(this);p.drawPixmap(20,20,500,500,pixmap);
}
四、源代码下载
下载链接
更多推荐
QT 简单拼图游戏
发布评论