-
Notifications
You must be signed in to change notification settings - Fork 0
/
OldTetrisGame.cpp
149 lines (122 loc) · 3.47 KB
/
OldTetrisGame.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
#include "TetrisGame.h"
#include "GoToXY.h"
#include "Config.h"
#include "TetrisBoard.h"
#include <thread>
#include <chrono>
#define invalid_Key -1
void TetrisGame::initGame(){
displayBorder();
printMenu();
runGame();
}
void TetrisGame::printMenu(){
gotoxy(14,3);
for (int j = 2; j < 17; j++){
gotoxy(14, j+1);
cout << "*";
gotoxy(34, j + 1);
cout << "*" << endl;
}
for (int k = 0; k < 12; k++)
cout << "-";
cout << endl;
}
void TetrisGame::displayBorder(){
cout << "Speed:" << " Parts: " << endl;
cout << "Score: ";
gotoxy(0, 3);
for (int j = 2; j < 17; j++){
cout << "#";
gotoxy(11, j + 1);
cout << "#" << endl;
}
for (int k = 0; k < 12; k++)
cout << "-";
cout << endl;
}
int TetrisGame::checkKeys(char ch){
for (int i = 0; i < 11; i++){
if (ch == keyboards[i])
return i;
}
return invalid_Key;
}
void TetrisGame::runGame(){
int maxY, minY, isBombed = 1, tempTime=0;
TetrisBoard board;
unsigned long int validKey, currentTime, whichShape = rand() % 2 + 10; // update
char keyEntered = '5';
int checkPosition, timeInterval = 800;
setKeys();
board.setBoard();
currentShape.createShape(whichShape);
while (keyEntered != ESC)
{
currentTime = GetTickCount64() + timeInterval;
while (GetTickCount64() <= currentTime){
if (_kbhit()){
keyEntered = _getch();
validKey = checkKeys(keyEntered);
if (validKey != invalid_Key && validKey != ESC){
if (keyEntered == THREE) //accelerate speed
{
currentTime -= 800;
tempTime -= 200;
timeInterval += tempTime;
break;
}
else if (keyEntered == FOUR) // decrease speed
{
currentTime -= 800;
tempTime += 200;
timeInterval += tempTime;
break;
}
if (board.checkPos(currentShape, validKey) == TetrisBoard::FREE_SPACE){ // returns true also if it's a joker
if (keyEntered == SPACE_key){
currentTime -= 800;
timeInterval = 0;
}
else if (currentShape.getShape() == Shape::JOKER && (keyEntered == s_key || keyEntered == S_key))
{
board.updateBoard(currentShape);
isBombed = 0;
break;
}
currentShape.move(validKey, board);
}
else if (currentShape.getShape() == Shape::BOMB){
currentShape.checkBomb(validKey, board);
isBombed = 0;
break;
}
}
}
}
checkPosition = board.checkPos(currentShape, Shape::DOWN);
if ((checkPosition == TetrisBoard::FREE_SPACE || (currentShape.getShape() == Shape::BOMB && currentShape.checkBomb(Shape::DOWN, board) && currentShape.shape[0].getY()>18) ||
(currentShape.getShape() == Shape::JOKER && checkPosition == TetrisBoard::SHAPE_ENCOUNTER)) && isBombed)
currentShape.move(Shape::DOWN, board);
else{ // the last object stopped and a new one needs to be created
isBombed = 1;
timeInterval = 800 + tempTime;
if (currentShape.getShape() != Shape::BOMB)
board.updateBoard(currentShape);
currentShape.getMinMaxShape(minY, maxY);
board.deleteLines(currentShape, minY, maxY);
whichShape = randomNum();
currentShape.deleteShape();
currentShape.createShape(whichShape); // creates a new shape
currentShape.move(Shape::DOWN,board);
}
if (board.checkEndGame()){
gotoxy(1, 20);
cout << "Game over";
break;
}
}
gotoxy(2, 17);
keyEntered = _getch();
exit(0);
}