#include "playscene.h"
#include<QDebug>
#include<QMenuBar>
#include<QPainter>
#include<mypushbutton.h>
#include<QTimer>
#include<QLabel>
#include "mycoin.h"
#include"dataconfig.h"
#include<QPropertyAnimation>
#include<QSound>
#pragma execution_character_set("utf-8")
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{
//}
PlayScene::PlayScene(int levelNum)
{
QString str = QString("当前关卡为 第 %1 关").arg(levelNum);
qDebug()<<str;
this->levalIndex = levelNum;
//设置窗口固定大小
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 *flipSound = new QSound(":/res/ConFlipSound.wav",this);
//胜利按钮音效
QSound *winSound = new QSound(":/res/LevelWinSound.wav",this);
//返回按钮音效
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();
//触发自定义信号,关闭自身,该信号写到 signals下做声明
emit this->chooseSceneBack();
}
);
});
//显示当前的关卡数
QLabel *label = new QLabel;
label->setParent(this);
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20);
label->setFont(font);
QString str1 = QString("Leavel: %1").arg(this->levalIndex);
label->setText(str1);
label->setGeometry(QRect(30, this->height() - 50,120, 50)); //设置大小和位置
// label->setText(QString::number( this->levalIndex));
// label->setGeometry(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 tmpPix;
tmpPix.load(":/res/LevelCompletedDialogBg.png");
winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
winLabel->setPixmap(tmpPix);
winLabel->setParent(this);
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->setGeometry(0,0,50,50);
label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
label->setParent(this);
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; //记录x坐标
coin->posY = j; //记录y坐标
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+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++)
{
//qDebug() << coinBtn[i][j]->flag ;
if( coinBtn[i][j]->flag == false)
{
没有合适的资源?快使用搜索试试~ 我知道了~
看传智教育qt课程之后编写的qt初学者简单小程序,里面有详细教程,包含源码,包含发布版本。欢迎下载!!!!
共42个文件
png:18个
cpp:7个
h:6个
需积分: 28 5 下载量 23 浏览量
2022-04-05
15:50:42
上传
评论
收藏 9.63MB RAR 举报
温馨提示
看传智教育qt课程之后编写的qt初学者简单小程序,里面有详细教程,包含源码,包含发布版本。欢迎下载!!!!
资源详情
资源评论
资源推荐
收起资源包目录
翻金币.rar (42个子文件)
翻金币
res
Qt案例-翻金币教程.docx 5.99MB
program
mypushbutton.h 808B
mainscene.ui 980B
res
MenuSceneStartButton.png 37KB
BoardNode(1).png 18KB
MenuSceneBg.png 371KB
LevelWinSound.wav 198KB
Coin0006.png 20KB
Coin0008.png 22KB
LevelIcon.png 21KB
Coin0007.png 22KB
PlayLevelSceneBg.png 364KB
BackButton.png 19KB
BackButtonSelected.png 19KB
Coin0003.png 20KB
Coin0005.png 18KB
Title.png 48KB
BackButtonSound.wav 20KB
LevelCompletedDialogBg.png 61KB
Coin0002.png 21KB
OtherSceneBg.png 331KB
ConFlipSound.wav 60KB
Coin0001.png 22KB
Coin0004.png 18KB
TapButtonSound.wav 26KB
CoinFlip.pro 1KB
res.qrc 996B
program.rar 1.4MB
mypushbutton.cpp 3KB
mainscene.cpp 3KB
mycoin.cpp 3KB
chooselevelscene.cpp 3KB
CoinFlip.pro.user 134KB
dataconfig.h 311B
playscene.cpp 12KB
main.cpp 234B
mycoin.h 817B
chooselevelscene.h 576B
dataconfig.cpp 9KB
mainscene.h 570B
playscene.h 559B
release
CoinFlip.exe 1.76MB
共 42 条
- 1
LeoTao7
- 粉丝: 2
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0