-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamewindow.cpp
184 lines (174 loc) · 4.65 KB
/
gamewindow.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "gamewindow.h"
#include "ui_gamewindow.h"
#include"gamescene.h"
#include"QTimer"
#include<QDebug>
#include<QMediaPlayer>
#include<QMediaPlaylist>
//构造函数列表初始化
GameWindow::GameWindow(QWidget *parent) :
QWidget(parent),
timer(new QTimer(this)),
scene(new GameScene(this)),
gameLevel(0),
gameScore(0),
maxGameScore(0),
gameLife(3),
ui(new Ui::GameWindow),
backgroundPlayer(new QMediaPlayer(this)),
gameWinPlayer(new QMediaPlayer(this)),
gameLosePlayer(new QMediaPlayer(this))
{
ui->setupUi(this);
ui->GameView->setScene(scene);
timer->setInterval(1000);
this->setFixedSize(this->size());
this->renewGame();
//调色板控制颜色
QPalette palette;
palette.setBrush(this->backgroundRole(),QBrush(QImage(":/background/image/background/background.png").scaled(this->size())));
this->setPalette(palette);
ui->GameView->setStyleSheet("background: transparent;border:0px");
//音乐播放
auto list=new QMediaPlaylist(this);
list->addMedia(QUrl("qrc:/music/background.mp3"));
list->setPlaybackMode(QMediaPlaylist::Loop);
backgroundPlayer->setPlaylist(list);
backgroundPlayer->setVolume(8);
backgroundPlayer->play();
gameWinPlayer->setMedia(QUrl("qrc:/music/gameWin.mp3"));
gameLosePlayer->setMedia(QUrl("qrc:/music/gameLose.mp3"));
//连接信号和槽
//定时器时间到,出现奖品
connect(timer,SIGNAL(timeout()),this,SLOT(setAward()));
connect(ui->backgroundCheckBox,SIGNAL(clicked(bool)),this,SLOT(backgroundControler(bool)));
//控制音量的单选框
connect(ui->backgroundCheckBox,SIGNAL(clicked(bool)),this,SLOT(setGameWindowFocus()));
}
GameWindow::~GameWindow()
{
delete ui;
}
//游戏胜利
void GameWindow::gameWin()
{
//中断信号和槽的联系
this->disconnect(scene,SIGNAL(gameWin()),this,SLOT(gameWin()));
this->disconnect(scene,SIGNAL(gameLose()),this,SLOT(gameOver()));
this->disconnect(scene,SIGNAL(upScore(int)),this,SLOT(updataScore(int)));
this->disconnect(scene,SIGNAL(gameContinue()),this->timer,SLOT(start()));
this->disconnect(scene,SIGNAL(gamePause()),this->timer,SLOT(stop()));
//增加游戏等级
gameLevel++;
gameWinPlayer->play();
this->startLevel();
}
void GameWindow::gameOver()
{
//游戏失败
this->disconnect(scene,SIGNAL(gameWin()),this,SLOT(gameWin()));
this->disconnect(scene,SIGNAL(gameLose()),this,SLOT(gameOver()));
this->disconnect(scene,SIGNAL(upScore(int)),this,SLOT(updataScore(int)));
this->disconnect(scene,SIGNAL(gameContinue()),this->timer,SLOT(start()));
this->disconnect(scene,SIGNAL(gamePause()),this->timer,SLOT(stop()));
gameLosePlayer->play();
switch (gameLife--)
{
case 0:
this->renewGame();
break;
case 1:
ui->life1->hide();
this->startLevel();
break;
case 2:
ui->life2->hide();
this->startLevel();
break;
case 3:
ui->life3->hide();
this->startLevel();
break;
}
}
//分数
void GameWindow::updataScore(int upScore)
{
//QT字符串
this->gameScore+=upScore;
ui->score->setText(QString("得分:\n"
" %1").arg(gameScore));
if(this->gameScore>maxGameScore)
{
maxGameScore=gameScore;
ui->maxScore->setText(QString("最高得分:\n"
" %1").arg(gameScore));
}
}
void GameWindow::updataLevel()
{
//关卡
ui->level->setText(QString(" 第 %1 关").arg(gameLevel));
}
//重新开始
void GameWindow::renewGame()
{
this->gameLevel=1;
this->gameScore=0;
this->gameLife=3;
ui->life1->show();
ui->life2->show();
ui->life3->show();
this->startLevel();
}
//奖品
void GameWindow::setAward()
{
auto nextVal=ui->awardProgressBar->value()+sqrt(gameLevel)+1;
if(nextVal>=100)
{
nextVal=0;
this->scene->randSetGift();
}
ui->awardProgressBar->setValue(nextVal);
}
//开始游戏
void GameWindow::startLevel()
{
//把UI界面的奖励进度条重置
ui->awardProgressBar->setValue(0);
updataLevel();
//分数为0
updataScore(0);
//定时停止
this->timer->stop();
//建立新场景
scene->deleteLater();
scene=new GameScene(this);
scene->selectGameLevel(gameLevel);
ui->GameView->setScene(scene);
//连接信号和槽
this->connect(scene,SIGNAL(gameWin()),this,SLOT(gameWin()));
this->connect(scene,SIGNAL(gameLose()),this,SLOT(gameOver()));
this->connect(scene,SIGNAL(upScore(int)),this,SLOT(updataScore(int)));
this->connect(scene,SIGNAL(gamePause()),this->timer,SLOT(stop()));
this->connect(scene,SIGNAL(gameContinue()),this->timer,SLOT(start()));
this->connect(scene,SIGNAL(gameContinue()),this->timer,SLOT(start()));
this->connect(scene,SIGNAL(gamePause()),this->timer,SLOT(stop()));
}
//控制背景音乐的复选框
void GameWindow::backgroundControler(bool on)
{
if(on)
{
backgroundPlayer->play();
}
else
{
backgroundPlayer->stop();
}
}
void GameWindow::setGameWindowFocus()
{
ui->GameView->setFocus();
}