-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetrisGame.cpp
99 lines (88 loc) · 1.97 KB
/
TetrisGame.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
#include "TetrisGame.h"
TetrisGame::TetrisGame() : BaseApp(50, 30),
tetris(*this),
game_status(game_new)
{
PrintControlInfo();
}
void TetrisGame::KeyPressed(int btnCode)
{
char ch = 0;
enum {
quit = 113,
pause = 112,
game_continue = 99,
reset = 114,
start = 115
};
switch (btnCode) {
case quit:
exit(0);
break;
case pause:
game_status = game_paused;
PrintPaused();
break;
case game_continue:
if (game_status == game_paused) {
game_status = game_paused;
game_status = game_active;
tetris.SetTetrisField();
tetris.FigureAction(true);
}
break;
case reset:
if (game_status != game_new) {
game_status = game_new;
PrintGameOver();
tetris.ResetGame();
}
break;
case start:
if (game_status == game_new) {
game_status = game_active;
tetris.StartGame();
}
break;
}
if (game_status == game_active) {
tetris.KeyPressed(btnCode);
}
}
void TetrisGame::UpdateF(float deltaTime)
{
bool result = false;
if (game_status == game_active) {
result = tetris.UpdateF(deltaTime);
if (result == false) {
game_status = game_over;
PrintGameOver();
}
}
}
void TetrisGame::PrintGameOver()
{
auto over_str = "Game Over";
for (int i = 0; i < strlen(over_str); i++) {
SetChar(1 + i, tetris.tetris_field_pos.second + 7, ' ');
SetChar(1 + i, tetris.tetris_field_pos.second + 8, over_str[i]);
SetChar(1 + i, tetris.tetris_field_pos.second + 9, ' ');
}
}
void TetrisGame::PrintPaused()
{
auto paused_str = "Paused";
for (int i = 0; i < strlen(paused_str); i++) {
SetChar(3+i, tetris.tetris_field_pos.second + 7, ' ');
SetChar(3+i, tetris.tetris_field_pos.second + 8, paused_str[i]);
SetChar(3+i, tetris.tetris_field_pos.second + 9, ' ');
}
}
void TetrisGame::PrintControlInfo()
{
for (int i = 0; i < controls_description.size(); i++) {
for (int j = 0; j < controls_description[i].length(); j++) {
SetChar(controls_desc_position.first + j, controls_desc_position.second + i, controls_description[i][j]);
}
}
}