-
Notifications
You must be signed in to change notification settings - Fork 12
/
Game.cpp
406 lines (368 loc) · 8.41 KB
/
Game.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
#include "stdafx.h"
#include "Game.h"
Game::Game(SDL_Renderer* renderer, SDL_Texture* texture, TTF_Font* Font):
FocusBlock(nullptr), NextBlock(nullptr), renderer(renderer),
texture(texture), Font(Font)
{
Score = 0;
Level = 1;
FocusBlockSpeed = INITIAL_SPEED;
}
// 初始化游戏
void Game::Init()
{
// 初始化随机数种子
srand((unsigned int)time(0));
// 初始化游戏块,位置等信息
FocusBlock = new GameBlock(BLOCK_START_X, BLOCK_START_Y, (BlockType)(rand() % 7), texture);
NextBlock = new GameBlock(NEXT_BLOCK_X, NEXT_BLOCK_Y, (BlockType)(rand() % 7), texture);
}
// 当前游戏自动向下移动
void Game::GameBlock_selfMove()
{
// 当前游戏块向下移动每一帧的逻辑
static int GameBlock_down_counter = 0;
// 当当前游戏块移动到底部的逻辑
static int Slide_counter = SLIDE_TIME;
// 处理当前游戏块自动下落
GameBlock_down_counter++;
if (GameBlock_down_counter >= FocusBlockSpeed)
{
// 游戏块向下移动需要做碰撞检测
if (!CheckWallCollisions(FocusBlock, DOWN) &&
!CheckEntityCollisions(FocusBlock, DOWN))
{
FocusBlock->Move(DOWN);
GameBlock_down_counter = 0;
}
}
// 处理当前游戏块移动到底部的逻辑
if (CheckWallCollisions(FocusBlock, DOWN) ||
CheckEntityCollisions(FocusBlock, DOWN))
{
Slide_counter--;
}
else
{
Slide_counter = SLIDE_TIME;
}
if (Slide_counter == 0)
{
Slide_counter = SLIDE_TIME;
HandleStaticBlockCollision();
}
// 绘制
Draw();
}
// 绘制当前游戏块、下一个游戏块以及所有方块
void Game::Draw()
{
// 绘制当前游戏块、下一个游戏块
FocusBlock->DrawSquares(renderer);
NextBlock->DrawSquares(renderer);
// 绘制所有方块
for (int i=0; i<GameSquares.size(); ++i)
{
GameSquares[i]->DrawToRenderer(renderer);
}
// 绘制当前得分、进入下一关所需得分、级别
char nex_square[50];
sprintf_s(nex_square, "NEXT SQUARE:");
Draw_text(nex_square, NEXT_BLOCK_X - 95, (NEXT_BLOCK_Y - 115));
// 得分
char cur_score[50];
sprintf_s(cur_score, "CURRENT SCORE: %d", Score);
Draw_text(cur_score, SCORE_POSITION_X, SCORE_POSITION_Y);
// 级别
char cur_level[50];
sprintf_s(cur_level, "CURRENT LEVEL: %d", Level);
Draw_text(cur_level, LEVEL_POSITION_X, LEVEL_POSITION_Y);
// 每关所需得分
char level_score[50];
sprintf_s(level_score, "SCORE OF LEVEL: %d", SCORE_PRE_LEVEL);
Draw_text(level_score, SCORE_PER_LEVEL_X, SCORE_PER_LEVEL_Y);
// 当前游戏块下落速度
char cur_speed[50];
sprintf_s(cur_speed, "CURRENT SPEED: %d", INITIAL_SPEED);
Draw_text(cur_speed, SPEED_X, SPEED_Y);
}
void Game::Draw_text(std::string message, int x, int y)
{
SDL_Color color = { 255, 255, 255 };
SDL_Surface* surface = TTF_RenderText_Blended(Font, message.c_str(), color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
int width = 0, height = 0;
SDL_QueryTexture(texture, NULL, NULL, &width, &height);
SDL_Rect rect = {
x, y, width, height
};
SDL_RenderCopy(renderer, texture, NULL, &rect);
}
// 检测游戏块是否与Wall碰撞
bool Game::CheckWallCollisions(GameBlock* FocusGameBlock, Direction dir)
{
Square** squares = FocusGameBlock->GetSquares();
for (int i=0; i<4; ++i)
{
if (CheckWallCollisions(squares[i], dir))
return true;
}
return false;
}
bool Game::CheckWallCollisions(Square* square, Direction dir)
{
// 每移动一次的距离
int Distance = SQUARES_MEDIAN * 2;
// 获得给定方块的中心
int x = square->getCenter_x();
int y = square->getCenter_y();
bool result = false;
switch (dir)
{
case LEFT:
{
if ((x - Distance) < GAME_AREA_LEFT)
result = true;
else
result = false;
}
break;
case RIGHT:
{
if ((x + Distance) > GAME_AREA_RIGHT)
result = true;
else
result = false;
}
break;
case DOWN:
{
if ((y + Distance) > GAME_AREA_BOTTOM)
result = true;
else
result = false;
}
break;
default:
break;
}
return result;
}
bool Game::CheckEntityCollisions(Square* square, Direction dir)
{
// 两个方块之间的距离
int Distance = SQUARES_MEDIAN * 2;
// 获得给定方块的中心
int centerX = square->getCenter_x();
int centerY = square->getCenter_y();
switch (dir)
{
case LEFT:
centerX -= Distance;
break;
case RIGHT:
centerX += Distance;
break;
case DOWN:
centerY += Distance;
break;
default:
break;
}
// 检测给定方块是否与其它方块发生碰撞
for (int i=0; i<GameSquares.size(); ++i)
{
int x = GameSquares[i]->getCenter_x();
int y = GameSquares[i]->getCenter_y();
if ((abs(x - centerX) < Distance) &&
(abs(y - centerY) < Distance))
{
return true;
}
}
return false;
}
// 检测游戏块是否与其它块碰撞
bool Game::CheckEntityCollisions(GameBlock* FocusGameBlock, Direction dir)
{
Square** squares = FocusGameBlock->GetSquares();
for (int i=0; i<4; ++i)
{
if (CheckEntityCollisions(squares[i], dir))
return true;
}
return false;
}
// 处理游戏块移动到底部时的逻辑
void Game::HandleStaticBlockCollision()
{
// 改变焦点块
ChangeFocusBlock();
// 获得已完成的行数
int completeNums = CalcCompleteRows();
if (completeNums > 0)
{
// 计算获得的分数
Score += SCORE_PRE_LINE * completeNums;
// 判断是否能进入下一关
if (Score >= Level * SCORE_PRE_LEVEL)
{
Level++;
// is Win?
// 每进入下一关游戏块的移动速度将会增加
FocusBlockSpeed -= SPEED_CHANGE;
}
}
// is Fail?
}
// 检测当前游戏块旋转后是否发生碰撞
bool Game::CheckRotationCollisions(GameBlock* block)
{
// 获取当前游戏块旋转后的位置
int* arr = block->GetRotatedPositions();
// 两个方块中心之间的距离
int Distance = SQUARES_MEDIAN * 2;
// 检测Wall和已有的方块
for (int i=0; i<4; ++i)
{
// 检测左右游戏边界
if (arr[i*2] < GAME_AREA_LEFT ||
arr[i*2] > GAME_AREA_RIGHT)
{
delete arr;
return true;
}
// 检测游戏下方边界
if (arr[i*2 + 1] > GAME_AREA_BOTTOM)
{
delete arr;
return true;
}
// 与已有的方块比较
for (int j=0; j<GameSquares.size(); ++j)
{
if ((abs(arr[i*2] - GameSquares[j]->getCenter_x()) < Distance) &&
(abs(arr[i*2+1] - GameSquares[j]->getCenter_y()) < Distance))
{
delete arr;
return true;
}
}
}
delete arr;
return false;
}
// 改变焦点块,每次改变焦点块时,我们需要将当前焦点块中的方块加入到已有方块集合中
// 删除焦点块,设置焦点块以及下一个游戏块
void Game::ChangeFocusBlock()
{
Square** squares = FocusBlock->GetSquares();
// 将焦点块中的方块加入到已有方块集合中
for (int i=0; i<4; ++i)
{
GameSquares.push_back(squares[i]);
}
// 删除焦点块
delete FocusBlock;
// 重新设置焦点块
FocusBlock = new GameBlock(NextBlock->getBlockType(), texture);
FocusBlock->SetupBlock(BLOCK_START_X, BLOCK_START_Y, texture);
// 设置下一个游戏块
delete NextBlock;
NextBlock = new GameBlock(NEXT_BLOCK_X, NEXT_BLOCK_Y, (BlockType)(rand() % 7), texture);
}
// 计算完成的行数
int Game::CalcCompleteRows()
{
// 底线
int bottomLine = GAME_AREA_BOTTOM - SQUARES_MEDIAN;
// top line
int topLine = SQUARES_MEDIAN;
// 每一行的距离
int rowSize = SQUARES_MEDIAN * 2;
// 记录每行中方块的个数
int SquareNums_per_row[21];
for (int i=0; i<21; ++i)
{
SquareNums_per_row[i] = 0;
}
int row = 0, completeRows = 0;
// Check for full lines
for (int i=0; i<GameSquares.size(); ++i)
{
// 获得到当前方块所在的行
int y = GameSquares[i]->getCenter_y();
row = (y - topLine) / rowSize;
SquareNums_per_row[row]++;
}
// Erase any full lines
for (int line=0; line<21; ++line)
{
// 如果当前行被方块填满
if (SquareNums_per_row[line] == SQUARES_PER_ROW)
{
completeRows++;
// 删除掉被方块填满的行中的方块
for (int i=0; i<GameSquares.size(); ++i)
{
if (((GameSquares[i]->getCenter_y() - topLine) / rowSize) == line)
{
delete GameSquares[i];
GameSquares.erase(GameSquares.begin() + i);
i--;
}
}
}
}
// 将已填满行上面的所有方块向下移动
for (int i=0; i<GameSquares.size(); ++i)
{
for (int line=0; line<21; ++line)
{
if (SquareNums_per_row[line] == SQUARES_PER_ROW)
{
int y = GameSquares[i]->getCenter_y();
row = (y - topLine) / rowSize;
if (line > row)
{
GameSquares[i]->Move(DOWN);
}
}
}
}
return completeRows;
}
// 处理键盘输入,IsUp参数只对向上方向有效
void Game::HandleInput(Direction dir, bool IsUp)
{
// 处理向上方向键,旋转当前游戏块
if (IsUp)
{
if (!CheckRotationCollisions(FocusBlock))
FocusBlock->Rotate();
}
else
{
if (!CheckWallCollisions(FocusBlock, dir) &&
!CheckEntityCollisions(FocusBlock, dir))
{
FocusBlock->Move(dir);
}
}
}
Game::~Game(void)
{
if (FocusBlock)
delete FocusBlock;
if (NextBlock)
delete NextBlock;
vector<Square*>::const_iterator it = GameSquares.begin();
for (; it != GameSquares.end(); ++it)
{
const Square* del = (*it);
if (del)
delete del;
}
GameSquares.clear();
}