-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.cpp
84 lines (76 loc) · 2.38 KB
/
box.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
#include "box.h"
Box::Box(QWidget *parent) : QWidget(parent){
block.clear();
shadowBlock.clear();
memset(box, 0, sizeof (box));
int w = 30*10 + 4*(10-1);
int h = 30*20 + 4*(20-1);
setFixedSize(w, h);
}
void Box::updateTetris(Tetris tetris){
block = tetris.getBlock();
shadowBlock = tetris.getShadowBlock();
for (int i = 0; i < 10; i++){
for (int j = 0; j < 20; j++){
box[i][j] = tetris.getBox(i, j);
}
}
repaint();
}
void Box::paintEvent(QPaintEvent *event){
QPainter painter(this);
painter.setPen(Qt::black);
painter.drawRect(this->rect().x(),this->rect().y(),this->rect().width()-1,this->rect().height()-1);
painter.setPen(Qt::white);
int w = 30*10 + 4*(10-1);
int h = 30*20 + 4*(20-1);
setPalette(QPalette(Qt::black));
setAutoFillBackground(true);
for (int i = 1; i < 10; i++){
painter.drawLine(i*34-2, 0, i*34-2, h);
}
for (int i = 1; i < 20; i++){
painter.drawLine(0, i*34-2, w, i*34-2);
}
QPen boxPen;
QBrush boxBrush;
boxPen.setStyle(Qt::SolidLine);
boxPen.setColor(Qt::gray);
boxBrush.setStyle(Qt::SolidPattern);
boxBrush.setColor(Qt::white);
painter.setPen(boxPen);
painter.setBrush(boxBrush);
for (int i = 0; i < 10; i++){
for (int j = 0; j < 20; j++){
if (box[i][j]){
painter.drawRect(i*34, j*34, 30, 30);
}
}
}
QPen blockPen;
QBrush blockBrush;
blockPen.setStyle(Qt::SolidLine);
blockPen.setColor(Qt::gray);
blockBrush.setStyle(Qt::SolidPattern);
blockBrush.setColor(block.getColor());
painter.setPen(blockPen);
painter.setBrush(blockBrush);
for (int i = 0; i < 4; i++){
int x = block.getX(i);
int y = block.getY(i);
painter.drawRect(x*34, y*34, 30, 30);
}
QPen shadowBlockPen;
QBrush shadowBlockBrush;
shadowBlockPen.setStyle(Qt::SolidLine);
shadowBlockPen.setColor(Qt::gray);
shadowBlockBrush.setStyle(Qt::SolidPattern);
shadowBlockBrush.setColor(QColor(shadowBlock.getColor().red(), shadowBlock.getColor().green(), shadowBlock.getColor().blue(), 150));
painter.setPen(shadowBlockPen);
painter.setBrush(shadowBlockBrush);
for (int i = 0; i < 4; i++){
int x = shadowBlock.getX(i);
int y = shadowBlock.getY(i);
painter.drawRect(x*34, y*34, 30, 30);
}
}