#include "playscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSound>
PlayScene::PlayScene(int index)
{
//返回按钮音效
QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);
//翻金币音效
QSound *flipSound = new QSound(":/res/ConFlipSound.wav",this);
//胜利按钮音效
QSound *winSound = new QSound(":/res/LevelWinSound.wav",this);
this->levelIndex = index;
qDebug() << "第" << this->levelIndex << "关";
//设置窗口固定大小
this->setFixedSize(320,588);
//设置图标
this->setWindowIcon(QIcon(":/res/Coin0001.png"));
//设置标题
this->setWindowTitle("翻金币");
//创建菜单栏
QMenuBar * bar = this->menuBar();
this->setMenuBar(bar);
//创建开始菜单
QMenu * startMenu = bar->addMenu("开始");
//创建按钮菜单项
QAction * quitAction = startMenu->addAction("退出");
//点击退出 退出游戏
connect(quitAction,&QAction::triggered,[=](){this->close();});
// 返回按钮
MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
backBtn->setParent(this);
backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
connect(backBtn,&MyPushButton::clicked,[=](){
// 播放音效
backSound->play();
QTimer::singleShot(500, this,[=](){
this->hide();
//触发自定义信号,关闭自身,该信号写到 signals下做声明
emit this->chooseSceneBack();
});
});
// 显示当前关卡号
QLabel * label = new QLabel;
label->setParent(this);
// 字体
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20);
label->setFont(font);
// 文字
QString str = QString("Leavel: %1").arg(this->levelIndex);
label->setText(str);
label->setGeometry(QRect(30,this->height() - 50, this->width(),50 )); label->setAttribute(Qt::WA_TransparentForMouseEvents);
// 避免关卡号把back给盖住了
label->setAttribute(Qt::WA_TransparentForMouseEvents,true);
// 胜利图片
QLabel * winLabel = new QLabel;
winLabel->setParent(this);
QPixmap pix;
pix.load(":/res/LevelCompletedDialogBg.png");
winLabel->setPixmap(pix);
winLabel->setGeometry(0,0,pix.width(),pix.height());
winLabel->move(this->width()*0.5-pix.width()*0.5, -pix.height());
//初始化二维数组
dataConfig config;
for(int i = 0 ; i < 4;i++)
{
for(int j = 0 ; j < 4; j++)
{
this->gameArray[i][j] = config.mData[this->levelIndex][i][j];
}
}
//创建金币的背景图片
// 用二阶矩阵是为了方便找之间的关系
for(int i = 0 ; i < 4;i++)
{
for(int j = 0 ; j < 4; j++)
{
//绘制背景图片
QLabel* label = new QLabel;
QPixmap pix;
pix.load(":/res/BoardNode.png");
label->setGeometry(0,0,pix.width(), pix.height());
label->setPixmap(pix);
label->setParent(this);
label->move(57 + i*pix.width(),200+j*pix.width());
// 创建金币
QString str;
if(this->gameArray[i][j] == 1)
{
str = ":/res/Coin0001.png";
}
else
{
str = ":/res/Coin0008.png";
}
MyCoin * coin = new MyCoin(str);
coin->setParent(this);
coin->move(59 + i*50,204+j*50);
coin->posX = i; //记录x坐标
coin->posY = j; //记录y坐标
coin->flag =gameArray[i][j]; //记录正反标志
coinBtn[i][j] = coin;
// 点击硬币,调用changeFlag
connect(coin,&MyCoin::clicked,[=](){
// 播放音效
flipSound->play();
// 禁用所有金币的点击
for(int i = 0; i< 4 ; i++)
{
for(int j = 0; j<4 ; j++)
{
this->coinBtn[i][j]->isWin = true;
}
}
coin->changeFlag();
// 记录当前状态,同步二维数组,利于后期存档等
gameArray[i][j] = gameArray[i][j] == 0 ? 1 : 0;
// 延时翻转周围金币
QTimer::singleShot(300, this,[=](){
// 检测右侧金币
if(coin->posX+1 <=3)
{
coinBtn[coin->posX+1][coin->posY]->changeFlag();
gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
}
// 检测左侧金币
if(coin->posX-1>=0)
{
coinBtn[coin->posX-1][coin->posY]->changeFlag();
gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]== 0 ? 1 : 0;
}
// 检测上侧金币
if(coin->posY+1<=3)
{
coinBtn[coin->posX][coin->posY+1]->changeFlag();
gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
}
// 检测下侧金币
if(coin->posY-1>=0)
{
coinBtn[coin->posX][coin->posY-1]->changeFlag();
gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
}
// 点完硬币再检测
//判断是否胜利
this->isWin = true; // 默认true
for(int i = 0 ; i < 4;i++)
{
for(int j = 0 ; j < 4; j++)
{
//qDebug() << coinBtn[i][j]->flag ;
if( coinBtn[i][j]->flag == false)
{
this->isWin = false;
break;
}
}
}
if(this->isWin)
{
qDebug() << "YOU WIN!";
// 播放音效
winSound->play();
for(int i = 0 ; i < 4;i++)
{
for(int j = 0 ; j < 4; j++)
{
//qDebug() << coinBtn[i][j]->flag ;
this->coinBtn[i][j]->isWin = true;
}
}
// 将胜利图片下移 显示
QPropertyAnimation * animation = new QPropertyAnimation(winLabel,"geometry");
animation->setDuration(1000);
animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),