思路:在主界面可以选择图片进行对应的拼图,把一张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();
}
});