This repository has been archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
167 lines (131 loc) · 4.7 KB
/
MainWindow.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
#include <QPainter>
#include "MainWindow.h"
MainWindow::MainWindow ( const int min_width, const int min_height, QMainWindow * parent ) : QMainWindow(parent)
{
const auto timeout_time = 200;
connect(&timer, SIGNAL(timeout()), this, SLOT(updateStates()));
timer.start(timeout_time);
current_shape.setCoordinate(default_x, default_y);
current_shape.setType(FigureType::I_type);
setFixedSize(min_width, min_height);
}
void MainWindow::updateStates ( )
{
auto checkCollision = [&]()
{
const auto x = current_shape.coord().x();
const auto y = current_shape.coord().y() + 1;
for (auto& shape : fixed_shapes)
{
if (shape.coord().x() == x && shape.coord().y() == y)
{
return true;
}
}
return false;
};
// If current shape reached end of game field or other shape than fixed her
if (current_shape.coord().y() == row_count - 1 || checkCollision())
{
fixed_shapes.append(current_shape);
current_shape.setCoordinate(default_x, default_y);
}
else
{
const auto next_x = current_shape.coord().x();
const auto next_y = current_shape.coord().y() + 1;
current_shape.setCoordinate(next_x, next_y);
}
update();
}
void MainWindow::drawShape ( QPainter& painter, Shape& shape )
{
const auto x = shape.coord().x() * side_size + delta_x;
const auto y = shape.coord().y() * side_size + delta_y;
const auto rect_width = side_size;
const auto rect_height = side_size;
QBrush brush(Qt::red);
painter.setBrush(brush);
painter.drawRect(x, y, rect_width, rect_height);
}
void MainWindow::paintEvent ( QPaintEvent * event )
{
QPainter painter(this);
drawField(painter);
drawInfo (painter);
drawShape(painter, current_shape);
drawFixedShapes(painter);
drawGrid(painter);
}
void MainWindow::drawInfo ( QPainter& painter )
{
/* Function draw information about scores count at screen */
const auto x = delta_x + row_count * side_size + delta_x;
const auto y = delta_y;
const auto rect_width = 200;
const auto rect_height = 50;
QRect drawing_rect(x, y, rect_width, rect_height);
const auto point_size = 18;
QFont font = painter.font();
font.setPointSize(point_size);
painter.setFont(font);
QTextOption option = QTextOption(Qt::AlignHCenter);
painter.drawText(drawing_rect, "Scores", option);
}
void MainWindow::drawFixedShapes ( QPainter& painter )
{
for (auto& shape : fixed_shapes)
{
drawShape(painter, shape);
}
}
void MainWindow::drawField ( QPainter& painter )
{
/* Draw game field border at screen */
/* x coordinate */ /* y coordinate */
const QPoint up_left (0 + delta_x, 0 + delta_y);
const QPoint up_right (0 + delta_x + side_size * column_count, 0 + delta_y);
const QPoint down_left (0 + delta_x, side_size * row_count + delta_y);
const QPoint down_right(0 + delta_x + side_size * column_count, side_size * row_count + delta_y);
painter.drawLine(up_left, up_right);
painter.drawLine(up_left, down_left);
painter.drawLine(down_left, down_right);
painter.drawLine(down_right, up_right);
}
void MainWindow::drawGrid ( QPainter& painter )
{
/* Draw game field grid at screen */
// Vertical
for (auto i = 1; i < column_count; i++)
{
const QPoint up (delta_x + i * side_size, delta_y);
const QPoint down(delta_x + i * side_size, side_size * row_count + delta_y);
painter.drawLine(up, down);
}
// Horizontal
for (auto i = 1; i < row_count; i++)
{
const QPoint left (delta_x, i * side_size + delta_y);
const QPoint right(side_size * column_count + delta_x, i * side_size + delta_y);
painter.drawLine(left, right);
}
}
void MainWindow::keyPressEvent ( QKeyEvent* event )
{
const auto pressed_key = event->key();
const auto shape_coords = current_shape.coord();
auto current_shape_x = shape_coords.x();
switch (pressed_key)
{
case Qt::Key_Left:
current_shape_x = current_shape_x - 1 < 0 ? current_shape_x : current_shape_x - 1;
current_shape.setCoordinate(current_shape_x, shape_coords.y());
break;
case Qt::Key_Right:
current_shape_x = current_shape_x + 1 > column_count - 1 ? current_shape_x : current_shape_x + 1;
current_shape.setCoordinate(current_shape_x, shape_coords.y());
break;
case Qt::Key_Down:
break;
}
}