main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setFixedSize(760,500);
w.setWindowFlags(Qt::WindowCloseButtonHint);
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFileDialog>
#include <QPushButton>
#include <QMouseEvent>
#include <QMessageBox>
#include <QtGlobal>
#include <QTimer>
#include <QTime>
#define SMALL_W 130
#define SMALL_H 100
#define PHOTO_X 20
#define PHOTO_Y 110
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),
SLOT(onTimerOut()));
ui->label_3->setText((QString::number(bushusum)));
ui->time_label->setText((QString::number(tim)));
this->setStyleSheet(
"MainWindow{border-image: url(:/beijing.jpg);}");
pSourceImage=NULL;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
pLbImage[i*3+j] = new QLabel(this);
pLbImage[i*3+j]->setGeometry(0,0,SMALL_W,SMALL_H);
pLbImage[i*3+j]->move(PHOTO_X+SMALL_W*i,
PHOTO_Y+SMALL_H*j);
pLbImage[i*3+j]->setFrameShape(QFrame::Box);
}
}
connect(ui->btn,SIGNAL(clicked()),this,SLOT(test()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btn_clicked()
{
strFileName =
QFileDialog::getOpenFileName(this,
"select pictures",
"/home/newcapec",
"Images(*.png *.jpg)");
if(strFileName==NULL) return ;
if(NULL!=pSourceImage)
{
delete pSourceImage;
pSourceImage = NULL;
}
pSourceImage =new QImage(strFileName);
if(pSourceImage == NULL) return ;
QImage tep=pSourceImage ->scaled(ui->label->width(),
ui->label->height());
ui->label->setPixmap(QPixmap::fromImage(tep));
cutimage();
}
void MainWindow::onTimerOut()
{
tim++;
ui->time_label->setText((QString::number(tim)));
}
void MainWindow::cutimage()
{
QImage temp = pSourceImage->scaled(SMALL_W*3,
SMALL_H*3);
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
pImage[i][j]=temp.copy(i*SMALL_W,
j*SMALL_H,
SMALL_W,
SMALL_H);
pLbImage[i*3+j]->setPixmap(QPixmap::fromImage(pImage[i][j]));
pCompare[i][j]=i*3+j;
}
}
QPixmap tep(":/blank.jpg");
pLbImage[8]->setPixmap(tep);
Random();
}
void MainWindow::Random()
{
bushusum=0;
ui->label_3->setText((QString::number(bushusum)));
tim=0;
ui->time_label->setText((QString::number(tim)));;
int x=-1;
int y=-1;
for(int w=0;w<3;w++){
for(int j=0;j<3;j++){
if(pCompare[w][j]==8){
x=w;
y=j;
break;
}
}
if(x!=-1)
break;
}
if(x==-1||y==-1)
return ;
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
for(int i=0;i<1000;i++){
int direction = qrand()%4;
switch (direction) {
case 0:
if(x<2){
pCompare[x][y]=pCompare[x+1][y];
pCompare[x+1][y]=8;
x++;
}
break;
case 1:
if(x>0){
pCompare[x][y]=pCompare[x-1][y];
pCompare[x-1][y]=8;
x--;
}
break;
case 2:
if(y<2){
pCompare[x][y]=pCompare[x][y+1];
pCompare[x][y+1]=8;
y++;
}
break;
case 3:
if(y>0){
pCompare[x][y]=pCompare[x][y-1];
pCompare[x][y-1]=8;
y--;
}
break;
default:
break;
}
}
moveImage();
if(timer->isActive())
{
timer->stop();
}
timer->start(1000);
}
void MainWindow::moveImage()
{
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int index = pCompare[i][j];
pLbImage[index]->move(PHOTO_X+i*SMALL_W,
PHOTO_Y+j*SMALL_H);
}
}
}
int MainWindow::panduanwancheng ()
{
int y=1;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(pCompare[i][j]!=i*3+j)
{
y=0;
break;
}
}
if(!y) break;
}
return y;
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
int num=0;
if(pSourceImage==NULL) return ;
if(panduanwancheng()) return ;
if(event->button() == Qt::LeftButton
||event->button() == Qt::RightButton)
{
QPoint pressPoint = event->pos();
if(pressPoint.x()>PHOTO_X&&pressPoint.y()>PHOTO_Y
&&pressPoint.x()<PHOTO_X+SMALL_W*3&&pressPoint.y()<PHOTO_Y+SMALL_H*3)
{
int x = (pressPoint.x()-PHOTO_X)/SMALL_W;
int y = (pressPoint.y()-PHOTO_Y)/SMALL_H;
if(x>0&&pCompare[x-1][y]==8)
{
pCompare[x-1][y]=pCompare[x][y];
pCompare[x][y]=8;
num++;
}else if(x<2&&pCompare[x+1][y]==8)
{
pCompare[x+1][y]=pCompare[x][y];
pCompare[x][y]=8;
num++;
}else if(y>0&&pCompare[x][y-1]==8)
{
pCompare[x][y-1]=pCompare[x][y];
pCompare[x][y]=8;
num++;
}else if(y<2 && pCompare[x][y+1] == 8)
{
pCompare[x][y+1] = pCompare[x][y];
pCompare[x][y] = 8;
num++;
}
}
}
bushusum+=num;
ui->label_3->setText((QString::number(bushusum)));
moveImage();
if(panduanwancheng()){
timer->stop();
huifu();
}
}
void MainWindow::huifu ()
{
pLbImage[8]->setPixmap(QPixmap::fromImage(pImage[2][2]));
QMessageBox::about(this,"successful","***************************************");
}
void MainWindow::on_btn2_clicked()
{
if(pSourceImage==NULL) return ;
QPixmap tep(":/blank.jpg");
pLbImage[8]->setPixmap(tep);
Random();
}
void MainWindow::on_btn2_2_clicked()
{
if (!(QMessageBox::information(this,tr("CT Control View"),tr("Do you really want to quit the game?"),tr("Yes"),tr("No"))))
{
this->close();
}
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QLabel>
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void cutimage();
void Random();
void moveImage();
void mousePressEvent(QMouseEvent *event);
int panduanwancheng();
void huifu();
int bushusum;
QTimer *timer;
int tim;
private slots: //在槽函数中 只能包含函数
void on_btn_clicked();
void on_btn2_clicked();
void on_btn2_2_clicked();
void onTimerOut();
private:
Ui::MainWindow *ui;
QString strFileName;
QImage* pSourceImage;
QLabel* pLbImage[9];
QImage pImage[3][3];
int pCompare[3][3];
};
#endif // MAINWINDOW_H