forked from hBuzzy/TetrisSample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamefield.cpp
More file actions
103 lines (79 loc) · 2.94 KB
/
Copy pathgamefield.cpp
File metadata and controls
103 lines (79 loc) · 2.94 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
#include "gamefield.h"
#include <QDebug>
#include <QKeyEvent>
#include <QPainter>
GameField::GameField(QWidget *parent) : QWidget{parent} {
setFocusPolicy(Qt::StrongFocus);
// Устанавливаем асинхронное соединение
connect(this, &GameField::InitialisationStarted, this, &GameField::SetCells,
Qt::QueuedConnection);
// Вызываем отложенный сигнал
emit InitialisationStarted();
}
// Геттер свойства rowsCount
uint GameField::GetRowsNumber() const { return rowsCount_; }
// Сеттер свойства rowsCount
void GameField::SetRowsNumber(uint rowsCount) {
if (rowsCount_ == rowsCount) return;
rowsCount_ = rowsCount;
emit RowsNumberChanged();
}
// Геттер свойства columnsCount
uint GameField::GetColumnsNumber() const { return columnsCount_; }
// Сеттер свойства columnsCount
void GameField::SetColumnsNumber(uint columnsCount) {
if (columnsCount_ == columnsCount) return;
columnsCount_ = columnsCount;
emit ColumnsNumberChanged();
}
void GameField::paintEvent(QPaintEvent *event) {
Q_UNUSED(event)
QPainter painter(this);
painter.setPen(QPen(Qt::white, 1, Qt::SolidLine));
DrawCells(&painter);
}
void GameField::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Space) {
qDebug() << "space pressed";
}
}
void GameField::SetCells() {
// Задаем развер вектора согласно rowsCount_
cellsColors_.resize(rowsCount_);
for (uint i = 0; i < rowsCount_; i++) {
// Изменяем размер вложенного вектора согласно columnsCount_
cellsColors_[i].resize(columnsCount_);
}
ResetCellsColor(); // Перекрашиваем ячейки в базовый цвет
setFixedSize(GetSize()); // Задам разер виджта по размру игровой сетки
}
void GameField::StartNewGame() {
setFocus(); // Устанавливаем фокус на виджет
qDebug() << "new game started";
}
void GameField::ResetCellsColor() {
for (uint i = 0; i < rowsCount_; i++) {
// Заполняем весь вектор базовым цветом
cellsColors_[i].fill(kCellDefaultColor);
}
}
// Получаем размер игровой сетки
QSize GameField::GetSize() const {
return QSize(columnsCount_, rowsCount_) * kCellSize;
}
void GameField::DrawCells(QPainter *painter) {
int positionX = 0;
int positionY = 0;
for (uint i = 0; i < rowsCount_; i++) {
for (uint j = 0; j < columnsCount_; j++) {
// Рисуем ячейку ее цветом, заданным в cellsColors_
painter->setBrush(cellsColors_[i][j]);
// Рисуем каждую ячейку со смещением
painter->drawRect(positionX, positionY, positionX + kCellSize,
positionY + kCellSize);
positionX += kCellSize;
}
positionX = 0;
positionY += kCellSize;
}
}