-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelscene.cpp
106 lines (97 loc) · 3.85 KB
/
levelscene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*************************************************************************
> File Name: levelscene.cpp
> Author: luochenhao
> Mail: [email protected]
> Created Time: 2023-08-11 17:26:25
************************************************************************/
#include "levelscene.h"
#include "mainscene.h"
#include "mypushbutton.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include <QString>
#include <QLabel>
#include <QSound>
LevelScene::LevelScene(QWidget *parent):QMainWindow(parent) {
//1.设置level窗口基本参数
setFixedSize(400, 700);
setWindowIcon(QIcon(":/res/img/Coin0001.png"));
setWindowTitle("CoinFlip-levelchoose");
QMenuBar *mbar = menuBar();
setMenuBar(mbar);
QMenu *startMenu = mbar->addMenu("start");
QAction *quitAction = startMenu->addAction("quit");
//加载音效资源
QSound *clickSound = new QSound(":/res/music/tap.wav", this);
QSound *backSound = new QSound(":/res/music/back.wav", this);
//2.quit按钮功能实现
connect(quitAction, &QAction::triggered, this, [=](){
this->close();
});
//3.加载返回按钮
QString normalImagePath = ":/res/img/BackButton.png";
QString pressImagePath = ":/res/img/BackButtonSelected.png";
MyPushButton *backBtn = new MyPushButton(this, normalImagePath, pressImagePath);
backBtn->move(this->width() - backBtn->width(), this->height() - backBtn->height());
//back按钮功能实现
connect(backBtn, &MyPushButton::clicked, this, [=](){
qDebug() << "debug: player choosed to return to the start page.";
backSound->play();
QTimer::singleShot(200, this, [=](){
emit this->levelSceneClose();//向mainsecene发送信息
});
});
//4.加载关卡选择按钮
for (int i = 0; i < 20; ++i) {
//关卡图片
MyPushButton *levelBtn = new MyPushButton(this, ":/res/img/LevelIcon.png");
levelBtn->move(60 + i%4 * 70, 200 + i/4 * 70);
//关卡数字
QLabel *label = new QLabel(this);
label->setFixedSize(levelBtn->width(), levelBtn->height());
label->setText(QString::number(1 + i));
label->move(60 + i%4 * 70, 200 + i/4 * 70);
label->setAlignment(Qt::AlignCenter);//label中的数字居中
label->setAttribute(Qt::WA_TransparentForMouseEvents);//鼠标点击穿过label标签
//5.监听每个level按钮的点击事件 准备场景跳转
connect(levelBtn, &QPushButton::clicked, this, [=](){
QString str = QString("debug: player choosed level %1.").arg(i + 1);
qDebug() << str;
levelBtn->sink();
levelBtn->jump();
clickSound->play();
MainScene::_musicPlayer->pause();
if (_gamescene == nullptr) {
//游戏场景的创建需要放到触发函数中
_gamescene = new GameScene(i + 1);
_gamescene->setGeometry(this->geometry());
this->hide();
_gamescene->show();
}
//6.开始监听来自gamescene的信号
connect(_gamescene, &GameScene::gameSceneClose, this, [=](){
qDebug() << "debug: receive signal form _gamescene.";
this->setGeometry(_gamescene->geometry());
this->show();
delete _gamescene;
_gamescene = nullptr;
});
});
}
}
//绘制背景图片
void LevelScene::paintEvent(QPaintEvent *event) {
QPainter painter(this);//当前窗口内绘制
//1.加载背景
QPixmap bground;
bground.load(":/res/img/OtherSceneBg.png");
painter.drawPixmap(0, 0, this->width(), this->height(), bground);
//2.加载背景装饰性图标
QPixmap dec1;
dec1.load(":/res/img/Title.png");
painter.drawPixmap(20, 50, dec1);
}