mypushbutton.h

  1. #ifndef MYPUSHBUTTON_H
  2. #define MYPUSHBUTTON_H
  3. #include <QPushButton>
  4. class MyPushButton : public QPushButton
  5. {
  6. Q_OBJECT
  7. public:
  8. // explicit MyPushButton(QWidget *parent = nullptr);
  9. // 构造函数 参数1:正常显示的图片路径,参数2:按下后显示的图片路径
  10. MyPushButton(QString normalImg, QString pressImg = "");
  11. // 成员属性 保存用户传入的默认显示路径 以及按下后显示的图片路径
  12. QString normalImgPath;
  13. QString presslImgPath;
  14. // 弹跳特性
  15. void zoom1(); // 向下跳
  16. void zoom2(); // 向上跳
  17. // 重写按钮的按下和释放
  18. void mousePressEvent(QMouseEvent *e);
  19. void mouseReleaseEvent(QMouseEvent *e);
  20. signals:
  21. public slots:
  22. };
  23. #endif // MYPUSHBUTTON_H

mypushbutton.cpp文件

  1. #include "mypushbutton.h"
  2. #include <QDebug>
  3. #include <QPropertyAnimation>
  4. //MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
  5. //{
  6. //}
  7. MyPushButton::MyPushButton(QString normalImg, QString pressImg)
  8. {
  9. this->normalImgPath = normalImg;
  10. this->presslImgPath = pressImg;
  11. QPixmap pix;
  12. bool ret = pix.load(normalImg);
  13. if(!ret){
  14. qDebug() << "图片加载失败";
  15. return;
  16. }
  17. // 设置图片的固定大小
  18. this->setFixedSize(pix.width(), pix.height());
  19. // 设置不规则图片样式
  20. this->setStyleSheet("QPushButton{border: 0px;}");
  21. // 设置图标
  22. this->setIcon(pix);
  23. // 设置图标大小
  24. this->setIconSize(QSize(pix.width(), pix.height()));
  25. }
  26. // 向下跳
  27. void MyPushButton::zoom1()
  28. {
  29. // 创建动态对象
  30. QPropertyAnimation * animation = new QPropertyAnimation(this, "geometry");
  31. // 设置动画的时间间隔
  32. animation->setDuration(200);
  33. // 起始位置
  34. animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));
  35. // 结束位置
  36. animation->setEndValue(QRect(this->x(), this->y() + 10, this->width(), this->height()));
  37. // 设置弹跳曲线
  38. animation->setEasingCurve(QEasingCurve::OutBounce);
  39. // 执行动画
  40. animation->start();
  41. }
  42. // 向上跳
  43. void MyPushButton::zoom2()
  44. {
  45. // 创建动态对象
  46. QPropertyAnimation * animation = new QPropertyAnimation(this, "geometry");
  47. // 设置动画的时间间隔
  48. animation->setDuration(200);
  49. // 起始位置
  50. animation->setStartValue(QRect(this->x(), this->y() + 10, this->width(), this->height()));
  51. // 结束位置
  52. animation->setEndValue(QRect(this->x(), this->y(), this->width(), this->height()));
  53. // 设置弹跳曲线
  54. animation->setEasingCurve(QEasingCurve::OutBounce);
  55. // 执行动画
  56. animation->start();
  57. }
  58. void MyPushButton::mousePressEvent(QMouseEvent *e)
  59. {
  60. // 先判断是否存在图片
  61. if(this->presslImgPath != "") // 传入的按下图片不为空 说明需要有按下状态,切换图片
  62. {
  63. QPixmap pix;
  64. bool ret = pix.load(this->presslImgPath);
  65. if(!ret){
  66. qDebug() << "图片加载失败";
  67. return;
  68. }
  69. // 设置图片的固定大小
  70. this->setFixedSize(pix.width(), pix.height());
  71. // 设置不规则图片样式
  72. this->setStyleSheet("QPushButton{border: 0px;}");
  73. // 设置图标
  74. this->setIcon(pix);
  75. // 设置图标大小
  76. this->setIconSize(QSize(pix.width(), pix.height()));
  77. }
  78. // 让父类执行其他的内容
  79. return QPushButton::mousePressEvent(e);
  80. }
  81. void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
  82. {
  83. // 先判断是否存在图片
  84. if(this->presslImgPath != "") // 传入的按下图片不为空 说明需要有按下状态,切换成初始图片
  85. {
  86. QPixmap pix;
  87. bool ret = pix.load(this->normalImgPath);
  88. if(!ret){
  89. qDebug() << "图片加载失败";
  90. return;
  91. }
  92. // 设置图片的固定大小
  93. this->setFixedSize(pix.width(), pix.height());
  94. // 设置不规则图片样式
  95. this->setStyleSheet("QPushButton{border: 0px;}");
  96. // 设置图标
  97. this->setIcon(pix);
  98. // 设置图标大小
  99. this->setIconSize(QSize(pix.width(), pix.height()));
  100. }
  101. // 让父类执行其他的内容
  102. return QPushButton::mouseReleaseEvent(e);
  103. }

mainscene.h文件

  1. #ifndef MAINSCENE_H
  2. #define MAINSCENE_H
  3. #include <QMainWindow>
  4. #include "chooselevelscene.h"
  5. namespace Ui {
  6. class MainScene;
  7. }
  8. class MainScene : public QMainWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit MainScene(QWidget *parent = 0);
  13. ~MainScene();
  14. // 重现paintEvent事件 画背景图
  15. void paintEvent(QPaintEvent *event);
  16. ChooseLevelScene * chooseScene = NULL;
  17. private:
  18. Ui::MainScene *ui;
  19. };
  20. #endif // MAINSCENE_H

mainscene.cpp文件

  1. #include "mainscene.h"
  2. #include "ui_mainscene.h"
  3. #include "mypushbutton.h"
  4. #include <QPainter>
  5. #include <QDebug>
  6. #include <QTimer>
  7. #include <QSound> // 多媒体模块下的头文件
  8. MainScene::MainScene(QWidget *parent) :
  9. QMainWindow(parent),
  10. ui(new Ui::MainScene)
  11. {
  12. ui->setupUi(this);
  13. // 配置主场景
  14. // 设置固定大小
  15. setFixedSize(320, 588);
  16. // 设置图标
  17. setWindowIcon(QIcon(":/res/Coin0001.png"));
  18. // 设置标题
  19. setWindowTitle("翻金币主场景");
  20. // 退出按钮实现
  21. connect(ui->actionquit, &QAction::triggered, [=](){
  22. this->close();
  23. });
  24. // 准备开始按钮的音效
  25. QSound * startSound = new QSound(":/res/TapButtonSound.wav", this);
  26. // 开始按钮
  27. MyPushButton * startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
  28. startBtn->setParent(this);
  29. startBtn->move(this->width()*0.5 - startBtn->width()*0.5, this->height()*0.7);
  30. // 实例化选择关卡场景
  31. chooseScene = new ChooseLevelScene;
  32. // 监听选择关卡的返回按钮信号
  33. connect(chooseScene, &ChooseLevelScene::chooseSceneBack, this, [=](){
  34. // 设置chooseScene返回的场景位置
  35. this->setGeometry(chooseScene->geometry());
  36. // 隐藏关卡
  37. chooseScene->hide();
  38. // 重新显示主场景
  39. this->show();
  40. });
  41. connect(startBtn, &MyPushButton::clicked, [=](){
  42. qDebug() << "点击了开始";
  43. // 播放开始音效资源
  44. startSound->play();
  45. // 做弹起的特效
  46. startBtn->zoom1();
  47. startBtn->zoom2();
  48. // 延时进入选择关卡场景中
  49. QTimer::singleShot(500, this, [=](){
  50. // 设置chooseScene去的场景位置
  51. chooseScene->setGeometry(this->geometry());
  52. // 将自身隐藏掉
  53. this->hide();
  54. // 显示选择关卡场景
  55. chooseScene->show();
  56. });
  57. });
  58. }
  59. // 创建画家,指定绘图设备
  60. // 重现paintEvent事件 画背景图
  61. void MainScene::paintEvent(QPaintEvent *event)
  62. {
  63. QPainter painter(this);
  64. QPixmap pix;
  65. pix.load(":/res/PlayLevelSceneBg.png");
  66. // 画背景上图标
  67. painter.drawPixmap(0, 0, this->width(), this->height(), pix);
  68. // 加载标题
  69. pix.load(":/res/Title.png");
  70. // 缩放图片
  71. pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
  72. painter.drawPixmap(10, 30, pix);
  73. }
  74. MainScene::~MainScene()
  75. {
  76. delete ui;
  77. }

chooselevelscene.h文件

  1. #ifndef CHOOSELEVELSCENE_H
  2. #define CHOOSELEVELSCENE_H
  3. #include <QMainWindow>
  4. #include "playscene.h"
  5. class ChooseLevelScene : public QMainWindow
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit ChooseLevelScene(QWidget *parent = nullptr);
  10. // 重写paintEvent事件 画背景图
  11. void paintEvent(QPaintEvent *event);
  12. // 游戏场景对象指针
  13. PlayScene * play = NULL;
  14. signals:
  15. // 写一个自定义信号,告诉主场景点击了返回
  16. void chooseSceneBack();
  17. public slots:
  18. };
  19. #endif // CHOOSELEVELSCENE_H

chooselevelscene.cpp文件

  1. #include "chooselevelscene.h"
  2. #include <QMenuBar>
  3. #include <QPainter>
  4. #include <QDebug>
  5. #include <QTimer>
  6. #include <QLabel>
  7. #include "mypushbutton.h"
  8. #include <QSound> // 多媒体模块下的头文件
  9. ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
  10. {
  11. // 配置选择关卡场景
  12. this->setFixedSize(320, 588);
  13. // 设置图标
  14. this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
  15. // 设置标题
  16. this->setWindowTitle("选择关卡");
  17. // 创建菜单栏
  18. QMenuBar * bar = menuBar();
  19. setMenuBar(bar);
  20. // 创建开始菜单
  21. QMenu * startMenu = bar->addMenu("开始");
  22. // 创建退出菜单项
  23. QAction * quitAction = startMenu->addAction("退出");
  24. connect(quitAction, &QAction::triggered, [=](){
  25. this->close();
  26. });
  27. // 准备关卡按钮的音效
  28. QSound * startSound = new QSound(":/res/TapButtonSound.wav", this);
  29. // 返回按钮的音效
  30. QSound * backSound = new QSound(":/res/BackButtonSound.wav", this);
  31. // 返回按钮
  32. MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
  33. backBtn->setParent(this);
  34. backBtn->move(this->width() - backBtn->width(), this->height() - backBtn->height());
  35. // 点击返回
  36. connect(backBtn, &MyPushButton::clicked, [=](){
  37. // 返回按钮音效
  38. backSound->play();
  39. qDebug() << "点击了返回";
  40. // 告诉主场景 返回了,主场景监听chooseSceneBack的返回按钮
  41. // 延时返回
  42. QTimer::singleShot(200, this, [=](){
  43. emit this->chooseSceneBack();
  44. });
  45. });
  46. // 创建选择关卡按钮
  47. for(int i=0; i<20; i++) {
  48. MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
  49. menuBtn->setParent(this);
  50. menuBtn->move(25 + i%4 * 70, 130 + i/4 * 70);
  51. // 监听每个按钮的点击事件
  52. connect(menuBtn, &MyPushButton::clicked, [=](){
  53. QString str = QString("你选择的是第 %1 关").arg(i+1);
  54. qDebug() << str;
  55. // 音效
  56. startSound->play();
  57. // 进入到游戏场景
  58. this->hide(); // 将选关场景隐藏掉
  59. play = new PlayScene(i+1);
  60. // 设置游戏场景的初始位置
  61. play->setGeometry(this->geometry());
  62. play->show(); // 显示游戏场景
  63. connect(play, &PlayScene::playSceneBack, this, [=](){
  64. this->setGeometry(play->geometry());
  65. // 重新显示关卡场景
  66. this->show();
  67. // 销毁退出的关卡场景
  68. delete play;
  69. play = NULL;
  70. });
  71. });
  72. // 使用label添加数字
  73. QLabel * label = new QLabel;
  74. label->setParent(this);
  75. label->setFixedSize(menuBtn->width(), menuBtn->height());
  76. label->setText(QString::number(i+1));
  77. label->move(25 + i%4 * 70, 130 + i/4 * 70);
  78. // 设置label上的文字对其方式 水平居中和垂直居中
  79. label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  80. // 设置让鼠标进行穿透label
  81. label->setAttribute(Qt::WA_TransparentForMouseEvents);
  82. }
  83. }
  84. void ChooseLevelScene::paintEvent(QPaintEvent *event)
  85. {
  86. QPainter painter(this);
  87. QPixmap pix;
  88. pix.load(":/res/OtherSceneBg.png");
  89. // 画背景上图标
  90. painter.drawPixmap(0, 0, this->width(), this->height(), pix);
  91. // 加载标题
  92. pix.load(":/res/Title.png");
  93. // 缩放图片
  94. // pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
  95. painter.drawPixmap((this->width() - pix.width()) * 0.5, 30, pix.width(), pix.height(), pix);
  96. }

mycoin.h

  1. #ifndef MYCOIN_H
  2. #define MYCOIN_H
  3. #include <QPushButton>
  4. #include <QTimer>
  5. class MyCoin : public QPushButton
  6. {
  7. Q_OBJECT
  8. public:
  9. // explicit MyCoin(QWidget *parent = nullptr);
  10. // 参数代表传入的金币路径还是银币路径
  11. MyCoin(QString btnImg);
  12. // 金币是的属性
  13. int posX; // x坐标位置
  14. int posY; // y坐标位置
  15. bool flag; // 正反标志
  16. // 改变标志的方法
  17. void changeFlag();
  18. QTimer * timer1; // 正面翻反面的定时器
  19. QTimer * timer2; // 反面翻正面的定时器
  20. int min = 1;
  21. int max = 8;
  22. // 执行动画的标志
  23. bool isAnimation = false;
  24. // 是否胜利标志
  25. bool isWin = false;
  26. signals:
  27. public slots:
  28. };
  29. #endif // MYCOIN_H

mycoin.cpp

  1. #include "mycoin.h"
  2. #include <QDebug>
  3. #include <QSound>
  4. //MyCoin::MyCoin(QWidget *parent) : QWidget(parent)
  5. //{
  6. //}
  7. MyCoin::MyCoin(QString btnImg)
  8. {
  9. QPixmap pix;
  10. bool ret = pix.load(btnImg);
  11. if(!ret)
  12. {
  13. QString str = QString("图片 %1 加载失败").arg(btnImg);
  14. qDebug() << str;
  15. return;
  16. }
  17. this->setFixedSize(pix.width(), pix.height());
  18. this->setStyleSheet("QPushButton{border:0px;}");
  19. this->setIcon(pix);
  20. this->setIconSize(QSize(pix.width(),pix.height()));
  21. // 初始化定时器对象
  22. timer1 = new QTimer(this);
  23. timer2 = new QTimer(this);
  24. // 点击金币音效
  25. QSound * flipSound = new QSound(":/res/ConFlipSound.wav", this);
  26. // 监听正面翻反面信号,并且翻转金币
  27. connect(timer1, &QTimer::timeout, [=](){
  28. QPixmap pix;
  29. QString str = QString(":/res/Coin000%1").arg(this->min++);
  30. pix.load(str);
  31. //
  32. this->setFixedSize(pix.width(), pix.height());
  33. this->setStyleSheet("QPushButton{border:0px;}");
  34. this->setIcon(pix);
  35. this->setIconSize(QSize(pix.width(),pix.height()));
  36. // 判断如果翻完了, 将min重置为1
  37. if(this->min > this->max)
  38. {
  39. // // 开始点击金币音效
  40. // flipSound->play();
  41. this->min = 1;
  42. isAnimation = false; // 停止做动画了
  43. timer1->stop();
  44. }
  45. });
  46. // 监听反面翻正面信号,并且翻转金币
  47. connect(timer2, &QTimer::timeout, [=](){
  48. QPixmap pix;
  49. QString str2 = QString(":/res/Coin000%1").arg(this->max--);
  50. pix.load(str2);
  51. //
  52. this->setFixedSize(pix.width(), pix.height());
  53. this->setStyleSheet("QPushButton{border:0px;}");
  54. this->setIcon(pix);
  55. this->setIconSize(QSize(pix.width(),pix.height()));
  56. // 判断如果翻完了, 将max重置为8
  57. if(this->min > this->max)
  58. {
  59. // // 开始点击金币音效
  60. // flipSound->play();
  61. this->max = 8;
  62. isAnimation = false; // 停止做动画了
  63. timer2->stop();
  64. }
  65. });
  66. }
  67. // 正反面标志的方法
  68. void MyCoin::changeFlag()
  69. {
  70. // 点击金币音效
  71. QSound * flipSound = new QSound(":/res/ConFlipSound.wav", this);
  72. if(!this->isAnimation && !this->isWin)
  73. {
  74. // 开始点击金币音效
  75. flipSound->play();
  76. qDebug() << "zc";
  77. if(this->flag)
  78. {
  79. // 如果是正面 翻成反面
  80. timer1->start(30);
  81. isAnimation = true; // 开始做动画了
  82. this->flag = false;
  83. }else{
  84. // 如果是正面 翻成反面
  85. timer2->start(30);
  86. isAnimation = true; // 开始做动画了
  87. this->flag = true;
  88. }
  89. }
  90. }

playscene.h

  1. #ifndef PLAYSCENE_H
  2. #define PLAYSCENE_H
  3. #include <QMainWindow>
  4. #include "mycoin.h"
  5. class PlayScene : public QMainWindow
  6. {
  7. Q_OBJECT
  8. public:
  9. // explicit PlayScene(QWidget *parent = nullptr);
  10. PlayScene(int levelNum);
  11. int levelIndex; // 内部成员属性,记录所选关卡
  12. // 重现paintEvent事件 画背景图
  13. void paintEvent(QPaintEvent *event);
  14. int gameArray[4][4]; // 二维数组 维护每个关卡的具体数据
  15. // 二维数组 维护每个关卡的具体数据
  16. MyCoin * coinBtn[4][4];
  17. // 是否胜利标志
  18. bool isWin = false;
  19. //
  20. bool isSucc = false;
  21. signals:
  22. // 写一个自定义信号,告诉关卡场景点击了返回
  23. void playSceneBack();
  24. public slots:
  25. };
  26. #endif // PLAYSCENE_H

playscene.h

  1. #include "playscene.h"
  2. #include <QDebug>
  3. #include <QMenuBar>
  4. #include <QTimer>
  5. #include <QPainter>
  6. #include <QLabel>
  7. #include <QFont>
  8. #include "mypushbutton.h"
  9. #include "mycoin.h"
  10. #include "dataconfig.h"
  11. #include <QPropertyAnimation>
  12. #include <QSound>
  13. //PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
  14. //{
  15. //}
  16. PlayScene::PlayScene(int levelNum)
  17. {
  18. QString str = QString("进入了第 %1 关").arg(levelNum);
  19. qDebug() << str;
  20. // 将传进来的关卡数值进行存储到当前作用域中
  21. this->levelIndex = levelNum;
  22. // 初始化游戏场景
  23. // 设置固定大小
  24. this->setFixedSize(320, 588);
  25. // 设置图标
  26. this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
  27. // 设置标题
  28. this->setWindowTitle(str);
  29. // 创建菜单栏
  30. QMenuBar * bar = menuBar();
  31. setMenuBar(bar);
  32. // 创建开始菜单
  33. QMenu * startMenu = bar->addMenu("开始");
  34. // 创建退出菜单项
  35. QAction * quitAction = startMenu->addAction("退出");
  36. // 点击退出 实现退出游戏
  37. connect(quitAction, &QAction::triggered, [=](){
  38. this->close();
  39. });
  40. // 返回按钮的音效
  41. QSound * backSound = new QSound(":/res/BackButtonSound.wav", this);
  42. // // 点击金币音效
  43. // QSound * flipSound = new QSound(":/res/ConFlipSound.wav", this);
  44. QSound * winSound = new QSound(":/res/LevelWinSound.wav", this);
  45. // 返回按钮
  46. MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
  47. backBtn->setParent(this);
  48. backBtn->move(this->width() - backBtn->width(), this->height() - backBtn->height());
  49. // 点击返回
  50. connect(backBtn, &MyPushButton::clicked, [=](){
  51. // // 返回按钮音效
  52. backSound->play();
  53. qDebug() << "点击了返回";
  54. // 告诉选关场景 返回了,主场景监听playSceneBack的返回按钮
  55. // 延时返回
  56. QTimer::singleShot(200, this, [=](){
  57. emit this->playSceneBack();
  58. });
  59. });
  60. // 显示当前关卡
  61. QLabel * label = new QLabel;
  62. label->setParent(this);
  63. label->setFixedSize(150, 50);
  64. label->setFont(QFont("华文新魏", 20));
  65. QString str2 = QString("Leavel: %1").arg(levelIndex);
  66. label->setText(str2);
  67. label->move(20, this->height()-40);
  68. // 初始化每个关卡的二维数组
  69. dataConfig config;
  70. for(int i=0; i<4; i++)
  71. {
  72. for(int j=0; j<4; j++)
  73. {
  74. this->gameArray[i][j] = config.mData[this->levelIndex][i][j];
  75. }
  76. }
  77. // 胜利的图片显示
  78. QLabel * winLabel = new QLabel;
  79. QPixmap tmpPix;
  80. tmpPix.load(":/res/LevelCompletedDialogBg.png");
  81. winLabel->setGeometry(0, 0, tmpPix.width(), tmpPix.height());
  82. winLabel->setPixmap(tmpPix);
  83. winLabel->setParent(this);
  84. winLabel->move((this->width() - tmpPix.width())*0.5, -tmpPix.height());
  85. // 显示金币的背景图案
  86. for(int i=0; i<4; i++)
  87. {
  88. for(int j = 0; j<4; j++) {
  89. // 绘制金币背景图
  90. QLabel * label2 = new QLabel;
  91. label2->setGeometry(0, 0, 50, 50);
  92. label2->setPixmap(QPixmap(":/res/BoardNode(1).png"));
  93. label2->setParent(this);
  94. label2->move(57 + i*50, 200+j*50);
  95. // 创建金币
  96. QString str;
  97. if(this->gameArray[i][j] == 1)
  98. {
  99. // 显示金币
  100. str = ":/res/Coin0001.png";
  101. }else {
  102. // 显示银币
  103. str = ":/res/Coin0008.png";
  104. }
  105. MyCoin * coin = new MyCoin(str);
  106. coin->setParent(this);
  107. coin->move(59 + i*50, 204+j*50);
  108. // 给金币属性赋值
  109. coin->posX = i;
  110. coin->posY = j;
  111. coin->flag = this->gameArray[i][j]; // 1正面,0反面
  112. // 将金币放入金币的二维数组中,以便后期的维护
  113. coinBtn[i][j] = coin;
  114. // 点击金币 进行翻转
  115. connect(coin, &MyCoin::clicked, [=](){
  116. coin->changeFlag();
  117. this->gameArray[i][j] = this->gameArray[i][j] == 0 ? 1 : 0;
  118. // 翻转周围金币
  119. // 右侧
  120. QTimer::singleShot(200, this, [=](){
  121. if(coin->posX + 1 <= 3)
  122. {
  123. qDebug() << coin->posX;
  124. coinBtn[coin->posX + 1][coin->posY]->changeFlag();
  125. this->gameArray[coin->posX + 1][coin->posY] = this->gameArray[coin->posX + 1][coin->posY] == 0 ? 1 : 0;
  126. }
  127. // 左侧
  128. if(coin->posX - 1 >= 0)
  129. {
  130. qDebug() << coin->posX;
  131. coinBtn[coin->posX - 1][coin->posY]->changeFlag();
  132. this->gameArray[coin->posX - 1][coin->posY] = this->gameArray[coin->posX - 1][coin->posY] == 0 ? 1 : 0;
  133. }
  134. // 上侧
  135. if(coin->posY + 1 <= 3)
  136. {
  137. qDebug() << coin->posY;
  138. coinBtn[coin->posX][coin->posY + 1]->changeFlag();
  139. this->gameArray[coin->posX][coin->posY + 1] = this->gameArray[coin->posX][coin->posY + 1] == 0 ? 1 : 0;
  140. }
  141. // 下侧
  142. if(coin->posY - 1 >= 0)
  143. {
  144. qDebug() << coin->posY;
  145. coinBtn[coin->posX][coin->posY - 1]->changeFlag();
  146. this->gameArray[coin->posX][coin->posY - 1] = this->gameArray[coin->posX][coin->posY - 1] == 0 ? 1 : 0;
  147. }
  148. // 判断是否胜利
  149. this->isWin = true;
  150. for(int i=0; i<4; i++)
  151. {
  152. for(int j=0; j<4; j++)
  153. {
  154. if(coinBtn[i][j]->flag == false)
  155. {
  156. this->isWin = false;
  157. break;
  158. }
  159. }
  160. }
  161. if(this->isWin == true && !this->isSucc)
  162. {
  163. // 胜利了
  164. //胜利音效
  165. winSound->play();
  166. qDebug() << "游戏胜利";
  167. this->isSucc = true;
  168. // 将所有按钮的胜利标志改为true
  169. for(int i=0; i<4; i++)
  170. {
  171. for(int j=0; j<4; j++)
  172. {
  173. coinBtn[i][j]->isWin = true;
  174. }
  175. }
  176. // 将胜利的图片移动下来
  177. QPropertyAnimation * animation = new QPropertyAnimation(winLabel, "geometry");
  178. // 设置时间间隔
  179. animation->setDuration(1000);
  180. animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
  181. animation->setEndValue(QRect(winLabel->x(), winLabel->y() + 114, winLabel->width(), winLabel->height()));
  182. // 设置弹跳曲线
  183. animation->setEasingCurve(QEasingCurve::OutBounce);
  184. animation->start();
  185. }
  186. });
  187. });
  188. }
  189. }
  190. }
  191. // 创建画家,指定绘图设备
  192. // 重现paintEvent事件 画背景图
  193. void PlayScene::paintEvent(QPaintEvent *event)
  194. {
  195. QPainter painter(this);
  196. QPixmap pix;
  197. pix.load(":/res/PlayLevelSceneBg.png");
  198. // 画背景上图标
  199. painter.drawPixmap(0, 0, this->width(), this->height(), pix);
  200. // 加载标题
  201. pix.load(":/res/Title.png");
  202. // 缩放图片
  203. pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
  204. painter.drawPixmap(10, 30, pix);
  205. }

image.png