-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetrisPiece.cpp
368 lines (282 loc) · 9.51 KB
/
TetrisPiece.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
//
// Created by Douglas Wilcox on 2019-03-30.
//
#include "TetrisPiece.h"
#include <Qt>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QKeyEvent>
#include <QDebug>
#include <QTimer>
#include <memory>
#include <iostream>
using namespace Qt;
using namespace std;
//constructor
TetrisPiece::TetrisPiece(QGraphicsScene *scene, PField & pTetrinoField, int x, int y, int gridsize)
: myScene(scene), myTetrinoField(pTetrinoField), myPosX(x), myPosY(y), startPosY(y), myGridSize(gridsize)
{
//connect
myMoveDownTimer = new QTimer();
connect(myMoveDownTimer, SIGNAL(timeout()), this, SLOT(moveDown())); //connect syntax example
myMoveDownTimer->start(1000); //how to set timer, duration
mySquares.reserve(16);
mySquares.resize(16);
myShape.reserve(16);
myShape.resize(16);
}
//destructor
TetrisPiece::~TetrisPiece() {
delete myMoveDownTimer;
}
void TetrisPiece::Spawn() {
//draw rect
setRect(0, 0, 0, 0);
setPos(myPosX, myPosY);
this->setFlag(QGraphicsItem::ItemIsFocusable);
this->setFocus();
myScene->addItem(this);
//random generator for piece and color
std::random_device r;
std::default_random_engine e1(r());
std::uniform_int_distribution<int> uniform_dist(0, 6);
int piece = uniform_dist(e1);
int color = uniform_dist(e1);
myRot = 0;
switch (piece)
{
case 0:
myShape = {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0};
break;
case 1:
myShape = {0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0};
break;
case 2:
myShape = {0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0};
break;
case 3:
myShape = {0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0};
break;
case 4:
myShape = {0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0};
break;
case 5:
myShape = {0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0};
break;
case 6:
myShape = {0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0};
break;
}
for(int idx = 0; idx < 16; idx++)
{
if (myShape[idx])
{
mySquares[idx] = make_unique<QGraphicsRectItem>();
// mySquares[idx] = new QGraphicsRectItem();
mySquares[idx]->setRect(0, 0, myGridSize, myGridSize);
//assign active/static colors
switch (color)
{
case 0:
mySquares[idx]->setBrush(Qt::green); //darkGreen
myColor = Qt::green;
myStaticColor = Qt::darkGreen;
break;
case 1:
mySquares[idx]->setBrush(Qt::red); //darkRed
myColor = Qt::red;
myStaticColor = Qt::darkRed;
break;
case 2:
mySquares[idx]->setBrush(Qt::blue); //darkBlue
myColor = Qt::blue;
myStaticColor = Qt::darkBlue;
break;
case 3:
mySquares[idx]->setBrush(Qt::lightGray); //gray, DarkGray
myColor = Qt::lightGray;
myStaticColor = Qt::darkGray;
break;
case 4:
mySquares[idx]->setBrush(Qt::yellow); //darkYellow
myColor = Qt::yellow;
myStaticColor = Qt::darkYellow;
break;
case 5:
mySquares[idx]->setBrush(Qt::cyan); //darkCyan
myColor = Qt::cyan;
myStaticColor = Qt::darkCyan;
break;
case 6:
mySquares[idx]->setBrush(Qt::magenta); //darkMagenta
myColor = Qt::magenta;
myStaticColor = Qt::darkMagenta;
break;
}
mySquares[idx]->setParentItem(this);
//calculate position correctly
int xOffset = idx % 4;
int yOffset = idx / 4;
mySquares[idx]->setPos(xOffset * myGridSize - 2*myGridSize, yOffset * myGridSize - 2*myGridSize);
}
}
//initialize bools
isActive = true;
doCountDropRows = false;
myDropRows = 0;
endGame = false;
}
int TetrisPiece::GetRotatedPieceIndex(int gridX, int gridY, int r) {
int whichPieceIndex = 0;
switch (r % 4)
{
case 0: // 0 degrees // 0 1 2 3
whichPieceIndex = gridY * 4 + gridX; // 4 5 6 7
break; // 8 9 10 11
//12 13 14 15
case 1: // 90 degrees //12 8 4 0
whichPieceIndex = 12 + gridY - (gridX * 4); //13 9 5 1
break; //14 10 6 2
//15 11 7 3
case 2: // 180 degrees //15 14 13 12
whichPieceIndex = 15 - (gridY * 4) - gridX; //11 10 9 8
break; // 7 6 5 4
// 3 2 1 0
case 3: // 270 degrees // 3 7 11 15
whichPieceIndex = 3 - gridY + (gridX * 4); // 2 6 10 14
break; // 1 5 9 13
} // 0 4 8 12
return whichPieceIndex;
}
void TetrisPiece::keyPressEvent(QKeyEvent *event) {
QGraphicsItem::keyPressEvent(event);
if (event->key() == Key_Left){
if (CheckMove( myPosX - myGridSize, myPosY, myRot))
{
setPos(x() - myGridSize, y());
myPosX -= myGridSize;
}
} else if (event->key() == Key_Right)
{
if (CheckMove( myPosX + myGridSize, myPosY, myRot))
{
setPos(x() + myGridSize, y());
myPosX += myGridSize;
}
}
// else if (event->key() == Key_Up)
// {
//
// if (CheckMove( pos().x(), pos().y() - 10, myRot))
// {
// setPos(x(), y() - 10);
// myPosY -= 10;
// }
//
//
// }
else if (event->key() == Key_Down)
{
moveDown();
} else if (event->key() == Key_Z)
{
if (CheckMove( myPosX, myPosY, (myRot + 1) % 4))
{
setRotation(rotation() + 90);
myRot = (myRot + 1) % 4;
}
} else if (event->key() == Key_Space)
{
Drop();
} else if (event->key() == Key_Q)
{
qApp->exit();
}
}
void TetrisPiece::moveDown() {
if (isActive)
{
if (CheckMove( myPosX, myPosY + myGridSize, myRot))
{
setPos(x(), y() + myGridSize);
myPosY += myGridSize;
if (doCountDropRows)
{
myDropRows++;
}
} else{
// can't move down - plant this piece and spawn a new one
isActive = false;
//update field for collision
FixPiece();
if (myPosY - startPosY < myGridSize) //game over
{
endGame = true;
}
}
}
}
void TetrisPiece::UpdateTempField() {
myTetrinoField.ResetTempIntField();
for(int x = 0; x < myShape.size(); x++) //x steps through this tetrino int shape
{
int rPosX = x % 4;
int rPosY = x / 4;
int indexRotated = GetRotatedPieceIndex(rPosX, rPosY, myRot);
// Get index into field zero rot gridY * 4 + gridX
int fieldindex = ((myPosY-myGridSize)/myGridSize + rPosY) * myTetrinoField.GetFieldWidth() + ((myPosX -2*myGridSize)/myGridSize + rPosX);
if (myShape[indexRotated] == 1)
{
myTetrinoField.PutTempIntField(fieldindex);
}
}
}
//test this piece against field if moved or rotated
bool TetrisPiece::CheckMove(int posX, int posY, int rot) {
for(int x = 0; x < myShape.size(); x++) //x steps through this tetrino grid
{
int rPosX = x % 4;
int rPosY = x / 4;
int indexRotated = GetRotatedPieceIndex(rPosX, rPosY, rot);
// Get index into field zero rot gridY * 4 + gridX
int fieldindex = ((posY-2*myGridSize)/myGridSize + rPosY) * myTetrinoField.GetFieldWidth() + ((posX -2*myGridSize)/myGridSize + rPosX);
if (myShape[indexRotated] == 1 && myTetrinoField.CheckIntField(fieldindex) >= 1)
{
return false;
}
}
return true;
}
bool TetrisPiece::getIsActive() const {
return isActive;
}
void TetrisPiece::FixPiece() {
for(int x = 0; x < myShape.size(); x++) //x steps through this tetrino grid
{
int rPosX = x % 4;
int rPosY = x / 4;
int indexRotated = GetRotatedPieceIndex(rPosX, rPosY, myRot);
// Get index into field zero rot gridY * 4 + gridX
int fieldindex = ((myPosY-2*myGridSize)/myGridSize + rPosY) * myTetrinoField.GetFieldWidth() + ((myPosX -2*myGridSize)/myGridSize + rPosX);
if (myShape[indexRotated] == 1 ) //this is a piece that needs to be moved to the board and myIntField
{
myTetrinoField.PutPieceToField(fieldindex, myStaticColor);
}
}
//remove master rect
scene()->removeItem(this);
myTetrinoField.CheckForFilledRow();
}
void TetrisPiece::Drop() {
myMoveDownTimer->setInterval(35);
doCountDropRows = true;
}
bool TetrisPiece::isEndGame() const {
return endGame;
}
int TetrisPiece::getMyDropRows() const {
return myDropRows;
}
void TetrisPiece::SetMoveDownTimer(int time) {
myMoveDownTimer->setInterval(time);
}