用Qt做的拼图小游戏

  思路:在主界面可以选择图片进行对应的拼图,把一张600*600的图片分割成3*3张200*200的九宫格图片,对每张图片进行编号,对应0~8,然后打乱顺序,用的是随机函数,然后添加鼠标过滤事件:鼠标左击两张不同的图片可以位置互换,互换的原理是编号互换,然后根据互换编号对应的图片互换。然后判断是否胜利:还是用编号,判断编号是否依次是0~8,下面是代码呈现。 

 

 1、主界面:

MainScene::MainScene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainScene)
{
    ui->setupUi(this);
    //设置主界面大小
    this->setFixedSize(490,490);

    //设置标题图标
    setWindowIcon(QIcon(":/res/icon"));

    //设置窗口标题
    setWindowTitle("BeautyGirl");

    //退出按钮实现
    connect(ui->exit,&QAction::triggered,[=](){
        this->close();
    });

    //帮助按钮实现
    connect(ui->help,&QAction::triggered,[=](){
        GameHelp *gameHelp =new GameHelp;
        gameHelp->show();
    });

    //背景音乐
    QSound *bgSound =new QSound(":/res/bgSound.wav",this);
    bgSound->setLoops(-1);
    bgSound->play();
    QSound *staSound = new QSound(":/res/TapButtonSound.wav",this);



    //加入mm1号
    loadMm(":/res/mm1.jpg",30,30);

    //加入mm2号

    loadMm(":/res/mm2.jpg",260,30);
    //加入mm3号
    loadMm(":/res/mm3.jpg",30,260);
    //加入mm4号
    loadMm(":/res/mm4.jpg",260,260);



}
void MainScene::loadMm(QString url,int x,int y)
{
    MyPushButton *mm = new MyPushButton(url);
    mm->setParent(this);
    mm->move(x,y);
    connect(mm,&MyPushButton::clicked,[=](){
        mm->jump();
        QTimer::singleShot(500,this,[=](){
            //还进入当前位置
            //chooseScene->setGeometry(this->geometry());
            //bgSound->stop();
            //staSound->play();
            this->hide();
            playGame=new PlayGame(url);
            playGame->show();
            connect(playGame,&PlayGame::backSignal,[=](){
                qDebug()<<"点击了返回!";
                QTimer::singleShot(500,this,[=](){
                    playGame->hide();
                    this->show();
                    delete playGame;
                    playGame=NULL;

                });
            });
        });

    });

}

2、分割图片

void PlayGame::devide()
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < ROW; j++)
        {
            m_pix[i][j]=m_mm.copy(j*200,i*200,200,200);
        }
    }
}

3、打乱图片顺序

void PlayGame::randMap()
{
    int a[NUMBER] = {0};  //去重作用
    int t;
    srand((unsigned char)time(NULL));
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < ROW; j++)
        {
            t = rand() % NUMBER;   //[0,8]
            while (a[t])
            {
                t = rand() % NUMBER;
            }
            m_map[i][j] = t;
            a[t]++;
        }
    }
}

4、鼠标事件

bool PlayGame::eventFilter(QObject *watched, QEvent *event)
{
    int t,f=1;
    for(int i=0;i<ROW;i++)
    {
        for(int j=0;j<ROW;j++)
        {
            if(flag==1&&watched==this->m_lab[i][j])
            {
                if(event->type()==QEvent::MouseButtonPress)
                {
                    QMouseEvent *mouseevent = static_cast<QMouseEvent *>(event);
                    if(mouseevent->buttons()==Qt::LeftButton)
                    {                        
                        position[this->m_count].x=i;
                        position[this->m_count].y=j;
                        this->m_count++;
                        if(this->m_count==2)
                        {
                            if(position[0].x==position[1].x&&position[0].y==position[1].y)
                            {
                                QMessageBox::information(this,"咳咳","你怎么一直点人家一个部位!");
                                this->m_count--;
                            }
                            else
                            {

                                this->m_stemNumber++;
                                ui->stepNumber->setText(QString::number(this->m_stemNumber)+"步");
                                t = this->m_map[position[0].x][position[0].y];
                                this->m_map[position[0].x][position[0].y]=
                                        this->m_map[position[1].x][position[1].y];
                                this->m_map[position[1].x][position[1].y]=t;
                                this->m_lab[position[0].x][position[0].y]->setPixmap
                                        (this->m_pix[this->m_map[position[0].x][position[0].y]/ROW]
                                        [this->m_map[position[0].x][position[0].y]%ROW]);
                                this->m_lab[position[1].x][position[1].y]->setPixmap
                                        (this->m_pix[this->m_map[position[1].x][position[1].y]/ROW]
                                        [this->m_map[position[1].x][position[1].y]%ROW]);
                                this->m_count=0;

                                for(int i=0;i<ROW;i++)
                                {
                                    for(int j=0;j<ROW;j++)
                                    {
                                        if(this->m_map[i][j]!=i*3+j)
                                        {
                                            f=0;
                                            break;
                                        }
                                        if(f==0)
                                        {
                                            break;
                                        }
                                    }
                                }
                                if(f==1)
                                {
                                    qDebug()<<"胜利!";
                                    emit this->victory();
                                }


                            }
                        }
                    }

                }
                return QWidget::eventFilter(watched, event);
            }

        }
    }
}

5、胜利

发送胜利信号:这个代码也在鼠标事件里面,每有两张图片交换就判断一次是否胜利

for(int i=0;i<ROW;i++)
{
    for(int j=0;j<ROW;j++)
    {
        if(this->m_map[i][j]!=i*3+j)
        {
            f=0;
            break;
        }
        if(f==0)
        {
            break;
        }
    }
}
if(f==1)
{
    qDebug()<<"胜利!";
    emit this->victory();
}

接收胜利信号:

connect(this,&PlayGame::victory,[=](){
        timer->stop();
        flag=0;
        qDebug()<<"vectory";
        int choice=QMessageBox::question(this,"Victory!!","是不是超美,\n是继续欣赏还是返回主页挑选其他MM","继续欣赏","返回主页挑选其他MM");
        if(choice==0);
        else
        {
            emit this->backSignal();
        }


    });

资源打包:用Qt做的拼图小游戏BeautyGirl_qt拼图游戏九宫格-C++文档类资源-CSDN下载

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

small_planet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值