-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.cpp
352 lines (309 loc) · 11.9 KB
/
widget.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "widget.h"
#include "ui_widget.h"
#include<QMessageBox>
#include <QColorDialog>
#include <QFile>
#include <QTextStream>
#include <QSound>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
gamewid=new gamewidget;
gamewid->setAttribute(Qt::WA_DeleteOnClose); //设置窗口关闭时自动删除
gamewid->showMaximized();//最大化显示
this->gameArea=new GameArea(gamewid);
this->timer=new QTimer(gamewid);
connect(this->timer,SIGNAL(timeout()),this,SLOT(timer_upDate()));
this->score=0;
this->doHightestScore();//显示最高分
this->gameArea->show();
this->gameArea->init_Game();//进入游戏初始化
this->gameArea->gameStart();
on_comboBox_activated(0);//颜色常规模式初始化
this->ui->horizontalSlider->setValue(100);//弱视程度初始化
on_horizontalSlider_actionTriggered(100);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timer_upDate(){//定时器溢出处理
if(this->gameArea->is_stop)this->ui->pushButton_2->click();
this->gameArea->moveOneStep();
if(this->gameArea->isMoveEnd()){
if(this->gameArea->isGame_Over()){
this->timer->stop();
QMessageBox::warning(this,tr("warning"),tr("Game Over!"),QMessageBox::Yes);
this->doHightestScore();//保存显示最高分
this->score=0;
this->gameArea->init_Game();
this->gameArea->gameStart();
int num=0;//清空分数
this->doScore(num);
gamewid->score_change();
}
else{
this->gameArea->nextItem();
int num=this->gameArea->getFullRowNum();
this->doScore(num);
gamewid->score_change();
this->gameArea->gameStart();
}
}
else{
this->gameArea->do_MoveNext();
}
}
void Widget::doScore(int num){//显示分数
score +=num*100*800/(this->speed);
gamewid->a=score;
}
void Widget::doHightestScore(){//保存显示最高分数
QFile file("hightest_score.txt");
file.open(QIODevice::ReadOnly);
QByteArray t = file.readAll();
file.close();
int hightest_score=t.toInt();
QString strText;
strText = tr("%1").arg(hightest_score);
if (hightest_score<score){
hightest_score=score;
strText = tr("%1").arg(score);//显示最高分数
file.open(QIODevice::WriteOnly);
file.write(strText.toUtf8());
}
ui->label_2->setText(strText.split("", QString::SkipEmptyParts).join("\n"));//竖排显示分数
ui->label_2->setAlignment(Qt::AlignCenter);
file.close();
}
void Widget::on_pushButton_clicked()//开始游戏按钮
{
this->gameArea->is_stop=false;//改变游戏暂停指示变量
this->timer->start(this->speed);//开启定时器
this->gameArea->setFocus();//让游戏区获得焦点,才能响应键盘
}
void Widget::on_pushButton_2_clicked()//暂停按钮
{
if(this->ui->pushButton_2->isChecked()){
this->timer->stop();
this->gameArea->is_stop=true;//改变游戏暂停指示变量
this->ui->pushButton_2->setText(tr("取消暂停"));
}
else{
this->timer->start(this->speed);
this->gameArea->is_stop=false;
this->ui->pushButton_2->setText(tr("暂停游戏"));
}
}
void Widget::on_pushButton_3_clicked()//重新开始
{
this->gameArea->is_stop=true;
this->timer->stop();
if(gamewid->b){
gamewid=new gamewidget;
gamewid->setAttribute(Qt::WA_DeleteOnClose);
gamewid->show();
this->gameArea=new GameArea(gamewid);
this->timer=new QTimer(gamewid);
connect(this->timer,SIGNAL(timeout()),this,SLOT(timer_upDate()));
}
this->score=0;
this->gameArea->show();
this->gameArea->init_Game();//进入游戏初始化
this->gameArea->gameStart();
int num=0;//清空分数
this->doScore(num);
gamewid->score_change();
on_comboBox_activated(0);//颜色常规模式初始化
}
void Widget::on_pushButton_4_clicked()//设置背景颜色
{
QColor color = QColorDialog::getColor(Qt::black, this, "Color dialog");
if(this->leftright==0)this->gameArea->setGameAreaColor(color);
else if (this->leftright==1)this->gameArea->setGameAreaColor1(color);
this->gameArea->draw_gameArea();
this->gameArea->setFocus();
}
void Widget::on_pushButton_5_clicked()//设置方块颜色
{
QColor color = QColorDialog::getColor(Qt::green, this, "Color dialog");
if(this->leftright==0)
{
this->gameArea->setBoxBrushColor(color);
gamewid->boxBrushColor=color;
}
else if (this->leftright==1)
{
this->gameArea->setBoxBrushColor1(color);
gamewid->boxBrushColor1=color;
}
this->gameArea->draw_gameArea();
this->gameArea->setFocus();
gamewid->score_change();
}
void Widget::on_pushButton_6_clicked()//设置方块边框颜色
{
QColor color = QColorDialog::getColor(Qt::black, this, "Color dialog");
if(this->leftright==0)this->gameArea->setBoxPenColor(color);
else if (this->leftright==1)this->gameArea->setBoxPenColor1(color);
this->gameArea->draw_gameArea();
this->gameArea->setFocus();
}
void Widget::on_pushButton_7_clicked()//锁定颜色设置按钮
{
if(this->ui->pushButton_7->isChecked())
{
ui->pushButton_4->setEnabled(false);
ui->pushButton_5->setEnabled(false);
ui->pushButton_6->setEnabled(false);
ui->comboBox->setEnabled(false);
ui->comboBox_2->setEnabled(false);
ui->comboBox_3->setEnabled(false);
ui->horizontalSlider->setEnabled(false);
this->ui->pushButton_7->setText(tr("取消锁定"));
}
else{
ui->pushButton_4->setEnabled(true);
ui->pushButton_5->setEnabled(true);
ui->pushButton_6->setEnabled(true);
ui->comboBox->setEnabled(true);
ui->comboBox_2->setEnabled(true);
ui->comboBox_3->setEnabled(true);
ui->horizontalSlider->setEnabled(true);
this->ui->pushButton_7->setText(tr("锁定按钮"));
}
}
void Widget::on_pushButton_8_clicked()//方块提示按钮
{
if(this->ui->pushButton_8->isChecked())
{
this->gameArea->setDrawNextItem(false);
}
else
{
this->gameArea->setDrawNextItem(true);
}
this->gameArea->draw_gameArea();
this->gameArea->setFocus();
}
QSound *sound=new QSound("./voice/background.wav");
void Widget::on_pushButton_9_clicked()//声音开关按钮
{
if(this->ui->pushButton_9->isChecked())
{
this->gameArea->setPlaySound_itemChange("./voice/changeItem.wav",true);
this->gameArea->setPlaySound_moveDown("./voice/moveDown.wav",true);
this->gameArea->setPlaySound_moveLeft("./voice/moveLeft.wav",true);
this->gameArea->setPlaySound_moveRight("./voice/moveLeft.wav",true);
this->gameArea->setPlaySound_clear("./voice/clear.wav",true);
sound->play();
sound->setLoops(1000);
this->ui->pushButton_9->setText(tr("关闭声音"));
}
else
{
sound->stop();
this->gameArea->setPlaySound(false); //关闭音乐
this->ui->pushButton_9->setText(tr("打开声音"));
}
}
void Widget::on_pushButton_10_clicked()//是否坠落按钮
{
if(this->ui->pushButton_10->isChecked())
{
this->gameArea->setKey_Down_Move_oneStep(true); //按一下向下方向键,下移一步
}
else
{
this->gameArea->setKey_Down_Move_oneStep(false); //按一下向下方向键,移动到底
}
this->gameArea->setFocus();
}
void Widget::on_comboBox_activated(int index)//选定弱视眼
{
if(ui->comboBox->currentIndex()==0) {this->leftright=0;this->gameArea->left_right=0;}//选定左眼,调节左区域
else if(ui->comboBox->currentIndex()==1){this->leftright=1;this->gameArea->left_right=1;}//选定右眼,调节右区域
QColor color =QColor(0,0,0,0);
if(ui->comboBox_2->currentIndex()==0&&ui->comboBox->currentIndex()==1){
this->gameArea->setBoxBrushColor(color);
this->gameArea->setBoxPenColor(color);
this->gameArea->setBoxBrushColor1(this->gameArea->green_change);
this->gameArea->setBoxPenColor1(Qt::black);
}
if(ui->comboBox_2->currentIndex()==0&&ui->comboBox->currentIndex()==0){
this->gameArea->setBoxBrushColor1(color);
this->gameArea->setBoxPenColor1(color);
this->gameArea->setBoxBrushColor(this->gameArea->green_change);
this->gameArea->setBoxPenColor(Qt::black);
}
if(ui->comboBox_2->currentIndex()==0){
this->gameArea->setGameAreaColor(Qt::black);
this->gameArea->setGameAreaColor1(Qt::black);
}
this->gameArea->draw_gameArea();
this->gameArea->setFocus();
}
void Widget::on_comboBox_2_activated(int index)//选定颜色模式
{
if(ui->comboBox_2->currentIndex()==0){
ui->pushButton_4->setEnabled(false);
ui->pushButton_5->setEnabled(false);
ui->pushButton_6->setEnabled(false);
ui->horizontalSlider->setEnabled(true);
this->gameArea->color_model=0;//更新颜色模式
on_comboBox_activated(0);
gamewid->boxBrushColor=this->gameArea->green_change;
gamewid->boxBrushColor1=this->gameArea->green_change;
gamewid->score_change();
}
if(ui->comboBox_2->currentIndex()==1){
ui->pushButton_4->setEnabled(true);
ui->pushButton_5->setEnabled(true);
ui->pushButton_6->setEnabled(true);
ui->horizontalSlider->setEnabled(false);
this->gameArea->color_model=1;//更新颜色模式
this->gameArea->setBoxBrushColor(this->gameArea->green_change);
this->gameArea->setBoxBrushColor1(this->gameArea->green_change);
this->gameArea->setBoxPenColor(Qt::black);
this->gameArea->setBoxPenColor1(Qt::black);
this->gameArea->draw_gameArea();
this->gameArea->setFocus();
}
}
void Widget::on_comboBox_3_activated(int index)//游戏模式选择
{
if (ui->comboBox_3->currentIndex() == 0)//左右分视
{
this->gameArea->mode = 0;
this->gamewid->mode = 0;
this->gameArea->init_gameArea(110, 20, QApplication::desktop()->width(), QApplication::desktop()->height(), QApplication::desktop()->width() / 3 - 8 - QApplication::desktop()->width() / 3 / 7, QApplication::desktop()->height() - 134, QApplication::desktop()->width() / 3 / 14, 220, 0);
}
if (ui->comboBox_3->currentIndex() == 1)//偏正光
{
this->gameArea->mode = 1;
this->gamewid->mode = 1;
this->gameArea->init_gameArea(240, 20, QApplication::desktop()->width(), QApplication::desktop()->height(), (QApplication::desktop()->width() / 3 - 8 - QApplication::desktop()->width() / 3 / 7) / 2, QApplication::desktop()->height() - 134, QApplication::desktop()->width() / 3 / 14, 220, 0);
}
on_pushButton_3_clicked();
}
void Widget::on_comboBox_4_activated(int index)//游戏难度选择
{
if(ui->comboBox_4->currentIndex()==0)this->speed=800;//简单
if(ui->comboBox_4->currentIndex()==1)this->speed=500;//普通
if(ui->comboBox_4->currentIndex()==2)this->speed=300;//困难
if(ui->comboBox_4->currentIndex()==3)this->speed=100;//地狱
on_pushButton_clicked();
this->timer->stop();
}
void Widget::on_horizontalSlider_actionTriggered(int action)//弱视程度
{ QString str;
this->ui->horizontalSlider->setMaximum(100);
str = tr("%1").arg(this->ui->horizontalSlider->value());
this->ui->label_6->setText(str.append("%"));
this->gameArea->transparency=255/100*this->ui->horizontalSlider->value()+50;
this->gameArea->green_change=QColor(0,255,0,this->gameArea->transparency);
on_comboBox_activated(ui->comboBox->currentIndex());
this->gameArea->draw_gameArea();
}