-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackpanel.cpp
376 lines (331 loc) · 7.18 KB
/
backpanel.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "backpanel.h"
#include "configuration.h"
#include <QDebug>
#include <QPainter>
#include <QStyleOption>
#include <QKeyEvent>
#include "widget.h"
#include <QFont>
BackPanel::BackPanel(QWidget *parent) :
QWidget(parent),
m_barrier(new Barrier)
{
init();
initStyle();
setFocusPolicy(Qt::StrongFocus);
//获得焦点
setFocus();
m_start = false;
m_parent = static_cast<Widget *>(parent);
m_score = 0;
//游戏水平默认为1
m_gameLevel = 7;
m_barrier->setBackPanel(this);
m_gameOver = false;
}
void BackPanel::suspendOrRun()
{
m_thread->suspendOrRun();
}
/**
* 增加分数
*/
void BackPanel::addScore()
{
//qDebug() << "BackPanel::addScore invoke!";
int score = (8 - m_gameLevel) * 10;
m_score += score;
m_parent->setScore(m_score);
}
void BackPanel::stopGame()
{
setStart(false);
m_thread->setRun(false);
//清空数组
m_barrier->clear();
m_shape->destroy();
m_shape = NULL;
//删除线程
// delete m_thread;
// m_thread = NULL;
update();
}
void BackPanel::setStart(bool s)
{
m_start = s;
}
QSize BackPanel::sizeHint() const
{
int w = Configuration::GRID_WIDTH * 15;
int h = Configuration::GRID_WIDTH * 19;
return QSize(w, h);
}
void BackPanel::init()
{
m_back = true;
m_net = true;
//设置默认背景颜色
m_backColor = QColor::fromRgb(205, 219, 255);
m_netColor = QColor::fromRgb(0xc0, 0xc0, 0xc0);
//初始化就有一个Shape
m_shape = new Shape;
//让Shape保持BackPanel的指针
m_shape->setBackPanel(this);
//开启线程
m_thread = new Thread;
m_thread->setBackPanel(this);
}
void BackPanel::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
//qDebug() << "resize Event!";
// m_shape->setWH(geometry().width(), geometry().height());
// int h = geometry().height();
m_shape->setWH(geometry().width(), 475);
}
void BackPanel::initStyle()
{
}
/**
* 恢复默认设置
*/
void BackPanel::reset()
{
m_backColor = QColor::fromRgb(205, 219, 255);
m_netColor = QColor::fromRgb(0xc0, 0xc0, 0xc0);
m_net = true;
m_back = true;
update();
}
/**
* 判断shape是否能够往下移动
* @return true 表示能移动,false表示不能移动
*/
bool BackPanel::canMoveDown(int y)
{
//计算形状所占的最长的距离
int d =4 - m_shape->trimDown();
int dy = (m_shape->getY())/ Configuration::GRID_WIDTH;
int x = m_shape->getX() / Configuration::GRID_WIDTH;
//qDebug() << "canMoveDown x:" << x;
//qDebug() << "getY:" << m_shape->getY() << "\ty:" << y;
bool flag = m_barrier->hasBrrerFromY(x, dy, m_shape);
return !flag;
}
/**
* 游戏结束
*/
void BackPanel::gameOver()
{
qDebug() << "game over";
m_thread->setRun(false);
m_gameOver = true;
}
bool BackPanel::canMoveXY(int x)
{
int dy = m_shape->getY()/ Configuration::GRID_WIDTH;
int dx = (m_shape->getX() + x)/ Configuration::GRID_WIDTH;
return !m_barrier->hasBarrerFromX(dx, dy, m_shape);
}
/**
* 线程安全的移动坐标
* @param x
* @param y
*/
void BackPanel::moveXY(int x, int y)
{
m_mutex.lock();
if( x != -1)
{
m_shape->addX(x);
} else
{
m_shape->addY();
}
m_mutex.unlock();
}
/**
* 形状运行
*/
void BackPanel::shapeRun()
{
//qDebug() << "shape run...";
//qDebug() << "m_shape:" << m_shape;
// m_shape->addY();
moveXY(-1, Configuration::GRID_WIDTH);
update();
}
/**
* 让Barrer接收Shape
*/
void BackPanel::acceptShape()
{
m_barrier->accept(m_shape);
m_shape->destroy();
m_shape = new Shape;
m_shape->setBackPanel(this);
m_shape->setWH(geometry().width(), 475);
//判断是否有满行
m_barrier->haveFullLine();
}
void BackPanel::setBackColor(QColor c)
{
m_backColor = c;
}
void BackPanel::setBack(bool b)
{
m_back = b;
update();
}
void BackPanel::setNet(bool n)
{
m_net = n;
update();
}
void BackPanel::setNetColor(QColor c)
{
m_netColor = c;
}
void BackPanel::paintEvent(QPaintEvent *e)
{
if( !m_start )
return;
QPainter paint(this);
paint.setRenderHint(QPainter::Antialiasing);
if( m_back )
drawBack(paint);
if( m_net )
drawNet(paint);
drawChild(paint);
//如果游戏结束,那么绘制结束字样
if( m_gameOver )
drawGameOver(paint);
}
//绘制结束时的样式
void BackPanel::drawGameOver(QPainter &paint)
{
paint.save();
//设置画笔颜色为红色
paint.setPen(Qt::red);
//设置画笔的字体
QFont font;
font.setPointSize(30);
paint.setFont(font);
paint.drawText(rect(), Qt::AlignCenter, QStringLiteral("GAME OVER!SCORE:%1").arg(m_score));
paint.restore();
}
void BackPanel::startGame()
{
if( !m_thread )
{
m_thread = new Thread;
m_thread->setBackPanel(this);
}
m_thread->start();
m_start = true;
update();
}
void BackPanel::keyPressEvent(QKeyEvent *e)
{
if( !m_shape )
return;
int k = e->key();
switch( k )
{
case Qt::Key_Left:
// m_shape->addX(- Configuration::GRID_WIDTH);
moveXY(- Configuration::GRID_WIDTH, -1);
break;
case Qt::Key_Right:
// m_shape->addX(Configuration::GRID_WIDTH);
moveXY(Configuration::GRID_WIDTH, -1);
break;
case Qt::Key_Up:
m_shape->nextShape();
break;
case Qt::Key_Down:
// m_shape->addY();
moveXY(-1, Configuration::GRID_WIDTH);
break;
//回车键
case Qt::Key_Enter: //小键盘的enter键
case Qt::Key_Return: //回车键
// qDebug() << "enter !";
m_parent->suspend();
break;
}
update();
}
void BackPanel::setGameLevel(int g)
{
//保存游戏水平
m_gameLevel = g / 100;
m_thread->setSleepTime(g);
}
void BackPanel::drawBack(QPainter &paint)
{
paint.save();
int w = geometry().width();
int h = geometry().height();
paint.setBrush(m_backColor);
paint.drawRect(0, 0, w, h);
paint.restore();
}
/**
* 绘制网格线
* @param paint
*/
void BackPanel::drawNet(QPainter &paint)
{
//qDebug() << "drawNet";
paint.save();
QPen pen;
pen.setColor(m_netColor);
pen.setWidth(1);
paint.setPen(pen);
int w = geometry().width();
int h = geometry().height();
//绘制行
for(int i = 0; i < Configuration::ROW; i++)
{
paint.drawLine(0, Configuration::GRID_WIDTH * i, w, Configuration::GRID_WIDTH * i);
}
//绘制列
for(int i = 0; i < Configuration::COLUMN; i++)
{
paint.drawLine(Configuration::GRID_WIDTH * i, 0, Configuration::GRID_WIDTH * i, h);
}
paint.restore();
}
void BackPanel::drawChild(QPainter &paint)
{
paint.save();
m_barrier->drawMe(paint);
//如果当前有Shape那么可以绘制Shape形状
if( m_shape )
{
m_shape->drawMe(paint);
}
paint.restore();
}
BackPanel::~BackPanel()
{
if( m_barrier )
{
delete m_barrier;
m_barrier = NULL;
}
if( m_shape )
{
delete m_shape;
m_shape = NULL;
}
if( m_thread )
{
qDebug() << "m_thread destroy!";
if( m_thread->isRunning() )
m_thread->quit();
delete m_thread;
m_thread = NULL;
}
}