目录
3.2 界面文件 mainscene.ui 和 res.qrc 资源添加
3.5 关卡数据类 dataconfig.h 和 dataconfig.cpp
3.6 首页主界面 mainscene.h 和 mainscene.cpp
3.7 选择关卡界面 chooselevelscene.h 和 chooselevelscene.h
3.8 翻金币界面 playscene.h 和 playscene.cpp
1. 项目简介
翻金币项目是一款经典的益智类游戏,我们需要将金币都翻成同色,才视为胜利。首先,开始界面如下:
点击start按钮,进入下层界面,选择关卡:
在这里我们设立了20个关卡供玩家选择,假设我们点击了第1关,界面如下:
如果想要赢取胜利,我们需要点击上图中红色方框选取的区域,翻动其上下左右的金币,然后当所有金币都变为金色,视为胜利,胜利界面如下:
2、程序框架
3、程序代码
3.1 项目工程配置文件 CoinFlip.pro
//CoinFlip.pro
#-------------------------------------------------
#
# Project created by QtCreator 2020-11-05T15:09:39
#
#-------------------------------------------------
QT += core gui multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CoinFlip
TEMPLATE = app
SOURCES += main.cpp\
mainscene.cpp \
mypushbutton.cpp \
chooselevelscene.cpp \
playscene.cpp \
mycoin.cpp \
dataconfig.cpp
HEADERS += mainscene.h \
mypushbutton.h \
chooselevelscene.h \
playscene.h \
mycoin.h \
dataconfig.h
FORMS += mainscene.ui
RESOURCES += \
res.qrc
CONFIG += c++11
3.2 界面文件 mainscene.ui 和 res.qrc 资源添加
mainscene.ui
res.qrc 资源添加
3.3 自定义按钮类 mypushbutton.h 和 mypushbutton.cpp
//mypushbutton.h
#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H
#include <QPushButton>
#include <QString>
#include <QDebug>
#include <QPropertyAnimation>
class MyPushButton : public QPushButton
{
Q_OBJECT
public:
// explicit MyPushButton(QWidget *parent = 0);
//重载有参构造函数
MyPushButton(QString normalImg, QString pressImg="");
//向下跳跃
void zoomDown();
//向上跳跃
void zoomUp();
//重载鼠标事件 按下事件
void mousePressEvent(QMouseEvent *e);
//重载鼠标事件 离开事件
void mouseReleaseEvent(QMouseEvent *e);
//默认显示图片路径
QString normalImgPath;
//按下后显示图片路径
QString pressImgPath;
signals:
public slots:
};
#endif // MYPUSHBUTTON_H
//mypushbutton.cpp
#include "mypushbutton.h"
//MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
//{
//}
//重载有参构造函数
MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
normalImgPath = normalImg;
pressImgPath = pressImg;
//加载图片
QPixmap pixmap;
bool ret = pixmap.load(normalImgPath);
if(!ret)
{
qDebug() << normalImgPath <<"加载图片失败!";
}
//设置图片的固定大小
this->setFixedSize(pixmap.width(),pixmap.height());
//设置不规则图片的样式表
this->setStyleSheet("QPushButton{border:0px;}");
//设置图片
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
}
//向下跳跃
void MyPushButton::zoomDown()
{
QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");
animation->setDuration(200);
animation->setStartValue(QVariant(QRect(this->x(),this->y(),this->width(),this->height())));
animation->setEndValue(QVariant(QRect(this->x(),this->y()+10,this->width(),this->height())));
animation->setEasingCurve(QEasingCurve::OutBounce);
animation->start();
}
//向上跳跃
void MyPushButton::zoomUp()
{
QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");
animation->setDuration(200);
animation->setStartValue(QVariant(QRect(this->x(),this->y()+10,this->width(),this->height())));
animation->setEndValue(QVariant(QRect(this->x(),this->y(),this->width(),this->height())));
animation->setEasingCurve(QEasingCurve::OutBounce);
animation->start();
}
//重载鼠标事件 按下事件
void MyPushButton::mousePressEvent(QMouseEvent *e)
{
//选中路径不为空,显示选中图片
if(this->pressImgPath != "")
{
QPixmap pixmap;
bool ret = pixmap.load(pressImgPath);
if(!ret)
{
qDebug() << pressImgPath << "加载图片失败!";
}
this->setFixedSize(pixmap.width(),pixmap.height());
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
}
return QPushButton::mousePressEvent(e);
}
//重载鼠标事件 离开事件
void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
//选中路径不为空,显示选中图片
if(this->normalImgPath != "")
{
QPixmap pixmap;
bool ret = pixmap.load(normalImgPath);
if(!ret)
{
qDebug() << normalImgPath << "加载图片失败!";
}
this->setFixedSize(pixmap.width(),pixmap.height());
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
}
return QPushButton::mouseReleaseEvent(e);
}
3.4 金币类 mycoin.h 和 mycoin.cpp
//mycoin.h
#ifndef MYCOIN_H
#define MYCOIN_H
#include <QPushButton>
#include <QTimer>
class MyCoin : public QPushButton
{
Q_OBJECT
public:
// explicit MyCoin(QWidget *parent = 0);
//重载构造 butImg代表图片路径
MyCoin(QString butImg);
//改变标志,执行翻转效果
void changeFlag();
//重写按钮按下事件
void mousePressEvent(QMouseEvent *e);
//X坐标
int posX;
//Y坐标
int posY;
//正反标志
bool flag;
//正面翻反面定时器
QTimer *timer1;
//反面翻正面定时器
QTimer *timer2;
//最小图片
int min = 1;
//最大图片
int max = 8;
//做翻转动画的标志
bool isAnimation = false;
//胜利标志
bool isWin = false;
signals:
public slots:
};
#endif // MYCOIN_H
//mycoin.cpp
#include "mycoin.h"
#include <QDebug>
//MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
//{
//}
//重载构造 butImg代表图片路径
MyCoin::MyCoin(QString butImg)
{
QPixmap pixmap;
bool ret = pixmap.load(butImg);
if(!ret)
{
qDebug() << butImg <<"加载图片失败";
}
this->setFixedSize(pixmap.width(),pixmap.height());
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
timer1 = new QTimer(this);
timer2 = new QTimer(this);
connect(timer1, &QTimer::timeout, [=](){
QPixmap pixmap;
QString str = QString(":/res/Coin000%1.png").arg(this->min++);
pixmap.load(str);
this->setFixedSize(pixmap.width(),pixmap.height());
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
if(this->min > this->max)
{
this->min = 1;
this->isAnimation = false;
timer1->stop();
}
});
connect(timer2, &QTimer::timeout, [=](){
QPixmap pixmap;
QString str = QString(":/res/Coin000%1.png").arg(this->max--);
pixmap.load(str);
this->setFixedSize(pixmap.width(),pixmap.height());
this->setStyleSheet("QPushButton{border:0px;}");
this->setIcon(pixmap);
this->setIconSize(QSize(pixmap.width(),pixmap.height()));
if(this->max < this->min)
{
this->max = 8;
this->isAnimation = false;
timer2->stop();
}
});
}
//改变标志,执行翻转效果
void MyCoin::changeFlag()
{
if(this->flag)
{
timer1->start(30);
this->isAnimation = true;
this->flag = false;
}
else
{
timer2->start(30);
this->isAnimation = true;
this->flag = true;
}
}
//重写按钮按下事件
void MyCoin::mousePressEvent(QMouseEvent *e)
{
if(this->isAnimation || isWin==true)
{
return;
}
else
{
return QPushButton::mousePressEvent(e);
}
}
3.5 关卡数据类 dataconfig.h 和 dataconfig.cpp
//dataconfig.h
#ifndef DATACONFIG_H
#define DATACONFIG_H
#include <QObject>
#include <QMap>
#include <QVector>
class dataConfig : public QObject
{
Q_OBJECT
public:
explicit dataConfig(QObject *parent = 0);
public:
QMap<int, QVector< QVector<int> > >mData;
signals:
public slots:
};
#endif // DATACONFIG_H
//dataconfig.cpp
#include "dataconfig.h"
#include <QDebug>
dataConfig::dataConfig(QObject *parent) : QObject(parent)
{
int array1[4][4] = {{1, 1, 1, 1},
{1, 1, 0, 1},
{1, 0, 0, 0},
{1, 1, 0, 1} } ;
QVector< QVector<int>> v;
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array1[i][j]);
}
v.push_back(v1);
}
mData.insert(1,v);
int array2[4][4] = { {1, 0, 1, 1},
{0, 0, 1, 1},
{1, 1, 0, 0},
{1, 1, 0, 1}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array2[i][j]);
}
v.push_back(v1);
}
mData.insert(2,v);
int array3[4][4] = { {0, 0, 0, 0},
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array3[i][j]);
}
v.push_back(v1);
}
mData.insert(3,v);
int array4[4][4] = { {0, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 1, 1},
{1, 1, 1, 1}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array4[i][j]);
}
v.push_back(v1);
}
mData.insert(4,v);
int array5[4][4] = { {1, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 0, 0, 1}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array5[i][j]);
}
v.push_back(v1);
}
mData.insert(5,v);
int array6[4][4] = { {1, 0, 0, 1},
{0, 1, 1, 0},
{0, 1, 1, 0},
{1, 0, 0, 1}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array6[i][j]);
}
v.push_back(v1);
}
mData.insert(6,v);
int array7[4][4] = { {0, 1, 1, 1},
{1, 0, 1, 1},
{1, 1, 0, 1},
{1, 1, 1, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array7[i][j]);
}
v.push_back(v1);
}
mData.insert(7,v);
int array8[4][4] = { {0, 1, 0, 1},
{1, 0, 0, 0},
{0, 0, 0, 1},
{1, 0, 1, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array8[i][j]);
}
v.push_back(v1);
}
mData.insert(8,v);
int array9[4][4] = { {1, 0, 1, 0},
{1, 0, 1, 0},
{0, 0, 1, 0},
{1, 0, 0, 1}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array9[i][j]);
}
v.push_back(v1);
}
mData.insert(9,v);
int array10[4][4] = { {1, 0, 1, 1},
{1, 1, 0, 0},
{0, 0, 1, 1},
{1, 1, 0, 1}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array10[i][j]);
}
v.push_back(v1);
}
mData.insert(10,v);
int array11[4][4] = { {0, 1, 1, 0},
{1, 0, 0, 1},
{1, 0, 0, 1},
{0, 1, 1, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array11[i][j]);
}
v.push_back(v1);
}
mData.insert(11,v);
int array12[4][4] = { {0, 1, 1, 0},
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array12[i][j]);
}
v.push_back(v1);
}
mData.insert(12,v);
int array13[4][4] = { {0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 1, 1, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array13[i][j]);
}
v.push_back(v1);
}
mData.insert(13,v);
int array14[4][4] = { {1, 0, 1, 1},
{0, 1, 0, 1},
{1, 0, 1, 0},
{1, 1, 0, 1}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array14[i][j]);
}
v.push_back(v1);
}
mData.insert(14,v);
int array15[4][4] = { {0, 1, 0, 1},
{1, 0, 0, 0},
{1, 0, 0, 0},
{0, 1, 0, 1}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array15[i][j]);
}
v.push_back(v1);
}
mData.insert(15,v);
int array16[4][4] = { {0, 1, 1, 0},
{1, 1, 1, 1},
{1, 1, 1, 1},
{0, 1, 1, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array16[i][j]);
}
v.push_back(v1);
}
mData.insert(16,v);
int array17[4][4] = { {0, 1, 1, 1},
{0, 1, 0, 0},
{0, 0, 1, 0},
{1, 1, 1, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array17[i][j]);
}
v.push_back(v1);
}
mData.insert(17,v);
int array18[4][4] = { {0, 0, 0, 1},
{0, 0, 1, 0},
{0, 1, 0, 0},
{1, 0, 0, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array18[i][j]);
}
v.push_back(v1);
}
mData.insert(18,v);
int array19[4][4] = { {0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array19[i][j]);
}
v.push_back(v1);
}
mData.insert(19,v);
int array20[4][4] = { {0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}} ;
v.clear();
for(int i = 0 ; i < 4;i++)
{
QVector<int>v1;
for(int j = 0 ; j < 4;j++)
{
v1.push_back(array20[i][j]);
}
v.push_back(v1);
}
mData.insert(20,v);
//测试数据
// for( QMap<int, QVector< QVector<int> > >::iterator it = mData.begin();it != mData.end();it++ )
// {
// for(QVector< QVector<int> >::iterator it2 = (*it).begin(); it2!= (*it).end();it2++)
// {
// for(QVector<int>::iterator it3 = (*it2).begin(); it3 != (*it2).end(); it3++ )
// {
// qDebug() << *it3 ;
// }
// }
// qDebug() << endl;
// }
}
3.6 首页主界面 mainscene.h 和 mainscene.cpp
//mainscene.h
#ifndef MAINSCENE_H
#define MAINSCENE_H
#include <QMainWindow>
namespace Ui {
class MainScene;
}
class MainScene : public QMainWindow
{
Q_OBJECT
public:
explicit MainScene(QWidget *parent = 0);
~MainScene();
//重写PaintEvent绘图事件
void paintEvent(QPaintEvent *);
private:
Ui::MainScene *ui;
};
#endif // MAINSCENE_H
//mainscene.cpp
#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include "chooselevelscene.h"
#include <QTimer>
#include <QSound>
MainScene::MainScene(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainScene)
{
ui->setupUi(this);
//设置窗口
this->setFixedSize(320,588);
this->setWindowTitle("krt翻金币游戏");
this->setWindowIcon(QIcon(":/res/Coin0001.png"));
//设置退出连接
connect(ui->actionQuit, &QAction::triggered, [=](){
this->close();
});
//选择关卡场景
ChooseLevelScene * chooseScene = new ChooseLevelScene;
//创建开始按钮
MyPushButton *startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
startBtn->setParent(this);
startBtn->move(this->width()*0.5 - startBtn->width()*0.5, this->height()*0.7);
//开始音效
QSound *startSound = new QSound(":/res/TapButtonSound.wav",this);
//监听开始按钮点击事件,执行特效
connect(startBtn, &MyPushButton::clicked, [=](){
startSound->play();
startBtn->zoomDown();
startBtn->zoomUp();
//延时0.5S后进入选择场景
QTimer::singleShot(500,this,[=](){
this->hide();
//确保移动场景后下一个场景位置一致
chooseScene->setGeometry((this->geometry()));
chooseScene->show();
});
});
//监听选择场景的返回按钮的自定义信号
connect(chooseScene, &ChooseLevelScene::chooseSceneBack, [=](){
//确保移动场景后下一个场景位置一致
this->setGeometry(chooseScene->geometry());
this->show();
});
}
//重写PaintEvent绘图事件
void MainScene::paintEvent(QPaintEvent *)
{
//创建画家,指定当前窗口为绘图设备
QPainter painter(this);
//绘制背景图
QPixmap pix(":/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);
}
MainScene::~MainScene()
{
delete ui;
}
3.7 选择关卡界面 chooselevelscene.h 和 chooselevelscene.h
//chooselevelscene.h
#ifndef CHOOSELEVELSCENE_H
#define CHOOSELEVELSCENE_H
#include <QMainWindow>
#include "playscene.h"
class ChooseLevelScene : public QMainWindow
{
Q_OBJECT
public:
explicit ChooseLevelScene(QWidget *parent = 0);
//重写绘图事件
void paintEvent(QPaintEvent *);
//核心 翻金币场景
PlayScene *pScene = NULL;
signals:
//自定义信号,关闭自身
void chooseSceneBack();
public slots:
};
#endif // CHOOSELEVELSCENE_H
//chooselevelscene.cpp
#include "chooselevelscene.h"
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include <QSound>
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
//设置窗口
this->setFixedSize(320,588);
this->setWindowIcon(QPixmap(":/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();
});
//返回按钮音效
QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);
//返回按钮
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, [=](){
backSound->play();
QTimer::singleShot(500,this,[=](){
this->hide();
emit this->chooseSceneBack();
});
});
//选择关卡按钮音效
QSound *chooseSound = new QSound(":/res/TapButtonSound.wav",this);
//关卡按钮
for(int i=0;i<20;i++)
{
//创建关卡按钮
MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
menuBtn->setParent(this);
menuBtn->move(25+(i%4)*70, 130+(i/4)*70);
//创建数字标签
QLabel *label = new QLabel;
label->setParent(this);
label->setFixedSize(menuBtn->width(),menuBtn->height());
label->setText(QString::number(i+1));
label->setAlignment(Qt::AlignCenter);
label->move(25+(i%4)*70, 130+(i/4)*70);
label->setAttribute(Qt::WA_TransparentForMouseEvents, true);
connect(menuBtn, &MyPushButton::clicked, [=](){
qDebug() << "select:"<<i+1;
chooseSound->play();
if(pScene == NULL)
{
this->hide();
pScene = new PlayScene(i+1);
//确保移动场景后下一个场景位置一致
pScene->setGeometry(this->geometry());
pScene->show();
//监听PlayScene自定义信号
connect(pScene, &PlayScene::chooseSceneBack, [=](){
this->setGeometry(pScene->geometry());
this->show();
delete pScene;
pScene = NULL;
});
}
});
}
}
//重写绘图事件
void ChooseLevelScene::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap pix(":/res/OtherSceneBg.png");
painter.drawPixmap(0,0,this->width(),this->height(),pix);
pix.load(":/res/Title.png");
painter.drawPixmap((this->width()-pix.width())*0.5, 30, pix.width(),pix.height(),pix);
}
3.8 翻金币界面 playscene.h 和 playscene.cpp
//playscene.h
#ifndef PLAYSCENE_H
#define PLAYSCENE_H
#include <QMainWindow>
#include <QDebug>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSound>
class PlayScene : public QMainWindow
{
Q_OBJECT
public:
// explicit PlayScene(QWidget *parent = 0);
//重载构造函数 menu代表关卡数字
PlayScene(int index);
//重载绘图事件
void paintEvent(QPaintEvent *);
//成员变量 记录关卡索引
int levalIndex;
//用户记录当前关卡的二维数组数据
int gameArray[4][4];
//金币按钮数组
MyCoin *coinBtn[4][4];
//胜利标志
bool isWin = true;
signals:
//自定义信号 关闭自身
void chooseSceneBack();
public slots:
};
#endif // PLAYSCENE_H
//playscene.cpp
#include "playscene.h"
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{
//}
//重载构造函数 menu代表关卡数字
PlayScene::PlayScene(int index)
{
//设置窗口
qDebug() << "当前关卡为 "<<index;
this->levalIndex = index;
this->setFixedSize(320,588);
this->setWindowIcon(QIcon(QPixmap(":/res/Coin0001.png")));
this->setWindowTitle("翻金币");
QMenuBar *bar = this->menuBar();
setMenuBar(bar);
QMenu *startMenu = bar->addMenu("开始");
QAction *quitAction = startMenu->addAction("退出");
connect(quitAction, &QAction::triggered, [=](){
this->close();
});
//返回按钮音效
QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);
//返回按钮
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, [=](){
backSound->play();
QTimer::singleShot(500,this,[=](){
this->hide();
emit this->chooseSceneBack();
});
});
//关卡显示
QLabel *label = new QLabel;
label->setParent(this);
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20);
label->setFont(font);
QString str = QString("Level:%1").arg(this->levalIndex);
label->setText(str);
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];
}
}
//翻金币音效
QSound *flipSound = new QSound(":/res/ConFlipSound.wav",this);
//胜利音效
QSound *winSound = new QSound(":/res/LevelWinSound.wav",this);
//胜利图片
QLabel *winLabel = new QLabel;
QPixmap tmpPix;
tmpPix.load(":/res/LevelCompletedDialogBg.png");
winLabel->setParent(this);
winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
winLabel->setPixmap(tmpPix);
winLabel->move((this->width()-tmpPix.width())*0.5, -tmpPix.height());
//金币翻转
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
QLabel *label = new QLabel;
label->setParent(this);
label->setGeometry(0,0,50,50);
label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
label->move(57+i*50,200+j*50);
QString img;
if(gameArray[i][j] == 1)
{
img = ":/res/Coin0001.png";
}
else
{
img = ":/res/Coin0008.png";
}
MyCoin *coin = new MyCoin(img);
coin->setParent(this);
coin->move(59+i*50,204+j*50);
coin->posX = i;
coin->posY = j;
coin->flag = gameArray[i][j];
coinBtn[i][j] = coin;
connect(coin, &MyCoin::clicked, [=](){
flipSound->play();
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][coin->posY+1]==0?1:0;
}
if(coin->posY-1 >= 0)
{
coinBtn[coin->posX][coin->posY-1]->changeFlag();
gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX][coin->posY-1]==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)
{
qDebug() << "胜利";
winSound->play();
QPropertyAnimation * animation = new QPropertyAnimation(winLabel, "geometry");
animation->setDuration(1000);
animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
animation->setEasingCurve(QEasingCurve::OutBounce);
animation->start();
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
coinBtn[i][j]->isWin = true;
}
}
}
});
});
}
}
}
//重载绘图事件
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);
}
3.9 主函数文件 main.cpp
//main.cpp
#include "mainscene.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainScene w;
w.show();
return a.exec();
}