#ifndef PLAYSCENE_H
#define PLAYSCENE_H
#include <QMainWindow>
#include"mycoin.h"
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
class PlayScene : public QMainWindow
{
Q_OBJECT
public:
//explicit PlayScene(QWidget *parent = 0);
PlayScene(int index);
void paintEvent(QPaintEvent *);
int levalIndex; //关卡索引
int gameArray[4][4]; //二维数组数据
mycoin * coinBtn[4][4]; //金币按钮数组
signals:
void chooseSceneBack();
public slots:
};
#endif // PLAYSCENE_H
#include "playscene.h"
#include<QMenuBar>
#include<QMenu>
#include<QAction>
#include"mypushbutton.h"
#include<QTimer>
#include<Qpainter>
#include<QLabel>
#include"mycoin.h"
#include"dataconfig.h"
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{ }
PlayScene::PlayScene(int index)
{
levalIndex=index; //选择的关卡
this->setFixedSize(320,588);
this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
this->setWindowTitle("翻比特币");
//创建菜单栏
QMenuBar *bar=menuBar();
this->setMenuBar(bar);
//创建开始菜单
QMenu * startMenu = bar->addMenu("开始");
//创建菜单项
QAction * quition=startMenu->addAction("退出");
connect(quition,&QAction::triggered,[=](){this->close();});
//返回按钮
MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
closeBtn->setParent(this);
closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());
//返回按钮功能实现
connect(closeBtn,&MyPushButton::clicked,[=](){
QTimer::singleShot(500, this,[=](){
this->hide();
//触发自定义信号,关闭自身,该信号写到 signals下做声明
emit this->chooseSceneBack();
}
);
});
//显示选择的关卡
QLabel * label=new QLabel;
label->setParent(this);
QString str=QString("level %1").arg(this->levalIndex);
label->setText(str);
QFont font;
font.setFamily("宋体");
font.setPointSize(20);
label->setFont(font);
label->setGeometry(QRect(30, this->height() - 50,120, 50));
//初始化二维数组
dataConfig config;
for(int i = 0 ; i < 4;i++)
{
for(int j = 0 ; j < 4; j++)
{
gameArray[i][j] = config.mData[this->levalIndex][i][j];
}
}
QLabel* winLabel = new QLabel;
QPixmap winPix;
winPix.load(":/res/LevelCompletedDialogBg.png");
winLabel->setGeometry(0,0,winPix.width(),winPix.height());
winLabel->setPixmap(winPix);
winLabel->setParent(this);
winLabel->move( (this->width() - winPix.width())*0.5 , -winPix.height());
//创建金币矩阵
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*50,200+j*50);
//金币对象
QString str;
if(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;
connect(coin,&MyCoin::clicked,[=](){
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;
for(int i = 0 ; i < 4;i++)
{
for(int j = 0 ; j < 4; j++)
{
if( coinBtn[i][j]->flag == false)
{
this->isWin = false;
break;
}
}
}
if(this->isWin)
{
//禁用所有按钮点击事件
for(int i = 0 ; i < 4;i++)
{
for(int j = 0 ; j < 4; j++)
{
coinBtn[i][j]->isWin = true;
}
//创建动态胜利图向下跑出
QPropertyAnimation * animation1 = new QPropertyAnimation(winLabel,"geometry");
animation1->setDuration(1000);
animation1->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
animation1->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
animation1->setEasingCurve(QEasingCurve::OutBounce);
animation1->start();
}
});
}
}
}
void PlayScene::paintEvent(QPaintEvent *)
{
//背景设置
QPainter painter(this);
QPixmap pix;
pix.load(":/res/PlayLevelSceneBg.png");
painter.drawPixmap(0,0,this->width(),this->height(),pix);
//加载标题
pix.load(":/res/Title.png");
pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
painter.drawPixmap( 10,30,pix.width(),pix.height(),pix);
}