-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisGame.cpp
More file actions
391 lines (332 loc) · 9.64 KB
/
TetrisGame.cpp
File metadata and controls
391 lines (332 loc) · 9.64 KB
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
#include "TetrisGame.h"
#include "Line.h"
#include "Cube.h"
#include "Gun.h"
#include "Tee.h"
#include "Zigzag.h"
#include "Bomb.h"
#include "Joker.h"
#include "ScoreBar.h"
#include "Config.h"
#include "TetrisBoard.h"
#include <thread>
#include <chrono>
int TetrisGame:: MenuControl(char keyPressed, TetrisBoard& board, Score& scoreStatus) {
switch (keyPressed) {
case '1':
if (!gameStarted) {
setGameStarted();
return runGame(board, scoreStatus);
}
break;
case '2':
if (gameStarted) {
setGameStarted(); //prints the normal continue color
return runGame(board, scoreStatus);
}
break;
case '8':
ShellExecute(0, 0, L"https://i.imgur.com/0u47UC2.jpg", 0, 0, SW_SHOW);
return CONTINUE_GAME;
case '9':
printGameOver();
return END_GAME;
}
_flushall();
return CONTINUE_GAME;
}
void TetrisGame::continueBlink() {
setTextColor(DARKGREY);
gotoxy(16, 8);
cout << "(2)";
gotoxy(28, 8);
cout << "CONTINUE";
Sleep(200);
setTextColor(WHITE);
gotoxy(16, 8);
cout << "(2)";
gotoxy(28, 8);
cout << "CONTINUE";
Sleep(200);
}
void TetrisGame::hardDrop(Score & scoreStatus, int & timeInterval, unsigned long int & currentTime, int & minY, int & maxY)
{
currentShape->getMinMaxShape(minY, maxY);
scoreStatus.setDistance(currentShape, minY);
scoreStatus.updateScoreValue(2 * scoreStatus.getDistance()); // hard drop
currentTime -= 800;
timeInterval = 0;
}
void TetrisGame::createNewShape(int whichShape)
{
switch (whichShape) {
case Shape::CUBE:
currentShape = new Cube;
break;
case Shape::LINE:
currentShape = new Line;
break;
case Shape::GUN:
currentShape = new Gun;
break;
case Shape::TEE:
currentShape = new Tee;
break;
case Shape::ZIGZAG:
currentShape = new Zigzag;
break;
case Shape::JOKER:
currentShape = new Joker;
break;
case Shape::BOMB:
currentShape = new Bomb;
break;
}
}
void TetrisGame::setGameStarted() {
setTextColor(DARKGREY);
gotoxy(16, 6);
cout << "(1) START GAME";
if (gameStarted) {
setTextColor(LIGHTGREY);
gotoxy(16, 8);
cout << "(2)";
gotoxy(28, 8);
cout << "CONTINUE";
return;
}
gameStarted = 1;
}
void TetrisGame::initGame(){
TetrisBoard board;
Score scoreStatus;
setKeys();
printMenu();
displayBorder();
scoreStatus.printScore();
scoreStatus.printSpeed();
scoreStatus.printParts();
createNewShape(rand() % 5 + 10);
while (true) // the game runs as long as the user doesn't press 9 or the game ends
{
char keyPressed;
if (gameStarted)
continueBlink();
while (_kbhit())
{
keyPressed = _getch();
if (MenuControl(keyPressed, board, scoreStatus) == END_GAME)
return;
}
}
}
void TetrisGame::printMenu(){
// STATUS BAR
gotoxy(0, 0);
cout << "Parts: " << "Speed: " << " Score: " << endl;
for (int k = 0; k < 39; k++)
cout << (unsigned char)175;
// START - MENU BORDERS
gotoxy(14,2);
cout << (unsigned char)201;
for (int k = 0; k < 23; k++)
cout << (unsigned char)205;
cout << (unsigned char)187 << endl;
for (int j = 2; j < 17; j++){
gotoxy(14, j+1);
cout << (unsigned char)186;
gotoxy(38, j + 1);
cout << (unsigned char)186 << endl;
}
gotoxy(14, 18);
cout << (unsigned char)200;
for (int k = 0; k < 23; k++)
cout << (unsigned char)205;
cout << (unsigned char)188 << endl;
// END MENU BORDERS
// HEDLINE MENU
gotoxy(23,3);
cout << "M E N U" << endl;
gotoxy(15, 4);
for (int k = 0; k < 23; k++)
cout << (unsigned char)175;
gotoxy(16,6);
cout << "(1) START GAME" ;
gotoxy(16, 8);
cout << "(2) PAUSE / CONTINUE" ;
gotoxy(16, 10);
cout << "(3) SPEED UP" ;
gotoxy(16, 12);
cout << "(4) SPEED DOWN";
gotoxy(16, 14);
cout << "(8) HELP!" ;
gotoxy(16, 16);
cout << "(9) EXIT";
}
void TetrisGame::printGameOver() const
{
setTextColor(LIGHTCYAN);
gotoxy(0, 20);
cout << " _____ ____ " << endl;
cout << " / ____| / __ \\ " << endl;
cout << " | | __ __ _ _ __ ___ ___ | | | |_ _____ _ __ " << endl;
cout << " | | |_ |/ _` | '_ ` _ \\ / _ \\ | | | \\ \\ / / _ \\ '__|" << endl;
cout << " | |__| | (_| | | | | | | __/ | |__| |\\ V / __/ | " << endl;
cout << " \\_____|\\__,_|_| |_| |_|\\___| \\____/ \\_/ \\___|_| " << endl;
Sleep(3000);
}
bool TetrisGame::checkExit(char keyEntered) const
{
if (keyEntered == '9') {
printGameOver();
return true;
}
return false;
}
bool TetrisGame::checkPause(char keyEntered) const
{
if (keyEntered == '8') { // 'help'
ShellExecute(0, 0, L"https://i.imgur.com/0u47UC2.jpg", 0, 0, SW_SHOW);
return true;
}
else if (keyEntered == '2') { //'pause'
Sleep(1000);
return true;
}
return false;
}
void TetrisGame::newRound(int & timeInterval, TetrisBoard & board, int & minY, int & maxY, Score & scoreStatus, int & whichShape)
{
updateInterval(timeInterval, scoreStatus);
if (currentShape->getShape() != Shape::BOMB)
board.updateBoard(currentShape);
currentShape->getMinMaxShape(minY, maxY);
scoreStatus.setLinesDeleted(board.deleteLines(currentShape, minY, maxY), currentShape); // deletes lines only within the shape's limit
scoreStatus.updateScoreValue(scoreStatus.getLinesDeleted());
scoreStatus.updateScoreValue(-50 * board.getHowManyDeleted()); // each block the bomb erased costs 50 points
board.sethowManyDeleted(0);
scoreStatus.printParts();
scoreStatus.printScore();
whichShape = randomNum();
delete currentShape;
createNewShape(whichShape); // creates a new shape randomly
currentShape->move(Shape::DOWN, board);
}
void TetrisGame::displayBorder() const
{
gotoxy(0, 3);
for (int j = 2; j < 17; j++){
cout << (unsigned char)186;
gotoxy(11, j + 1);
cout << (unsigned char)186 << endl;
}
cout << (unsigned char)200;
for (int k = 0; k < 10; k++)
cout << (unsigned char)205;
cout << (unsigned char)188 << endl;
cout << endl;
}
int TetrisGame::checkKeys(char ch) const{
for (int i = 0; i < 11; i++){
if (ch == keyboards[i])
return i;
}
return invalid_Key;
}
void TetrisGame::setKeys()
{
keyboards[0] = DOWN_KEY;
keyboards[1] = LEFT_KEY;
keyboards[2] = UP_KEY;
keyboards[3] = RIGHT_KEY;
keyboards[4] = SPACE_key;
keyboards[5] = s_key;
keyboards[6] = S_key;
keyboards[7] = ONE;
keyboards[8] = TWO;
keyboards[9] = THREE;
keyboards[10] = FOUR;
}
int TetrisGame::randomNum() const
{
int res = rand() % 100;
if (res < 70)
res = rand() % 5 + 10;
else
res = rand() % 2 + 15;
return res;
}
int TetrisGame::dropInterval(TetrisBoard & board, Score & scoreStatus, int & timeInterval, int & minY, int & maxY)
{
char keyEntered;
int validKey, speed;
unsigned long int currentTime;
currentTime = (unsigned long int) GetTickCount64() + timeInterval; // the shape goes down every timeInerval ms
while (GetTickCount64() <= currentTime) {
if (_kbhit()) { // otherwise, check for an input
keyEntered = _getch();
_flushall();
validKey = checkKeys(keyEntered); // checks if the pressed key is valid
if (checkExit(keyEntered)) return END_GAME;
if (checkPause(keyEntered)) return PAUSED;
if (validKey != invalid_Key) {
speed = scoreStatus.getSpeed();
//increases or decreases the current speed if possible
if ((keyEntered == THREE && speed < Score::VERY_HIGH) || (keyEntered == FOUR && speed > Score::VERY_SLOW)) {
changeSpeed(keyEntered, timeInterval, scoreStatus);
break;
}
// Space key has been pressed - hard drop
if (keyEntered == SPACE_key) {
hardDrop(scoreStatus, timeInterval, currentTime, minY, maxY);
continue;
}
//Stop the Joker
else if (currentShape->getShape() == Shape::JOKER && (keyEntered == s_key || keyEntered == S_key))
{
board.updateBoard(currentShape);
return TetrisBoard::STOP_JOKER;
}
//Down key has been pressed - soft drop
else if (validKey == Shape::DOWN) {
scoreStatus.updateScoreValue(1); // increases score by 1
scoreStatus.printScore();
setTextColor(currentShape->whichColor());
}
if (currentShape->move(validKey, board) == TetrisBoard::BOMB_EXPLODED)
return TetrisBoard::BOMB_EXPLODED;
}
}
}
return 0;
}
int TetrisGame::runGame(TetrisBoard& board, Score& scoreStatus) {
int maxY, minY, timeInterval, gameStatus;
int whichShape = currentShape->getShape(); // update
updateInterval(timeInterval, scoreStatus);
while (true)
{
gameStatus = dropInterval(board, scoreStatus, timeInterval, minY, maxY);
if (gameStatus==END_GAME || gameStatus==PAUSED) return gameStatus; // the game will continue unless the pause or exit button has been pressed
//additional tests for the shapes, if it can move/detonate (the bomb).
if (gameStatus != TetrisBoard::BOMB_EXPLODED && gameStatus != TetrisBoard::STOP_JOKER) {
gameStatus = currentShape->move(Shape::DOWN, board);
}
//start a new round (creates new shapes and updates the board with the old one)
if(gameStatus == TetrisBoard::BOMB_EXPLODED || gameStatus==TetrisBoard::MOVE_FAIL || gameStatus == TetrisBoard::STOP_JOKER)
newRound(timeInterval, board, minY, maxY, scoreStatus, whichShape);
if (board.checkEndGame()) {
printGameOver();
return END_GAME;
}
}
}
void TetrisGame::changeSpeed(char indicator, int & timeInterval, Score & scoreStatus)
{
if (indicator == THREE) // increase speed
scoreStatus.increaseSpeed();
else // decrease speed
scoreStatus.decreaseSpeed();
updateInterval(timeInterval, scoreStatus);
scoreStatus.printSpeed();
}