• 翻金币项目 QT项目 (利用Qt 5.80 实现 )


    1.项目的性质:游戏


    2.项目的名字:翻金币


    3.实现的软件 ; QT creator 4.2.1   , NisEdit  , Nsis


    4.游戏介绍: 

            1.包括游戏界面

            2.游戏音效

            


    5.游戏的资源(res)

    已经上传

    整个的代码包: CoinFilp

    已经上传


    6.文件:

    1.chooselevelscene.h

    2.dataconfig.h

    3.mainsence.h

    4.mycoin.h

    5.mypushbutton.h

    6.playscene.h

    7.main.cpp

    8.chooselevelscene.cpp

    9.dataconfig.cpp

    10.mainsence.cpp

    11.mycoin.cpp

    12.mypushbutton.cpp

    13.playscene.cpp


    7.ui ->  设计界面 (添加了一个菜单栏  开始  ,退出的菜单项 )


    8.资源文件

    res


    9.代码:

     CoinFlip.pro  (这里注意  添加了  一个   multimedia    第7 行 )

    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2022-09-23T10:33:56
    4. #
    5. #-------------------------------------------------
    6. QT += core gui multimedia
    7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    8. TARGET = CoinFlip
    9. TEMPLATE = app
    10. # The following define makes your compiler emit warnings if you use
    11. # any feature of Qt which as been marked as deprecated (the exact warnings
    12. # depend on your compiler). Please consult the documentation of the
    13. # deprecated API in order to know how to port your code away from it.
    14. DEFINES += QT_DEPRECATED_WARNINGS
    15. # You can also make your code fail to compile if you use deprecated APIs.
    16. # In order to do so, uncomment the following line.
    17. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    18. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    19. SOURCES += main.cpp\
    20. mainsence.cpp \
    21. mypushbutton.cpp \
    22. chooselevelscene.cpp \
    23. playscene.cpp \
    24. mycoin.cpp \
    25. dataconfig.cpp
    26. HEADERS += mainsence.h \
    27. mypushbutton.h \
    28. chooselevelscene.h \
    29. playscene.h \
    30. mycoin.h \
    31. dataconfig.h
    32. FORMS += mainsence.ui
    33. RESOURCES += \
    34. res.qrc

     

    1.chooselevelscene.h

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

    2.dataconfig.h

    1. #ifndef DATACONFIG_H
    2. #define DATACONFIG_H
    3. #include <QObject>
    4. #include <QMap>//STL 地图
    5. #include <QVector>//STL 动态数组
    6. class dataconfig : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit dataconfig(QObject *parent = 0);
    11. QMap<int ,QVector< QVector<int> > >mData;//一个地图
    12. //地图的 key 是 int 类型 value 是 int 的二维的数组QVector< QVector<int> >
    13. signals:
    14. public slots:
    15. };
    16. #endif // DATACONFIG_H

    3.mainsence.h

    1. #ifndef MAINSENCE_H
    2. #define MAINSENCE_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. namespace Ui {
    8. class Mainsence;
    9. }
    10. class Mainsence : public QMainWindow
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit Mainsence(QWidget *parent = 0);
    15. ~Mainsence();
    16. //重新paintEvent 事件, 画背景图
    17. void paintEvent(QPaintEvent *);//括号里面是数据类型,不需要写参数,没事
    18. ChooseLevelScene * chooseScene =NULL;
    19. private:
    20. Ui::Mainsence *ui;
    21. };
    22. #endif // MAINSENCE_H

    4.mycoin.h

    1. #ifndef MYCOIN_H
    2. #define MYCOIN_H
    3. #include <QWidget>
    4. #include <QPushButton>
    5. #include <QString>
    6. #include <QDebug>
    7. #include <QTimer>
    8. class MyCoin : public QPushButton
    9. {
    10. Q_OBJECT
    11. public:
    12. //explicit MyCoin(QWidget *parent = 0);
    13. //参数表示 传进来的金币路径 还是银币路径
    14. MyCoin(QString btnImg);
    15. //金币的属性
    16. int posX;//x坐标
    17. int posY;//y坐标
    18. bool flag;//正反的标志
    19. void changeFlag();//改变金币的状态
    20. QTimer *timer1;//正面翻反面的定时器
    21. QTimer *timer2;//反面翻正面的定时器
    22. int min=1;//图片的最小号码数
    23. int max=8;//图片的最大号码数
    24. //执行动画 标志
    25. bool isAnimation = false;// 判断他是否在执行翻转的操作 防止用户狂点的行为
    26. //重写 鼠标按下的事件
    27. void mousePressEvent(QMouseEvent * e);
    28. //设置一个是否可以翻转的标志
    29. bool ispaly=true;
    30. signals:
    31. public slots:
    32. };
    33. #endif // MYCOIN_H

    5.mypushbutton.h

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

    6.playscene.h

    1. #ifndef PLAYSCENE_H
    2. #define PLAYSCENE_H
    3. #include
    4. #include "mycoin.h"
    5. class PlayScene : public QMainWindow
    6. {
    7. Q_OBJECT
    8. public:
    9. //explicit PlayScene(QWidget *parent = 0);
    10. PlayScene(int levelNum);//构造函数
    11. int levelIndex;//内部成员属性;记录所选的关卡数字
    12. //重写paintEvent事件
    13. void paintEvent(QPaintEvent * );
    14. int gameArray[4][4];//二维数组 维护每个关卡的具体数据
    15. //下面这个指针数组是用来存储 金币的 就是下一个关卡的所有的金币对象
    16. MyCoin * coinBtn[4][4];//维护上一个数组的数据正常
    17. //是否胜利的标志
    18. bool isWin;
    19. signals:
    20. void chooseSceneBack();
    21. public slots:
    22. };
    23. #endif // PLAYSCENE_H

    7.main.cpp

    1. #include "mainsence.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Mainsence w;
    7. w.show();
    8. return a.exec();
    9. }

    8.chooselevelscene.cpp

    1. #include "chooselevelscene.h"
    2. #include <QMenuBar>
    3. #include <QDebug>
    4. #include <QPainter>
    5. #include "mypushbutton.h"
    6. #include <QTimer>
    7. #include <QString>
    8. #include <QLabel>
    9. #include <QSound>
    10. ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
    11. {
    12. //配置选择关卡场景
    13. this->setFixedSize(320,588);
    14. //设置图标
    15. this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    16. //设置标题
    17. this->setWindowTitle("选择关卡场景");
    18. //创建菜单栏
    19. QMenuBar * bar = menuBar();
    20. setMenuBar(bar);
    21. //创建开始菜单
    22. QMenu * startMenu = bar->addMenu("开始");
    23. //创建退出 菜单栏
    24. QAction * quitAction = startMenu->addAction("退出");
    25. //点击退出 实现退出游戏 建立连接
    26. connect(quitAction,&QAction::triggered,[=](){
    27. this->close();
    28. });
    29. //选择关卡的音效
    30. QSound * chooseSound = new QSound(":/res/TapButtonSound.wav",this);
    31. //返回按钮的音效
    32. QSound * backSound = new QSound(":/res/TapButtonSound.wav",this);
    33. //返回按钮
    34. mypushbutton * backBtn = new mypushbutton(":/res/BackButton.png", ":/res/BackButtonSelected.png");//这两个图片有细微的差别,这样构成了一个动态的感觉
    35. backBtn->setParent(this);
    36. backBtn->move(this->width() - backBtn->width(),this->height() - backBtn->height());
    37. //点击返回
    38. connect(backBtn,&mypushbutton::clicked,[=](){
    39. //点击了返回按钮的音效
    40. backSound->play();
    41. qDebug()<<"点击了选择关卡的 返回按键";
    42. //告诉主场景,我返回了,主场景监听 chooseLeveScene 的返回按键
    43. //延时发送信号
    44. QTimer::singleShot(500,this,[=](){
    45. emit this->chooseSceneBack();//激发信号 返回开始界面
    46. });
    47. });
    48. //创建选择关卡的按钮
    49. for(int i=0;i<20;i++)
    50. {
    51. mypushbutton * menuBtn = new mypushbutton(":/res/LevelIcon.png");
    52. menuBtn->setParent(this);//设置父亲,方便关闭和销毁
    53. menuBtn->move(25 + i%4 *70 ,130 + i/4 *70);//设置关卡的图片的位置
    54. //监听每个按钮的点击事件 建立信号的连接
    55. connect(menuBtn,&mypushbutton::clicked,[=](){
    56. //选择关卡之后播放音效
    57. chooseSound->play();
    58. QString str =QString("你选择的是第 %1 关").arg(i+1);
    59. qDebug()<<str;
    60. //
    61. //进入游戏场景
    62. this->hide();//进入游戏关卡 ,隐藏游戏选择界面
    63. paly =new PlayScene(i+1);//创建一个游戏的界面
    64. //设置游戏场景出现的初始位置
    65. paly->setGeometry(this->geometry());
    66. paly->show();//显示游戏界面
    67. //退出游戏的信号连接
    68. connect(paly,&PlayScene::chooseSceneBack,[=](){
    69. this->setGeometry(paly->geometry());//窗口的位置的调整
    70. this->show();
    71. delete paly;
    72. paly=NULL;
    73. });
    74. });
    75. //设置选择关卡上的数字
    76. QLabel *label = new QLabel; //建立一个标签的对象
    77. label->setParent(this);//设置父亲, 方便关闭
    78. label->setFixedSize(menuBtn->width(),menuBtn->height());//设置标签的大小
    79. label->setText(QString::number(i+1));//标签里设置数字
    80. label->move(25 + i%4 *70 ,130+ i/4*70);//设置标签的位置
    81. //设置标签 label 上的文字对齐方式 水平居中 和 垂直居中
    82. label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    83. //设置让鼠标进行穿透 51号属性
    84. label->setAttribute((Qt::WA_TransparentForMouseEvents));
    85. }
    86. }
    87. void ChooseLevelScene::paintEvent(QPaintEvent *)
    88. {
    89. //加载背景
    90. QPainter painter(this);
    91. QPixmap pix;//建立对象
    92. pix.load(":/res/OtherSceneBg.png");//加载背景
    93. painter.drawPixmap(0,0,this->width(),this->height(),pix);//加载第一张的图片
    94. //加载标题
    95. pix.load(":/res/Title.png");
    96. painter.drawPixmap((this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix);//加载下一张图片进去
    97. }

    9.dataconfig.cpp

    1. //dataconfig.cpp
    2. #include "dataconfig.h"
    3. #include <QDebug>
    4. dataconfig::dataconfig(QObject *parent) : QObject(parent)//构造函数
    5. {
    6. //二维数组
    7. int array1[4][4] = {{1, 1, 1, 1},
    8. {1, 1, 0, 1},
    9. {1, 0, 0, 0},
    10. {1, 1, 0, 1} } ;
    11. QVector< QVector<int>> v; //一个动态数组的 二维数组
    12. for(int i = 0 ; i < 4;i++)
    13. {
    14. QVector<int>v1;//一个一维数组的动态数组
    15. for(int j = 0 ; j < 4;j++)
    16. {
    17. v1.push_back(array1[i][j]);//把数据写入到动态数组
    18. }
    19. v.push_back(v1);//把一维数组计入到二维数组
    20. }
    21. mData.insert(1,v);//把二维数组 计入到之前的 map 地图中 key =1 value = v
    22. int array2[4][4] = { {1, 0, 1, 1},
    23. {0, 0, 1, 1},
    24. {1, 1, 0, 0},
    25. {1, 1, 0, 1}} ;
    26. v.clear();//清除二维数组。
    27. for(int i = 0 ; i < 4;i++)
    28. {
    29. QVector<int>v1;
    30. for(int j = 0 ; j < 4;j++)
    31. {
    32. v1.push_back(array2[i][j]);
    33. }
    34. v.push_back(v1);
    35. }
    36. mData.insert(2,v);
    37. int array3[4][4] = { {0, 0, 0, 0},
    38. {0, 1, 1, 0},
    39. {0, 1, 1, 0},
    40. {0, 0, 0, 0}} ;
    41. v.clear();
    42. for(int i = 0 ; i < 4;i++)
    43. {
    44. QVector<int>v1;
    45. for(int j = 0 ; j < 4;j++)
    46. {
    47. v1.push_back(array3[i][j]);
    48. }
    49. v.push_back(v1);
    50. }
    51. mData.insert(3,v);
    52. int array4[4][4] = { {0, 1, 1, 1},
    53. {1, 0, 0, 1},
    54. {1, 0, 1, 1},
    55. {1, 1, 1, 1}} ;
    56. v.clear();
    57. for(int i = 0 ; i < 4;i++)
    58. {
    59. QVector<int>v1;
    60. for(int j = 0 ; j < 4;j++)
    61. {
    62. v1.push_back(array4[i][j]);
    63. }
    64. v.push_back(v1);
    65. }
    66. mData.insert(4,v);
    67. int array5[4][4] = { {1, 0, 0, 1},
    68. {0, 0, 0, 0},
    69. {0, 0, 0, 0},
    70. {1, 0, 0, 1}} ;
    71. v.clear();
    72. for(int i = 0 ; i < 4;i++)
    73. {
    74. QVector<int>v1;
    75. for(int j = 0 ; j < 4;j++)
    76. {
    77. v1.push_back(array5[i][j]);
    78. }
    79. v.push_back(v1);
    80. }
    81. mData.insert(5,v);
    82. int array6[4][4] = { {1, 0, 0, 1},
    83. {0, 1, 1, 0},
    84. {0, 1, 1, 0},
    85. {1, 0, 0, 1}} ;
    86. v.clear();
    87. for(int i = 0 ; i < 4;i++)
    88. {
    89. QVector<int>v1;
    90. for(int j = 0 ; j < 4;j++)
    91. {
    92. v1.push_back(array6[i][j]);
    93. }
    94. v.push_back(v1);
    95. }
    96. mData.insert(6,v);
    97. int array7[4][4] = { {0, 1, 1, 1},
    98. {1, 0, 1, 1},
    99. {1, 1, 0, 1},
    100. {1, 1, 1, 0}} ;
    101. v.clear();
    102. for(int i = 0 ; i < 4;i++)
    103. {
    104. QVector<int>v1;
    105. for(int j = 0 ; j < 4;j++)
    106. {
    107. v1.push_back(array7[i][j]);
    108. }
    109. v.push_back(v1);
    110. }
    111. mData.insert(7,v);
    112. int array8[4][4] = { {0, 1, 0, 1},
    113. {1, 0, 0, 0},
    114. {0, 0, 0, 1},
    115. {1, 0, 1, 0}} ;
    116. v.clear();
    117. for(int i = 0 ; i < 4;i++)
    118. {
    119. QVector<int>v1;
    120. for(int j = 0 ; j < 4;j++)
    121. {
    122. v1.push_back(array8[i][j]);
    123. }
    124. v.push_back(v1);
    125. }
    126. mData.insert(8,v);
    127. int array9[4][4] = { {1, 0, 1, 0},
    128. {1, 0, 1, 0},
    129. {0, 0, 1, 0},
    130. {1, 0, 0, 1}} ;
    131. v.clear();
    132. for(int i = 0 ; i < 4;i++)
    133. {
    134. QVector<int>v1;
    135. for(int j = 0 ; j < 4;j++)
    136. {
    137. v1.push_back(array9[i][j]);
    138. }
    139. v.push_back(v1);
    140. }
    141. mData.insert(9,v);
    142. int array10[4][4] = { {1, 0, 1, 1},
    143. {1, 1, 0, 0},
    144. {0, 0, 1, 1},
    145. {1, 1, 0, 1}} ;
    146. v.clear();
    147. for(int i = 0 ; i < 4;i++)
    148. {
    149. QVector<int>v1;
    150. for(int j = 0 ; j < 4;j++)
    151. {
    152. v1.push_back(array10[i][j]);
    153. }
    154. v.push_back(v1);
    155. }
    156. mData.insert(10,v);
    157. int array11[4][4] = { {0, 1, 1, 0},
    158. {1, 0, 0, 1},
    159. {1, 0, 0, 1},
    160. {0, 1, 1, 0}} ;
    161. v.clear();
    162. for(int i = 0 ; i < 4;i++)
    163. {
    164. QVector<int>v1;
    165. for(int j = 0 ; j < 4;j++)
    166. {
    167. v1.push_back(array11[i][j]);
    168. }
    169. v.push_back(v1);
    170. }
    171. mData.insert(11,v);
    172. int array12[4][4] = { {0, 1, 1, 0},
    173. {0, 0, 0, 0},
    174. {1, 1, 1, 1},
    175. {0, 0, 0, 0}} ;
    176. v.clear();
    177. for(int i = 0 ; i < 4;i++)
    178. {
    179. QVector<int>v1;
    180. for(int j = 0 ; j < 4;j++)
    181. {
    182. v1.push_back(array12[i][j]);
    183. }
    184. v.push_back(v1);
    185. }
    186. mData.insert(12,v);
    187. int array13[4][4] = { {0, 1, 1, 0},
    188. {0, 0, 0, 0},
    189. {0, 0, 0, 0},
    190. {0, 1, 1, 0}} ;
    191. v.clear();
    192. for(int i = 0 ; i < 4;i++)
    193. {
    194. QVector<int>v1;
    195. for(int j = 0 ; j < 4;j++)
    196. {
    197. v1.push_back(array13[i][j]);
    198. }
    199. v.push_back(v1);
    200. }
    201. mData.insert(13,v);
    202. int array14[4][4] = { {1, 0, 1, 1},
    203. {0, 1, 0, 1},
    204. {1, 0, 1, 0},
    205. {1, 1, 0, 1}} ;
    206. v.clear();
    207. for(int i = 0 ; i < 4;i++)
    208. {
    209. QVector<int>v1;
    210. for(int j = 0 ; j < 4;j++)
    211. {
    212. v1.push_back(array14[i][j]);
    213. }
    214. v.push_back(v1);
    215. }
    216. mData.insert(14,v);
    217. int array15[4][4] = { {0, 1, 0, 1},
    218. {1, 0, 0, 0},
    219. {1, 0, 0, 0},
    220. {0, 1, 0, 1}} ;
    221. v.clear();
    222. for(int i = 0 ; i < 4;i++)
    223. {
    224. QVector<int>v1;
    225. for(int j = 0 ; j < 4;j++)
    226. {
    227. v1.push_back(array15[i][j]);
    228. }
    229. v.push_back(v1);
    230. }
    231. mData.insert(15,v);
    232. int array16[4][4] = { {0, 1, 1, 0},
    233. {1, 1, 1, 1},
    234. {1, 1, 1, 1},
    235. {0, 1, 1, 0}} ;
    236. v.clear();
    237. for(int i = 0 ; i < 4;i++)
    238. {
    239. QVector<int>v1;
    240. for(int j = 0 ; j < 4;j++)
    241. {
    242. v1.push_back(array16[i][j]);
    243. }
    244. v.push_back(v1);
    245. }
    246. mData.insert(16,v);
    247. int array17[4][4] = { {0, 1, 1, 1},
    248. {0, 1, 0, 0},
    249. {0, 0, 1, 0},
    250. {1, 1, 1, 0}} ;
    251. v.clear();
    252. for(int i = 0 ; i < 4;i++)
    253. {
    254. QVector<int>v1;
    255. for(int j = 0 ; j < 4;j++)
    256. {
    257. v1.push_back(array17[i][j]);
    258. }
    259. v.push_back(v1);
    260. }
    261. mData.insert(17,v);
    262. int array18[4][4] = { {0, 0, 0, 1},
    263. {0, 0, 1, 0},
    264. {0, 1, 0, 0},
    265. {1, 0, 0, 0}} ;
    266. v.clear();
    267. for(int i = 0 ; i < 4;i++)
    268. {
    269. QVector<int>v1;
    270. for(int j = 0 ; j < 4;j++)
    271. {
    272. v1.push_back(array18[i][j]);
    273. }
    274. v.push_back(v1);
    275. }
    276. mData.insert(18,v);
    277. int array19[4][4] = { {0, 1, 0, 0},
    278. {0, 1, 1, 0},
    279. {0, 0, 1, 1},
    280. {0, 0, 0, 0}} ;
    281. v.clear();
    282. for(int i = 0 ; i < 4;i++)
    283. {
    284. QVector<int>v1;
    285. for(int j = 0 ; j < 4;j++)
    286. {
    287. v1.push_back(array19[i][j]);
    288. }
    289. v.push_back(v1);
    290. }
    291. mData.insert(19,v);
    292. int array20[4][4] = { {0, 0, 0, 0},
    293. {0, 0, 0, 0},
    294. {0, 0, 0, 0},
    295. {0, 0, 0, 0}} ;
    296. v.clear();
    297. for(int i = 0 ; i < 4;i++)
    298. {
    299. QVector<int>v1;
    300. for(int j = 0 ; j < 4;j++)
    301. {
    302. v1.push_back(array20[i][j]);
    303. }
    304. v.push_back(v1);
    305. }
    306. mData.insert(20,v);
    307. }

    10.mainsence.cpp

    1. #include "mainsence.h"
    2. #include "ui_mainsence.h"
    3. #include <QPushButton>
    4. #include "mypushbutton.h"
    5. #include <chooselevelscene.h>
    6. #include <QTimer>
    7. #include <QSound>
    8. Mainsence::Mainsence(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::Mainsence)
    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->action_quit,&QAction::triggered,[=](){
    22. this->close();
    23. });
    24. //准备开始的音效
    25. QSound * startSound = new QSound(":/res/TapButtonSound.wav",this);
    26. //startSound->setLoops(-1); // 这个函数是播放几次 -1 是无限的播放
    27. //开始的按钮
    28. mypushbutton * startBtn = new mypushbutton(":/res/MenuSceneStartButton.png");
    29. startBtn->setParent(this);
    30. startBtn->move((this->width() *0.5) - (startBtn->width()*0.5),this->height()*0.7);
    31. //实列化选择关卡场景
    32. chooseScene =new ChooseLevelScene;
    33. //返回按钮的信号连接
    34. connect(chooseScene,&ChooseLevelScene::chooseSceneBack,this,[=](){
    35. this->setGeometry(chooseScene->geometry());//保证两个窗口出现的位置相同
    36. chooseScene->hide();//选择关卡场景 隐藏
    37. this->show();//重新显示主场景
    38. });
    39. connect(startBtn,&mypushbutton::clicked,[=](){
    40. //播放点击了开始的音效
    41. startSound->play();
    42. qDebug()<<"点击了开始";
    43. //弹起特效
    44. startBtn->zoom1();
    45. startBtn->zoom2();
    46. //延时进入到选择关卡场景中
    47. QTimer::singleShot(500,this,[=](){
    48. //设置chooseScene 场景的位置
    49. chooseScene->setGeometry(this->geometry());//这个函数的作用就是把 新的这个窗口放到 之前要隐藏的窗口的位置,防止返回的时候,两个窗口位置的不同
    50. //自身隐藏
    51. this->hide();
    52. //进入选择关卡场景中
    53. //显示选择关卡
    54. chooseScene->show();
    55. //监听选择关卡场景的返回按钮的信号
    56. });
    57. });
    58. }
    59. Mainsence::~Mainsence()
    60. {
    61. delete ui;
    62. }
    63. //重新paintEvent 事件, 画背景图
    64. void Mainsence::paintEvent(QPaintEvent *)
    65. {
    66. QPainter painter(this);//设置一个画家的对象
    67. QPixmap pix;//定义一个图片的对象
    68. pix.load(":res/PlayLevelSceneBg.png");//加载图片
    69. //第一个参数 第二个参数 位置开始的位置 ,参数三 画家对象窗口的宽 ,参数四 画家对象窗口的高 参数五,要加载的图片
    70. painter.drawPixmap(0,0,this->width(),this->height(),pix);//窗口设置图片 这个函数可以使图片的大小适应窗口的大小
    71. //画背景图标
    72. pix.load(":/res/Title.png");//再次加载一个图片
    73. pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);//得到一个缩放的图片
    74. painter.drawPixmap(10,30,pix);//再次放置图片 开始的位置是10,30
    75. }

    11.mycoin.cpp

    1. #include "mycoin.h"
    2. #include <QDebug>
    3. //MyCoin::MyCoin(QWidget *parent) : QWidget(parent)
    4. //{
    5. //}
    6. MyCoin::MyCoin(QString btnImg)
    7. {
    8. QPixmap pix;//建立一个图片的对象
    9. bool ret=pix.load(btnImg);//加载图片
    10. if(!ret)//判定图片是否加载成功
    11. {
    12. QString str = QString("图片 %1 加载失败").arg(btnImg);
    13. qDebug()<<str;//输出加载失败的 图片
    14. return;//结束函数
    15. }
    16. this->setFixedSize(pix.width(),pix.height());//设置 金币按钮的大小
    17. this->setStyleSheet("QPushButton{border:Opx}");//设置为不规则图像的过滤
    18. this->setIcon(pix);//加载图片,给这个按钮
    19. this->setIconSize(QSize(pix.width(),pix.height()));//图片大小的设置
    20. //初始化定时器对象
    21. timer1 = new QTimer(this);
    22. timer2 = new QTimer(this);
    23. //定时器的启动信号的连接
    24. //监听正面的金币的点击 信号
    25. connect(timer1,&QTimer::timeout,[=](){
    26. QPixmap pix;//建立一个图片的对象
    27. QString str1 = QString(":/res/Coin000%1").arg(this->min++);
    28. bool ret1=pix.load(str1);
    29. if(!ret1)
    30. {
    31. qDebug()<<"错误 金币正面翻转 "<<str1;
    32. }
    33. this->setFixedSize(pix.width(),pix.height());//设置 金币按钮的大小
    34. this->setStyleSheet("QPushButton{border:Opx}");//设置为不规则图像的过滤
    35. this->setIcon(pix);//加载图片,给这个按钮
    36. this->setIconSize(QSize(pix.width(),pix.height()));//图片大小的设置
    37. //判断 如果翻完了 将min 重置为 1;
    38. if(this->min > this->max)
    39. {
    40. this->min=1;
    41. timer1->stop();//定时器发出信号停止
    42. isAnimation=false;//停止做动画了
    43. }
    44. });
    45. //监听反面的金币的点击 信号
    46. connect(timer2,&QTimer::timeout,[=](){
    47. QPixmap pix;//建立一个图片的对象
    48. QString str1 = QString(":/res/Coin000%1").arg(this->max--);
    49. bool ret1=pix.load(str1);
    50. if(!ret1)
    51. {
    52. qDebug()<<"错误 金币正面翻转 "<<str1;
    53. }
    54. this->setFixedSize(pix.width(),pix.height());//设置 金币按钮的大小
    55. this->setStyleSheet("QPushButton{border:Opx}");//设置为不规则图像的过滤
    56. this->setIcon(pix);//加载图片,给这个按钮
    57. this->setIconSize(QSize(pix.width(),pix.height()));//图片大小的设置
    58. //判断 如果翻完了 将min 重置为 1;
    59. if(this->max < this->min)
    60. {
    61. this->max=8;
    62. timer2->stop();//定时器发出信号停止
    63. isAnimation=false;//停止做动画了
    64. }
    65. });
    66. }
    67. void MyCoin::changeFlag()//改变金币的状态
    68. {
    69. //如果是正面 翻成反面
    70. if(this->flag)
    71. {
    72. //开始运行正面翻反面的定时器
    73. timer1->start(30);//启动定时器 时间间隔为30毫秒
    74. this->flag=false;//把金币的状态改变
    75. isAnimation=true;//开始做动画了
    76. }
    77. else
    78. {
    79. //开始运行反面翻正面的定时器
    80. timer2->start(30);//启动定时器 时间间隔为30毫秒
    81. this->flag=true;//把金币的状态改变
    82. isAnimation=true;//开始做动画了
    83. }
    84. }
    85. //重写 鼠标按下的事件
    86. void MyCoin::mousePressEvent(QMouseEvent * e)
    87. {
    88. if(this->isAnimation || this->ispaly== false)//当他翻转动作正在执行的时候,我们点击按钮 ,使按钮失效
    89. {
    90. return;
    91. }
    92. else//没有进行翻转动作的时候,我们把这个信号交给父类处理
    93. {
    94. QPushButton::mousePressEvent(e);
    95. }
    96. }

    12.mypushbutton.cpp

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

    13.playscene.cpp

    1. #include "playscene.h"
    2. #include <QDebug>
    3. #include <QMenuBar>
    4. #include <QPainter>
    5. #include "mypushbutton.h"
    6. #include <QTimer>
    7. #include <QLabel>
    8. #include "mycoin.h"
    9. #include "dataconfig.h"
    10. #include <QPropertyAnimation>
    11. #include <QSound>
    12. //PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
    13. //{
    14. //}
    15. PlayScene::PlayScene(int levelNum)//构造函数
    16. {
    17. QString str=QString("进入了第 %1 关").arg(levelNum);//进入关卡的体术语句
    18. qDebug()<<str;//输出提示语句
    19. this->levelIndex=levelNum;//内部成员赋值
    20. //初始化游戏的场景
    21. //设置固定大小
    22. this->setFixedSize(320,588);
    23. //设置图标
    24. this->setWindowIcon(QPixmap(":/res/Coin0001.png"));//就是最上面的窗口名称旁边的图标
    25. //设置标题
    26. this->setWindowTitle("翻金币场景");
    27. //创建菜单栏
    28. QMenuBar * bar = menuBar();//创建一个菜单栏的对象,这个对象是空的
    29. setMenuBar(bar);//把菜单栏对象,放置到窗口
    30. //创建开始菜单
    31. QMenu * startMenu = bar->addMenu("开始");
    32. //创建退出 菜单选项
    33. QAction * quitAction = startMenu->addAction("退出");
    34. //点击退出 实现退出游戏
    35. connect(quitAction,&QAction::triggered,[=](){
    36. this->close();
    37. });
    38. //添加音效
    39. //返回按钮的音效
    40. QSound * backSound = new QSound(":/res/TapButtonSound.wav",this);
    41. //翻金币的音效
    42. QSound * flipSound = new QSound(":/res/ConFlipSound.wav",this);
    43. //成功胜利的音效
    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. //告诉主场景,我返回了,主场景监听 chooseLeveScene 的返回按键
    55. //延时发送信号
    56. QTimer::singleShot(500,this,[=](){
    57. emit this->chooseSceneBack();//激发信号 返回开始界面
    58. });
    59. });
    60. //显示当前关卡数
    61. QLabel * label = new QLabel;
    62. label->setParent(this);//设置父亲
    63. QFont font;//设置字体的对象
    64. font.setFamily("华文新魏");//字体的格式
    65. font.setPointSize(20);//字体大小为 20
    66. //输出的标签显示的内容
    67. QString str1=QString("Level: %1").arg(this->levelIndex);
    68. //将字体的设置加入标签空间之中
    69. label->setFont(font);
    70. label->setText(str1);//设置关卡的标签显示的内容
    71. //label->setFixedSize(20,20);//设置标签的大小
    72. //label->move(100,500);//设置标签的显示的位置在哪里
    73. label->setGeometry(30,this->height() -50 ,150,50);//这个上面 两个命令的合并的功能
    74. dataconfig config;//第一个对象 获取关卡的数据
    75. //初始化每个关卡的二维数组
    76. for(int i=0;i<4;i++)
    77. {
    78. for(int j=0;j<4;j++)
    79. {
    80. this->gameArray[i][j]=config.mData[this->levelIndex][i][j];//赋值
    81. }
    82. }
    83. //胜利的图片显示,
    84. //开始的时候我们把他放到屏幕之外 等到胜利之后我们直接让他掉下来
    85. QLabel * winLabel = new QLabel;
    86. QPixmap tmaPix;
    87. tmaPix.load(":/res/LevelCompletedDialogBg.png");//加载图片
    88. winLabel->setGeometry(0,0,tmaPix.width(),tmaPix.height());//设置标签里面放置的位置
    89. winLabel->setPixmap(tmaPix);//把图片放入到标签
    90. winLabel->setParent(this);//设置标签的父亲
    91. winLabel->move((this->width() - tmaPix.width())*0.5,-tmaPix.height());//设置标签在窗口的位置
    92. //显示金币背景图案 灰色的方形框框
    93. for(int i=0;i<4;i++)
    94. {
    95. for(int j=0;j<4;j++)
    96. {
    97. //绘制背景图片
    98. QPixmap pix = QPixmap(":/res/BoardNode(1).png");//加载图片
    99. QLabel * label = new QLabel;//新建一个标签
    100. label->setGeometry(0,0,pix.width(),pix.height());//设置标签里面放置的位置
    101. label->setPixmap(pix);//把图片放入到标签
    102. label->setParent(this);//设置标签的父亲
    103. label->move(57+i*50,200+j*50);//设置标签在窗口的位置
    104. //创建金币
    105. if(this->gameArray[i][j]==1)
    106. {
    107. //显示金币
    108. str=":/res/Coin0001.png";
    109. }
    110. else
    111. {
    112. //显示银币
    113. str=":/res/Coin0008.png";
    114. }
    115. MyCoin *coin = new MyCoin(str);//建立一个金币的对象,并且在这个里面加载图片
    116. coin->setParent(this);//设置父亲,对于这个按钮 父亲为游戏显示场景
    117. coin->move(59+ i*50, 204 + j*50);//移动金币的位置,与灰色的框框对齐
    118. //给金币的属性赋值
    119. coin->posX=i;//x 坐标
    120. coin->posY=j;//y 坐标
    121. coin->flag=this->gameArray[i][j];//正面是 1 反面是 0
    122. //将金币放到 金币的二维数组里 以便后期的维护
    123. coinBtn[i][j]=coin;//
    124. //点击金币 进行翻转
    125. //他的本质 继承的是 QPushButton
    126. //监听点击信号
    127. connect(coin,&MyCoin::clicked,[=](){
    128. //翻金币的音效
    129. flipSound->play();
    130. coin->changeFlag();//点击之后进行翻转
    131. this->gameArray[i][j]=(this->gameArray[i][j]==0 ? 1:0);
    132. //加一个延时的翻转
    133. QTimer::singleShot(100,this,[=](){
    134. //翻转周围的金币
    135. //周围的右侧金币翻转的条件
    136. if(coin->posX+1 <=3)
    137. {
    138. coinBtn[coin->posX+1][coin->posY]->changeFlag();
    139. this->gameArray[coin->posX+1][coin->posY] = (this->gameArray[coin->posX+1][coin->posY]==0 ? 1:0);
    140. }
    141. //周围的左侧金币翻转的条件
    142. if(coin->posX-1 >=0)
    143. {
    144. coinBtn[coin->posX-1][coin->posY]->changeFlag();
    145. this->gameArray[coin->posX-1][coin->posY] = (this->gameArray[coin->posX-1][coin->posY]==0 ? 1:0);
    146. }
    147. //周围的上侧金币翻转的条件
    148. if(coin->posY+1 <=3)
    149. {
    150. coinBtn[coin->posX][coin->posY+1]->changeFlag();
    151. this->gameArray[coin->posX][coin->posY+1] = (this->gameArray[coin->posX][coin->posY+1]==0 ? 1:0);
    152. }
    153. //周围的下侧金币翻转的条件
    154. if(coin->posY-1 >=0)
    155. {
    156. coinBtn[coin->posX][coin->posY-1]->changeFlag();
    157. this->gameArray[coin->posX][coin->posY-1] = (this->gameArray[coin->posX][coin->posY-1]==0 ? 1:0);
    158. }
    159. //判断是否胜利
    160. this->isWin=true;
    161. for(int i=0;i<4;i++)
    162. {
    163. for(int j=0;j<4;j++)
    164. {
    165. if(coinBtn[i][j]->flag==false)
    166. {
    167. this->isWin=false;
    168. break;
    169. }
    170. }
    171. }
    172. if(this->isWin==true)//胜利了
    173. {
    174. //游戏胜利的音效
    175. winSound->play();
    176. qDebug()<<"第"<<levelNum<<"关 "<<"游戏胜利了";
    177. //把所有的金币按钮禁用
    178. for(int i=0;i<4;i++)
    179. {
    180. for(int j=0;j<4;j++)
    181. {
    182. coinBtn[i][j]->ispaly=false;//金币按钮不可以使用
    183. }
    184. }
    185. //创建一个动态对象
    186. QPropertyAnimation * animation1 =new QPropertyAnimation(winLabel,"geometry");
    187. //设置动画的事件间隔
    188. animation1->setDuration(1000);
    189. //起始位置
    190. animation1->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
    191. //结束位置
    192. animation1->setEndValue(QRect(winLabel->x(),winLabel->y()+144,winLabel->width(),winLabel->height()));
    193. //设置弹跳曲线
    194. animation1->setEasingCurve(QEasingCurve::OutBounce);
    195. //执行动画
    196. animation1->start();
    197. }
    198. });
    199. });
    200. }
    201. }
    202. }
    203. //QWidget中的paintEvent事件处理器可以在子类中被重写来接收绘图事件,然后在指定区域完成图形的绘制。
    204. //重写paintEvent事件
    205. void PlayScene::paintEvent(QPaintEvent * )
    206. {
    207. QPainter painter(this);//设置一个画家的对象
    208. QPixmap pix;//定义一个图片的对象
    209. pix.load(":res/PlayLevelSceneBg.png");//加载图片
    210. //第一个参数 第二个参数 位置开始的位置 ,参数三 画家对象窗口的宽 ,参数四 画家对象窗口的高 参数五,要加载的图片
    211. painter.drawPixmap(0,0,this->width(),this->height(),pix);//窗口设置图片 这个函数可以使图片的大小适应窗口的大小
    212. //画背景图标
    213. pix.load(":/res/Title.png");//再次加载一个图片
    214. pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);//得到一个缩放的图片
    215. painter.drawPixmap(10,30,pix);//再次放置图片 开始的位置是10,30
    216. }

  • 相关阅读:
    数据库与Socket学习
    SparkCore系列-8、RDD 读写外部数据源
    TreeMap和LinkedHashMap
    ThreadLocal的两种典型应用场景
    第三章、运输层
    胆固醇-聚乙二醇-荧光素 Cholesterol-PEG-FITC Fluorescein-PEG-CLS概述
    PDA 红外扫码 uniapp
    IMMA~~
    MLC-LLM 部署RWKV World系列模型实战(3B模型Mac M2解码可达26tokens/s)
    模块及分类
  • 原文地址:https://blog.csdn.net/she666666/article/details/127038221