-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
196 lines (163 loc) · 5.8 KB
/
Copy pathmainwindow.cpp
File metadata and controls
196 lines (163 loc) · 5.8 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
#include <QFileDialog>
#include <QMessageBox>
#include <QKeyEvent> // Нажатия кнопок на клавиатуре
#include <QDebug> // Вывод в отладочную консоль
#include <QTimer>
#include <QPainter> // Рисование на форме
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "about.h"
#include "game.h"
#include "gamesettings.h"
void MainWindow::defaultShapesColors() {
brush.insert('_', QBrush(Qt::NoBrush)); // Для пустой клетки
brush.insert('I', QBrush(Qt::red));
brush.insert('J', QBrush(Qt::blue));
brush.insert('L', QBrush(Qt::magenta));
brush.insert('O', QBrush(Qt::gray));
brush.insert('S', QBrush(Qt::green));
brush.insert('T', QBrush(Qt::cyan));
brush.insert('Z', QBrush(QColor::fromRgb(153, 255, 0))); // lime
}
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);
game = new Game(settings.glassWidth(), settings.glassHeight());
defaultShapesColors();
timer = new QTimer(this);
// Соединяем сигнал со слотом
connect(timer,
SIGNAL(timeout()),
this,
SLOT(gameStep()));
game->timerInterval = 200;
timer->start(game->timerInterval);
QSize glassSize = ui->glass->maximumSize();
int borders = 2 * ui->glass->lineWidth() + 4;
glassSize.setWidth(game->width() * settings.cellSizeX() + borders);
glassSize.setHeight(game->height() * settings.cellSizeX() + borders);
ui->glass->setMinimumSize(glassSize);
ui->glass->setMaximumSize(glassSize);
game->start();
}
MainWindow::~MainWindow() {
delete game;
delete ui;
}
void MainWindow::on_about_triggered() {
About about;
// Модальное окно - пока не закроешь,
// остальное приложение блокируется
about.exec();
}
void MainWindow::on_saveGame_triggered() {
QString filename = QFileDialog::getSaveFileName(
this, "Сохранить игру", "",
"Тетрис (*.tetris)");
if(filename.isEmpty())
return;
try {
game->save(filename);
} catch(QString error) {
// critical(QWidget * parent, const QString & title,
// const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
QMessageBox::critical(this, "Ошибка при сохранении файла", error);
}
}
void MainWindow::on_loadGame_triggered() {
QString filename = QFileDialog::getOpenFileName(
this, "Загрузить игру", "",
"Тетрис (*.tetris)");
try {
game->load(filename);
} catch(QString error) {
QMessageBox::critical(this, "Ошибка при чтении файла", error);
}
//updateWindow();
}
void MainWindow::on_gameSettings_triggered() {
GameSettings form(this, settings);
form.exec();
}
// При нажатии на кнопку на клавиатуре
void MainWindow::keyPressEvent(QKeyEvent* event) {
switch (event->key()) {
case Qt::Key_Left: // Двигаем фигуру влево
qDebug() << "Left";
game->move(-1, 0);
update();
break;
case Qt::Key_Right: // Двигаем фигуру вправо
qDebug() << "Right";
game->move(+1, 0);
update();
break;
case Qt::Key_Down: // Приземляем фигуру
qDebug() << "Down - landed figure";
game->move(0, +1);
update();
break;
case Qt::Key_Up: // Поворачиваем фигуру
qDebug() << "Up - rotate";
game->rotate();
update();
break;
case Qt::Key_Space: // Пробел - пауза
if(game->paused) {
qDebug() << "Resume the game";
timer->start();
game->paused = false;
} else {
qDebug() << "Game pause";
timer->stop();
game->paused = true;
}
break;
}
// Вызываем стандартный обработчик нажатий
QMainWindow::keyPressEvent(event);
}
void MainWindow::gameStep() {
// Один шаг игры (движение фигуры вниз)
// + проверка на столкновения
// Уничтожение заполненных строк
// Появление новой фигуры
// Шаг игры
game->step();
// Обновление статистики игры: счёта, количества строк
ui->Score->setText(QString("%1").arg(game->score));
ui->Lines->setText(QString("%1").arg(game->lines));
ui->Level->setText(QString("%1").arg(game->level));
ui->TimerInterval->setText(QString("%1").arg(game->timerInterval));
// Обновляем период работы таймера
timer->setInterval(game->timerInterval);
// Перерисовка для движения фигур
update();
}
// Отрисовка поля игры
void MainWindow::paintEvent(QPaintEvent*) {
QPainter p(this);
const int cellBorder = settings.cellBorder();
const int cellSize = settings.cellSize();
const int cellSizeX = settings.cellSizeX();
QRect g1 = ui->centralWidget->geometry();
QRect g2 = ui->glass->geometry();
const int xLeft = g1.left() + g2.left() + ui->glass->lineWidth() + 1;
const int yTop = g1.top() + g2.top() + ui->glass->lineWidth() + 1;
// Рисуем стакан
p.drawRect(QRect(xLeft, yTop,
game->width() * cellSizeX,
game->height() * cellSizeX));
// Рисуем клетки
// Направим ось x вправо
// Ось y - вниз
for(int i = 0; i < game->height(); ++i)
for(int j = 0; j < game->width(); ++j) {
p.setBrush(brush[game->glass[i][j]]);
p.drawRoundedRect(
QRect(xLeft + j * cellSizeX + cellBorder,
yTop + i * cellSizeX + cellBorder,
cellSize, cellSize), 2, 2);
}
}