基于QT实现的简易3*3拼图游戏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SilverFOX111/article/details/78747301
  • 先建立游戏界面,将图片分割分别添加到相应的Label中,之后通过随机函数将Label顺序打乱,同时将游戏信息初始化。编写一个鼠标按压事件,当鼠标点击后,判断鼠标是否点击了空白格子旁边可移动的Label,若是,移动后,检测是否Label顺序正确,基本的逻辑就这样。
//playwindow.h
#ifndef PLAYWINDOW_H
#define PLAYWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QImage>

namespace Ui {
class PlayWindow;
}

class PlayWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit PlayWindow(QWidget *parent = 0);
    ~PlayWindow();
    void mousePressEvent(QMouseEvent *event); //鼠标点击事件

private:
    Ui::PlayWindow *ui;

    QLabel *picLabel[9];    //定义盛放被分割的9个图片块的Label
    QLabel *numLabel;       //显示步数的Label
    QLabel *referenceLabel; //显示全图的Label

    int picFlag[3][3];      //定义二维数组用来标志每个位置放的第几个图
    QImage labelImg[3][3];  //定义9个被分割的9个图片
    void createPic();       //创建界面
    void movePic();         //移动图片
    void RandomPic();       //打乱图片
    void checkOk();         //检测图片是否拼成功
    int stepNum;            //步数变量

    QPushButton *exitBt;    //退出按钮

private slots:
    void exit();            //退出槽函数

};

#endif // PLAYWINDOW_H
//playwindow.c
#include "playwindow.h"
#include "ui_playwindow.h"
#include <QImage>
#include <QPixmap>
#include <QTime>
#include <QPoint>
#include <QMouseEvent>


#define PICTURESIZE 100         //分割后单个图片边长
#define BASEX 2                 //图片X间隔
#define BASEY 2                 //图片Y间隔

PlayWindow::PlayWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::PlayWindow)
{
    ui->setupUi(this);

    //设置窗体外观
    this->setMinimumSize(308,410);
    this->setMaximumSize(308,410);
    this->setStyleSheet(tr("background-image: url(:/new/prefix1/background.jpg);"));

    //步数初始化
    stepNum = 0;

    //实例参考图片的Label
    referenceLabel = new QLabel(this);
    referenceLabel->setGeometry(129,333,50,50);
    referenceLabel->setFrameShape(QFrame::Box);

    //实例显示步数的Label
    numLabel = new QLabel(this);
    numLabel->setGeometry(20,344,70,25);
    numLabel->setText(QString::number(stepNum));
    numLabel->setFrameShape(QFrame::Box);

    //实例退出按钮
    exitBt = new QPushButton(this);
    exitBt->setGeometry(QRect(230,344,50,25));
    exitBt->setText("退出");
    connect(exitBt,SIGNAL(clicked()),this,SLOT(exit()));

    //初始化界面
    createPic();

}

void PlayWindow::createPic(){

    QImage sourceImg;
    //加载操作图片
    sourceImg.load(":/new/prefix1/sourcePic .jpg");

    sourceImg = sourceImg.scaled(PICTURESIZE*3,PICTURESIZE*3);

    for( int i=0 ; i<3 ; i++){
        for( int j=0 ; j<3 ; j++ ){

            picLabel[i*3+j] = new QLabel(this);
            picLabel[i*3+j]->setGeometry(0,0,PICTURESIZE,PICTURESIZE);
            picLabel[i*3+j]->move((BASEX+PICTURESIZE)*j+BASEX,(BASEY+PICTURESIZE)*i+BASEY);
            picLabel[i*3+j]->setFrameShape(QFrame::Box);
            picFlag[i][j] = i*3+j;
            labelImg[i][j] = sourceImg.copy(PICTURESIZE*j,PICTURESIZE*i,PICTURESIZE,PICTURESIZE);
            picLabel[i*3+j]->setPixmap(QPixmap::fromImage(labelImg[i][j]));

        }
    }

    sourceImg = sourceImg.scaled(50,50);
    referenceLabel->setPixmap(QPixmap::fromImage(sourceImg));
    //加载移动图片
    sourceImg.load(":/new/prefix1/blank.jpg");
    picLabel[8]->setPixmap(QPixmap::fromImage(sourceImg));

    RandomPic();
}

void PlayWindow::movePic(){

    for( int i=0 ; i<3 ; i++){
        for( int j=0 ; j<3 ; j++ ){

            int index = picFlag[i][j];
            picLabel[index]->move((BASEX+PICTURESIZE)*j+BASEX,(BASEY+PICTURESIZE)*i+BASEY);

        }
    }

}

void PlayWindow::RandomPic(){

    int x = 2;
    int y = 2;
    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>0 ){
                    picFlag[x][y] = picFlag[x-1][y];
                    picFlag[x-1][y] = 8;
                    x--;
                }break;
            case 1://下
                if( x<2 ){
                    picFlag[x][y] = picFlag[x+1][y];
                    picFlag[x+1][y] = 8;
                    x++;
                }break;
            case 2://左
                if( y>0 ){
                    picFlag[x][y] = picFlag[x][y-1];
                    picFlag[x][y-1] = 8;
                    y--;
                }break;
            case 3://右
                if( y<2 ){
                    picFlag[x][y] = picFlag[x][y+1];
                    picFlag[x][y+1] = 8;
                    y++;
                }break;
            default:
                break;

        }

    }

    movePic();

}

void PlayWindow::mousePressEvent(QMouseEvent *event){

    int x = -1;
    int y = -1;

    if( event->button() == Qt::LeftButton ||        \
        event->button() == Qt::RightButton   ){

        QPoint mousePoint = event->pos();
        if( mousePoint.x() > BASEX && mousePoint.x() < (BASEX+PICTURESIZE)*3 &&
            mousePoint.y() > BASEY && mousePoint.y() < (BASEY+PICTURESIZE)*3    ){

            for( int i=0 ; i<3 ; i++){
                for( int j=0 ; j<3 ; j++ ){
                    if( picFlag[i][j] == 8 ){
                        x = i;
                        y = j;
                        break;
                    }
                }
            }
            int xPos = x*(BASEX+PICTURESIZE)+BASEX;
            int yPos = y*(BASEY+PICTURESIZE)+BASEY;
            if( mousePoint.x() < (yPos-BASEY) &&
                mousePoint.x() > (yPos-BASEY-PICTURESIZE) &&
                mousePoint.y() > (xPos) &&
                mousePoint.y() < (xPos+PICTURESIZE)){//左

                picFlag[x][y] = picFlag[x][y-1];
                picFlag[x][y-1] = 8;
                stepNum++;
                movePic();

            }else if( mousePoint.x() < (yPos+BASEY+2*PICTURESIZE) &&
                      mousePoint.x() > (yPos+BASEY+PICTURESIZE) &&
                      mousePoint.y() > (xPos) &&
                      mousePoint.y() < (xPos+PICTURESIZE)){//右

                        picFlag[x][y] = picFlag[x][y+1];
                        picFlag[x][y+1] = 8;
                        stepNum++;
                        movePic();

            }else if( mousePoint.y() < (xPos-BASEX) &&
                      mousePoint.y() > (xPos-BASEX-PICTURESIZE) &&
                      mousePoint.x() > (yPos) &&
                      mousePoint.x() < (yPos+PICTURESIZE)){//上

                        picFlag[x][y] = picFlag[x-1][y];
                        picFlag[x-1][y] = 8;
                        stepNum++;
                        movePic();

            }else if( mousePoint.y() < (xPos+BASEX+2*PICTURESIZE) &&
                      mousePoint.y() > (xPos+BASEX+PICTURESIZE) &&
                      mousePoint.x() > (yPos) &&
                      mousePoint.x() < (yPos+PICTURESIZE)){//下

                        picFlag[x][y] = picFlag[x+1][y];
                        picFlag[x+1][y] = 8;
                        stepNum++;
                        movePic();

            }

        }
        numLabel->setText(QString::number(stepNum));
        checkOk();

    }

}

void PlayWindow::checkOk(){

    for( int i=0 ; i<3 ; i++){
        for( int j=0 ; j<3 ; j++ ){
            if( picFlag[i][j] != i*3+j ){
                return;
            }
        }
    }
    referenceLabel->setText("OK");

}

void PlayWindow::exit(){

    this->close();

}

PlayWindow::~PlayWindow()
{
    delete ui;
}
//main.c
#include "playwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    PlayWindow w;
    w.show();

    return a.exec();
}

参考博客链接如下:
http://blog.csdn.net/qq_34886018/article/details/72866807

猜你喜欢

转载自blog.csdn.net/SilverFOX111/article/details/78747301