-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetrisboard.cpp
380 lines (347 loc) · 9.04 KB
/
tetrisboard.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <QtWidgets>
#include <iostream>
#include "tetrisboard.h"
#include "tetrispiece.h"
using namespace std;
TetrisBoard::TetrisBoard(QWidget *parent)
: QFrame(parent)
{
setFrameStyle(QFrame::StyledPanel);
setFocusPolicy(Qt::StrongFocus);
started = false;
after_block = false;
for (int i = 0; i < 26; i++) //26 op 16 + speeldomein : 23 op 11 maar de rest zijn borders
{
vector<int> temporary;
for (int j = 0; j <16; j++)
{
temporary.push_back(0);
}
board.push_back(temporary);
}
}
float TetrisBoard::Get_Spm() const
{
return spm;
}
void TetrisBoard::start()
{
started = true;
lines = 0;
score = 0;
keystrokes = 0;
spm = 0;
setFocusPolicy(Qt::StrongFocus);
emit lines_changed(lines);
emit score_changed(score);
for (int i = 0; i < 26; i++)
{
for (int j = 0; j <16; j++)
{
board[i][j] = 0;
}
}
new_piece();
update();
timer.start(125, this); //timeout is in milliseconds
}
QSize TetrisBoard:: frameSize() const //moet met getallen zijn.
{
return QSize(220 + 2 , 420 + 2);
}
QSize TetrisBoard::minimumSizeHint() const
{
return QSize(220 + 2 , 420 + 2);
}
QSize TetrisBoard::maximumSize() const
{
return QSize(220 + 2 , 420 + 2);
}
void TetrisBoard::paintEvent(QPaintEvent *event)
{
QFrame::paintEvent(event);
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
vector<QColor> color_vector ;
color_vector.push_back(Qt::blue);
color_vector.push_back(Qt::red);
color_vector.push_back(Qt::yellow);
color_vector.push_back(Qt::green);
color_vector.push_back(Qt::magenta);
color_vector.push_back(Qt::cyan);
color_vector.push_back(Qt::gray);
QRect rect = contentsRect();
for (int i = 0; i < 11; ++i)
{
for (int j = 0; j < 23; ++j)
{
if(board[j+3][i+3] != 0)
{
painter.fillRect( (rect.left() + i * 20) + 1, (j * 20) + 1, (20 - 2) , (20 - 2),color_vector[board[j+3][i+3] -1]);
}
}
}
}
void TetrisBoard::keyPressEvent(QKeyEvent *event)
{
if (started == false || event->isAutoRepeat() )
{
QFrame::keyPressEvent(event); // pass to standard keypressevent handler
return;
}
keystrokes ++;
switch (event->key())
{
case Qt::Key_Left:
move_piece(-1,0);
repaint();
break;
case Qt::Key_Right:
move_piece(1,0);
repaint();
break;
case Qt::Key_Up:
rotate();
repaint();
break;
default:
QFrame::keyPressEvent(event);
}
}
void TetrisBoard::timerEvent(QTimerEvent *event)
{
if (Q_LIKELY(event->timerId() == timer.timerId())) //om de juiste timer te hebben, een beetje overkill.
{
if (after_block)
{
after_block = false;
if(upper_line_check())
{
started = false;
}
else
{
delete_lines();
repaint();
new_piece();
update();
}
}
else
{
gravity();
repaint();
}
static int counter = 0;
static float counter2 = 0;
counter++;
if(counter == 80) //every 10 seconds
{
counter = 0;
counter2++;
spm = keystrokes*(6/counter2);
}
}
else
{
QFrame::timerEvent(event);
}
}
bool TetrisBoard::event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
{
cout << "Gesture Detected" <<endl;
return gestureEvent(static_cast<QGestureEvent*>(event));
}
return QWidget::event(event);
}
bool TetrisBoard::gestureEvent(QGestureEvent *event)
{
if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
swipeTriggered(static_cast<QSwipeGesture *>(swipe));
return true;
}
void TetrisBoard::swipeTriggered(QSwipeGesture *gesture)
{
if (gesture->state() == Qt::GestureFinished) {
if (gesture->horizontalDirection() == QSwipeGesture::Left)
switch (gesture->horizontalDirection())
{
case QSwipeGesture::Left:
move_piece(-1,0);
repaint();
break;
case QSwipeGesture::Right:
move_piece(1,0);
repaint();
break;
case QSwipeGesture::Up:
rotate();
repaint();
break;
default:
break;
}
}
}
void TetrisBoard::new_piece()
{
current_piece.Set_Type(qrand() % 7);
current_piece.Set_x(5);
current_piece.Set_y(2);
store_piece_in_play();
}
int TetrisBoard::move_piece(int offset_x, int offset_y)
{
int Px = current_piece.Get_x();
int Py = current_piece.Get_y();
erase_piece_in_play();
current_piece.Set_x(Px + offset_x);
current_piece.Set_y(Py + offset_y);
int state = legal_place();
if (state== 0 || state== 1 || state== 2)
{
current_piece.Set_x(Px);
current_piece.Set_y(Py);
}
store_piece_in_play();
return state;
}
int TetrisBoard::rotate()
{
erase_piece_in_play();
current_piece.Rotate();
int state = legal_place();
if (state== 0 || state== 1 || state== 2)
{
current_piece.Rotate();
current_piece.Rotate();
current_piece.Rotate();
}
store_piece_in_play();
return state;
}
int TetrisBoard::gravity()
{
int Py = current_piece.Get_y();
erase_piece_in_play();
current_piece.Set_y(Py + 1);
int state = legal_place();
if(state == 1 || state== 2)
{
current_piece.Set_y(Py);
store_piece_in_play();
after_block = true;
}
else
{
store_piece_in_play();
}
return state;
}
void TetrisBoard::store_piece_in_play ()
{
int Px = current_piece.Get_x();
int Py = current_piece.Get_y();
for(int i = 0; i<5; i++)
{
for(int j = 0; j<5; j++)
{
if(current_piece.Get_Form()[i][j] != 0)
{
board[Py+i][Px+j] = current_piece.Get_Form()[i][j];
}
}
}
}
void TetrisBoard::erase_piece_in_play()
{
int Px = current_piece.Get_x();
int Py = current_piece.Get_y();
for (int i = Px, k = 0; i < Px + 5; i++, k++)
{
for (int j = Py, l = 0; j < Py + 5; j++, l++)
{
if(current_piece.Get_Form()[l][k] != 0)
{
board[j][i] = 0;
}
}
}
}
int TetrisBoard::legal_place()
{
int Px = current_piece.Get_x();
int Py = current_piece.Get_y();
for (int i = Px, k = 0; i < Px + 5; i++, k++) // k en l houden bij waar in de block dat het probleem zich stelt
{
for (int j = Py, l = 0; j < Py + 5; j++, l++)
{
//controleren op de randen
if ( i < 3 || i > 13)
{//controleren op de juiste spot in de block structuur
if(current_piece.Get_Form()[l][k] != 0){return 0;}
}
if ( j > 23)
{//controleren op de juiste spot in de block structuur
if(current_piece.Get_Form()[l][k] != 0){return 1;}
}
//controleren op botsing met andere blokjes
if (Q_LIKELY(j > 0))
{//controleren op de juiste spot in de block structuur
if ((current_piece.Get_Form()[l][k] != 0)&&(board[j][i]!=0)){return 2;}
}
}
}
return 3; // alles ok
}
bool TetrisBoard::upper_line_check()
{
for (int i = 3; i < 14; i++)
{
if (board[5][i] != 0)
{
return true;
}
}
return false; //true: is game over
}
void TetrisBoard::delete_line(int line)
{
for (int i = line; i > 0; i--)
{
for (int j = 3; j < 14; j++)
{
board[i][j] = board[i-1][j];
}
}
}
int TetrisBoard::delete_lines()
{
int lines_local = 0;
for (int i = 4; i < 24; i++)
{
int z = 0;
for (int j = 3; j < 14; j++)
{
if (board[i][j] == 0) {break;}
z++;
}
if (z == 11)
{
delete_line(i);
lines_local++;
}
}
lines = lines + lines_local;
if (lines_local % 4 == 0 && Q_LIKELY(lines_local != 0)){score = score + 4*lines_local;}
else {score = score + lines_local;}
emit lines_changed(lines);
emit score_changed(score);
if (lines > 19)
{
started = false;
}
return lines;
}