-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamescene.cpp
494 lines (476 loc) · 12.7 KB
/
gamescene.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include "gamescene.h"
#include"bordersquare.h"
#include"abstractball.h"
#include"ballfactory.h"
#include<QKeyEvent>
#include"brickfactory.h"
#include"forbiddensquare.h"
#include"abstractsurprisingbrick.h"
#include"abstractplank.h"
#include"gamelevel.h"
#include"plankfactory.h"
#include<QDebug>
#include<QGraphicsSceneMouseEvent>
#include<QTime>
#include<QMediaPlayer>
//构造函数列表初始化
GameScene::GameScene(QObject* parent):
QGraphicsScene(parent),
plank(nullptr),
//音乐音效
brickCollidedPlayer(new QMediaPlayer(this)),
brickDestroyedPlayer(new QMediaPlayer(this)),
ballAddedPlayer(new QMediaPlayer(this)),
plankCollidedPlayer(new QMediaPlayer(this)),
//球的数量,砖的数量
ballAmount(0),
brickAmount(0),
//游戏状态
gameState(ready)
{
qsrand(QTime::currentTime().msecsSinceStartOfDay());
this->setSceneRect(0,0,550,500);
this->initBorder();
//设置音乐
brickCollidedPlayer->setMedia(QUrl("qrc:/music/brickCollided.wav"));
brickDestroyedPlayer->setMedia(QUrl("qrc:/music/brickDestroyed.wav"));
ballAddedPlayer->setMedia(QUrl("qrc:/music/ballAdded.wav"));
plankCollidedPlayer->setMedia(QUrl("qrc:/music/plankCollided.wav"));
}
void GameScene::selectGameLevel(int level)
{
//场景长550,宽500
const bool (*map)[12]=nullptr;
switch ((level-1)%GameLevel::levelAmount+1) {
case 1:
map=GameLevel::leve1;
break;
case 2:
map=GameLevel::leve2;
break;
case 3:
map=GameLevel::leve3;
break;
case 4:
map=GameLevel::leve4;
break;
case 5:
map=GameLevel::leve5;
break;
case 6:
map=GameLevel::leve6;
break;
case 7:
map=GameLevel::leve7;
break;
case 8:
map=GameLevel::leve8;
break;
default:
qDebug()<<"error in math gameLevel"<<level;
break;
}
//砖块长40,宽30
//砖块待选位置 12 X 12 QRectF(35,10,480,360)
QPointF topLeftPoint{35+20,25};
for(int i=0;i<12;i++)
{
for(int j=0;j<12;j++)
{
if(map[j][i])
{
AbstractBrick *brick;
switch (qrand()%(level*10)) {
case 00:
brick=BrickFactory::constructStretchPlankBrick();
break;
case 10:
brick=BrickFactory::constructAcceleratePlankBrick();
break;
case 11:
case 12:
brick=BrickFactory::constructHardBrick();
break;
case 20:
case 21:
case 22:
case 23:
case 24:
brick=BrickFactory::constructHarderBrick();
break;
case 30:
case 31:
case 32:
case 33:
case 34:
brick=BrickFactory::constructShrinkPlankBrick();
break;
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
case 48:
case 49:
brick=BrickFactory::constructHarderBrick();
break;
case 50:
case 51:
case 52:
case 53:
brick=BrickFactory::constructDeceleratePlankBrick();
break;
default:
if(level<8)
{
brick=qrand()%5?BrickFactory::constructNormalBrick():BrickFactory::constructAddBallBrick();
}
else
{
brick=qrand()%3?(rand()%2?BrickFactory::constructHarderBrick():BrickFactory::constructHardBrick())\
:BrickFactory::constructAddBallBrick();
}
break;
}
this->addBrick(brick,topLeftPoint+QPoint{40*i,30*j});
}
}
}
//初始状态设置球和板子
auto ball=BallFactory::constructNormalBall();
ball->stopMoving();
this->addBall(ball,QPoint(255,460));
this->plank=PlankFactory::constructNormalPlank();
this->addItem(plank);
plank->setPos(255,480);
this->monitorPlank(plank);
this->gameState=ready;
}
//void GameScene::gameStart()
//{
// if(this->gameState==ready)
// {
// for(auto& i:this->items())
// {
// if(dynamic_cast<AbstractBall*>(i))
// {
// static_cast<AbstractBall*>(i)->startToMove();
// }
// }
// this->gameState=start;
// }
//}
//键盘动作控制板子的移动
void GameScene::keyPressEvent(QKeyEvent* e)
{
switch (e->key()) {
case Qt::Key_A:
case Qt::Key_Left:
if(plank->isMovingState(AbstractPlank::notMove)||plank->isMovingState(AbstractPlank::moveToRight))
{
plank->setMovingState(AbstractPlank::moveToLeft);
e->accept();
}
break;
case Qt::Key_D:
case Qt::Key_Right:
if(plank->isMovingState(AbstractPlank::notMove)||plank->isMovingState(AbstractPlank::moveToLeft))
{
plank->setMovingState(AbstractPlank::moveToRight);
e->accept();
}
break;
//使用空格游戏暂停
case Qt::Key_Space:
if(this->gameState==ready||this->gameState==pause)
{
emit this->gameContinue();
this->gameState=start;
}
else if(this->gameState==this->start)
{
emit this->gamePause();
this->gameState=pause;
}
break;
case Qt::Key_1:
// emit this->gameWin();
break;
case Qt::Key_3:
// emit this->gameLose();
break;
default:
e->ignore();
break;
}
}
//键盘事件
void GameScene::keyReleaseEvent(QKeyEvent* e)
{
switch (e->key())
{
case Qt::Key_A:
case Qt::Key_Left:
if(plank->isMovingState(AbstractPlank::moveToLeft))
{
plank->setMovingState(AbstractPlank::notMove);
e->accept();
}
break;
case Qt::Key_D:
case Qt::Key_Right:
if(plank->isMovingState(AbstractPlank::moveToRight))
{
plank->setMovingState(AbstractPlank::notMove);
e->accept();
}
break;
default:
e->ignore();
break;
}
}
//设置礼物
void GameScene::randSetGift()
{
//x是浮点类型
qreal x=qrand()%500;
Gift *gift;
//switch选择出现什么样的礼物
switch(qrand()%10)
{
case 1:
case 2:
gift=new Gift(Gift::addOneSuperBall);
break;
default:
gift=new Gift(Gift::addThreeNormalBall);
break;
}
this->addItem(gift);
gift->setPos(x+25,0);
this->monitorGift(gift);
}
void GameScene::ballCollided(QList<QGraphicsItem*> items, AbstractBall* ball)
{
//球的撞击
//撞击的循环
for(auto& i:items)
{
//指针类型转换
if(dynamic_cast<AbstractPlank*>(i))
{
//播放音效
plankCollidedPlayer->play();
static_cast<AbstractPlank*>(i)->collidingWithBall(ball);
}
else if(dynamic_cast<AbstractBrick*>(i))
{
brickCollidedPlayer->play();
static_cast<AbstractBrick*>(i)->collidingWithBall();
}
else if(dynamic_cast<ForbiddenSquare*>(i))
{
ForbiddenSquare::handleColliding(ball);
}
}
}
void GameScene::giftCollided(QList<QGraphicsItem*> items, Gift* gift)
{
//撞击到礼物
for(auto& i:items)
{
if(dynamic_cast<AbstractPlank*>(i))
{
this->analysisGift(gift);
gift->accept();
return;
}
else if(dynamic_cast<ForbiddenSquare*>(i))
{
ForbiddenSquare::handleColliding(gift);
gift->accept();
}
}
}
//坐标,角度
void GameScene::offsetBall(qreal xOff, qreal yOff, QGraphicsItem* ball)
{
static_cast<AbstractBall*>(ball)->offsetAngle(xOff,yOff);
}
void GameScene::monitorBall(AbstractBall* ball)
{
//连接信号和槽
//球没了
this->connect(ball,SIGNAL(destroyed(QObject*)),this,SLOT(ballDestroyed()));
//撞击到物体
connect(ball,SIGNAL(collidingWithItem(QList<QGraphicsItem*>,AbstractBall*)),\
this,SLOT(ballCollided(QList<QGraphicsItem*>,AbstractBall*)));
//游戏开始
connect(this,SIGNAL(gameContinue()),ball,SLOT(startToMove()));
//游戏暂停
connect(this,SIGNAL(gamePause()),ball,SLOT(stopMoving()));
}
void GameScene::monitorBrick(AbstractBrick* brick)
{
//连接信号和槽
//撞到砖块
this->connect(brick,SIGNAL(destroyed(QObject*)),this,SLOT(brickDestroyed()));
if(dynamic_cast<AbstractSurprisingBrick*>(brick))
{
//撞到有奖品的砖块
this->connect(static_cast<AbstractSurprisingBrick*>(brick),SIGNAL(awardSent(Gift*)),\
this,SLOT(monitorGift(Gift*)));
}
}
void GameScene::monitorPlank(AbstractPlank* plank)
{
//板子上球的位置
connect(plank,SIGNAL(setOffset(qreal,qreal,QGraphicsItem*)),\
this,SLOT(offsetBall(qreal,qreal,QGraphicsItem*)));
connect(this,SIGNAL(gameContinue()),plank,SLOT(startToMove()));
connect(this,SIGNAL(gamePause()),plank,SLOT(stopMoving()));
}
void GameScene::monitorGift(Gift* gift)
{
//撞到有奖品的板子
this->connect(gift,SIGNAL(collidingWithItem(QList<QGraphicsItem*>,Gift*)),\
this,SLOT(giftCollided(QList<QGraphicsItem*>,Gift*)));
connect(this,SIGNAL(gameContinue()),gift,SLOT(startToMove()));
connect(this,SIGNAL(gamePause()),gift,SLOT(stopMoving()));
}
//球没了
void GameScene::ballDestroyed()
{
//球的数量减少
ballAmount--;
//数量为0,游戏失败
if(ballAmount==0)
{
emit this->gameLose();
}
}
//砖块被击碎
void GameScene::brickDestroyed()
{
//播放音效
brickDestroyedPlayer->play();
//砖块的数量减少
brickAmount--;
//分数加100
emit upScore(100);
//砖的数量为0
if(brickAmount==0)
{
//游戏胜利的信号
emit this->gameWin();
}
}
void GameScene::initBorder()
{
//场景长550,宽500
//边界方块长50,宽50
for(int i=0;i<13;i++)
{
auto borderSquare=new BorderSquare;//上
this->addItem(borderSquare);
borderSquare->setPos(-25+i*50,-25);
auto forbiddenSquare=new ForbiddenSquare;//下
this->addItem(forbiddenSquare);
forbiddenSquare->setPos(-25+i*50,525);
}
for(int i=0;i<10;i++)
{
auto borderSquare=new BorderSquare;//左
this->addItem(borderSquare);
borderSquare->setPos(-25,25+i*50);
borderSquare=new BorderSquare;//右
this->addItem(borderSquare);
borderSquare->setPos(575,25+i*50);
}
}
void GameScene::analysisGift(Gift* gift)
{
switch (gift->information())
{
//QpointF类,可以用浮点的精度表示点的坐标
case Gift::addOneNormalBall:
addBall(BallFactory::constructNormalBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
break;
case Gift::addTwoNormalBall:
addBall(BallFactory::constructNormalBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
addBall(BallFactory::constructNormalBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
break;
//增加三个球
case Gift::addThreeNormalBall:
addBall(BallFactory::constructNormalBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
addBall(BallFactory::constructNormalBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
addBall(BallFactory::constructNormalBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
break;
case Gift::addOneFastBall:
addBall(BallFactory::constructFastBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
break;
case Gift::addOneSlowBall:
addBall(BallFactory::constructSlowBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
break;
case Gift::addOneBigBall:
addBall(BallFactory::constructBigBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
break;
case Gift::addOneSmallBall:
addBall(BallFactory::constructSmallBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
break;
case Gift::addOneSuperBall:
addBall(BallFactory::constructSuperBall(),gift->pos()-QPointF{0,plank->boundingRect().height()});
break;
//对板子进行改变
case Gift::shrinkPlank:
this->convertPlank(PlankFactory::constructShorterPlank());
break;
case Gift::stretchPlank:
this->convertPlank(PlankFactory::constructLongerPlank());
break;
case Gift::acceleratePlank:
this->convertPlank(PlankFactory::constructFastPlank());
break;
case Gift::deceleratePlank:
this->convertPlank(PlankFactory::constructSlowPlank());
break;
default:
qDebug()<<"not matching gift information"<<gift->information();
break;
}
}
//增加球
void GameScene::addBall(AbstractBall* ball, QPointF pos)
{
static int i=0;
ballAddedPlayer->play();
this->addItem(ball);
ball->setPos(pos);
this->offsetBall(-1+i++%3,-3,ball);
this->monitorBall(ball);
this->ballAmount++;
}
//增加砖块
void GameScene::addBrick(AbstractBrick* brick, QPointF pos)
{
this->addItem(brick);
brick->setPos(pos);
this->brickAmount++;
this->monitorBrick(brick);
}
//转换板子
void GameScene::convertPlank(AbstractPlank* newPlank)
{
this->addItem(newPlank);
newPlank->setPos(this->plank->pos());
if(this->plank!=nullptr)
{
this->plank->deleteLater();
}
this->plank=newPlank;
this->monitorPlank(newPlank);
}