-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetris.cpp
More file actions
279 lines (228 loc) · 6.81 KB
/
Tetris.cpp
File metadata and controls
279 lines (228 loc) · 6.81 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
#include "../Include/defGameEngine.hpp"
#include <array>
using namespace def;
// If you want to change it then edit
// the RotatePiece function by rewriting indecies there
const def::Vector2i TETROMINO_SIZE = { 4, 4 };
// https://tetris.fandom.com/wiki/Tetromino
// Construct tetrominos assuming that each piece is 4x4 in size
const std::array<std::string, 7> TETROMINOS =
{
"..c...c...c...c.", // def::CYAN
"..b...b..bb.....", // def::BLUE
".o...o...oo.....", // def::ORANGE
".....yy..yy.....", // def::YELLOW
".g...gg...g.....", // def::GREEN
"..p..pp...p.....", // def::PURPLE
"..r..rr..r......", // def::RED
};
def::Pixel GetColor(char c)
{
switch (c)
{
case 'c': return def::CYAN;
case 'b': return def::BLUE;
case 'o': return def::ORANGE;
case 'y': return def::YELLOW;
case 'g': return def::GREEN;
case 'p': return def::PURPLE;
case 'r': return def::RED;
case '#': return def::DARK_GREY;
case ' ': return def::WHITE;
}
return def::NONE;
}
// Assume that every piece is 4x4 in size,
// so applying simple math we get new indices
// of the pieces in map array
int RotatePiece(const def::Vector2i& p, uint8_t r)
{
switch (r % 4)
{
case 0: return p.y * 4 + p.x; // 0 deg
case 1: return 12 + p.y - p.x * 4; // 90 deg
case 2: return 15 - p.y * 4 - p.x; // 180 deg
case 3: return 3 - p.y + p.x * 4; // 270 deg
}
}
class Tetris : public def::GameEngine
{
public:
Tetris()
{
GetWindow()->SetTitle("Tetris");
}
// We will update that string variable
// and match each character to the screen pixels
// equivalent
std::string field;
def::Vector2i fieldPos = { 11, 1 };
def::Vector2i fieldSize = { 10, 20 };
def::Vector2i cellSize = { 8, 8 };
int score = 0;
int piece = -1;
int nextPiece = -1;
int rotation = 0;
def::Vector2f piecePos;
bool gameOver = false;
bool DoesPieceFit(const def::Vector2i& pos, size_t i, int r)
{
if (i >= TETROMINOS.size()) return false;
for (int y = 0; y < TETROMINO_SIZE.y; y++)
for (int x = 0; x < TETROMINO_SIZE.x; x++)
{
int fi = (pos.y + y) * fieldSize.x + (pos.x + x);
// Now we are checking each tetromino local cell against
// it's global analogue
if (pos.y + y >= 0 && pos.y + y < fieldSize.y)
if (pos.x + x >= 0 && pos.x + x < fieldSize.x)
{
// So if it's not a blank cell we check whether
// rotated tetromino piece fits
// in map's cell or not
if (field[fi] != ' ' && TETROMINOS[i][RotatePiece({ x, y }, r)] != '.')
return false;
}
}
return true;
}
/*
* Draws piece and it's background
* based on specified flags
*/
void DrawPiece(const def::Vector2i& pos, size_t i, int rot, bool fillBlank, bool colorised, const def::Pixel& blankCol = def::BLACK)
{
def::Vector2i p;
for (p.y = 0; p.y < TETROMINO_SIZE.y; p.y++)
for (p.x = 0; p.x < TETROMINO_SIZE.x; p.x++)
{
def::Vector2i gp = (fieldPos + def::Vector2i(pos) + p) * cellSize;
char cell = TETROMINOS[i][RotatePiece(p, rot)];
if (cell != '.')
FillRectangle(gp, cellSize, colorised ? GetColor(cell) : def::GREY);
else if (fillBlank)
FillRectangle(gp, cellSize, blankCol);
}
}
protected:
bool OnUserCreate() override
{
field.resize(fieldSize.x * fieldSize.y, ' ');
for (int y = 0; y < fieldSize.y; y++)
for (int x = 0; x < fieldSize.x; x++)
{
if (x == 0 || x == fieldSize.x - 1 || y == fieldSize.y - 1)
field[y * fieldSize.x + x] = '#';
}
nextPiece = rand() % TETROMINOS.size();
return true;
}
bool OnUserUpdate(float deltaTime) override
{
if (gameOver)
{
DrawString(4, cellSize.y * 4, "Game Over!", def::WHITE);
return true;
}
// Pick new piece, put it to the top
// in the center
if (piece < 0)
{
piece = nextPiece;
nextPiece = rand() % TETROMINOS.size();
piecePos = { (float)fieldSize.x * 0.5f, 0.0f };
rotation = 0;
}
if (GetInput()->GetKeyState(def::Key::SPACE).pressed && DoesPieceFit(piecePos, piece, rotation + 1))
rotation++;
if (GetInput()->GetKeyState(def::Key::LEFT).pressed && DoesPieceFit(piecePos + def::Vector2f(-1, 0), piece, rotation))
piecePos.x--;
if (GetInput()->GetKeyState(def::Key::RIGHT).pressed && DoesPieceFit(piecePos + def::Vector2f(+1, 0), piece, rotation))
piecePos.x++;
if (GetInput()->GetKeyState(def::Key::DOWN).held && DoesPieceFit(piecePos + def::Vector2f(0, 5.0f * deltaTime), piece, rotation))
piecePos.y += 5.0f * deltaTime;
// If piece fits, then move it
// one cell down
if (DoesPieceFit(piecePos + def::Vector2f(0.0f, deltaTime * 5.0f), piece, rotation))
piecePos.y += deltaTime * 5.0f;
else
{
if (piecePos.y == 0.0f)
gameOver = true;
// Otherwise place it into field
for (int y = 0; y < TETROMINO_SIZE.y; y++)
for (int x = 0; x < TETROMINO_SIZE.x; x++)
{
int ri = RotatePiece({ x, y }, rotation);
int gi = ((int)piecePos.y + y) * fieldSize.x + ((int)piecePos.x + x);
// If cell in tetromino space (4x4) isn't blank,
// then update that cell in the field string
if (TETROMINOS[piece][ri] != '.')
field[gi] = TETROMINOS[piece][ri];
}
// Reset piece and let program to create new one
piece = -1;
}
// Destruct all fully filled lines
int destructedLines = 0;
def::Vector2i p;
for (p.y = 0; p.y < fieldSize.y; p.y++)
{
bool fullyFilled = true;
// Find fully filled line
for (p.x = 0; p.x < fieldSize.x; p.x++)
{
char c = field[p.y * fieldSize.x + p.x];
if (c == ' ' || (c == '#' && p.x != 0 && p.x != fieldSize.x - 1))
{
fullyFilled = false;
break;
}
}
if (fullyFilled)
{
// Now destruct current line and move
// other pieces that are above one cell down
for (int y = p.y; y > 0; y--)
{
// Grab data from next line
std::string data = field.substr((y - 1) * fieldSize.x, fieldSize.x - 1);
// and put it on the current one
field.replace(y * fieldSize.x, data.size(), data);
}
destructedLines++;
}
}
// Add score based on amount of destroyed lines
switch (destructedLines)
{
case 1: score += 40; break;
case 2: score += 100; break;
case 3: score += 300; break;
case 4: score += 1200; break;
}
// Draw field on the screen
for (p.y = 0; p.y < fieldSize.y; p.y++)
for (p.x = 0; p.x < fieldSize.x; p.x++)
{
def::Pixel col = GetColor(field[p.y * fieldSize.x + p.x]);
FillRectangle((p + fieldPos) * cellSize, cellSize, col);
}
// Draw falling piece
if (piece != -1)
DrawPiece(piecePos, piece, rotation, false, false);
// Draw next piece and it's borders
DrawPiece(fieldPos + def::Vector2i(2, fieldSize.y * 0.4f), nextPiece, 0, true, true, def::DARK_GREY);
// Draw score
FillRectangle(4, cellSize.y * 4 + 12, 8 * 9, 10, def::BLACK);
DrawString(4, cellSize.y * 4 + 12, "Score: " + std::to_string(score), def::WHITE);
return true;
}
};
int main()
{
Tetris app;
app.Construct(256, 240, 4, 4);
app.Run();
return 0;
}