From ab4baa34e0630e47413666e0594fd08a6133c9d3 Mon Sep 17 00:00:00 2001 From: MuraBlinova <99964993+MuraBlinova@users.noreply.github.com> Date: Thu, 21 Dec 2023 16:58:09 +0300 Subject: [PATCH] Add files via upload --- Task3_Tetris.pro | 29 ++++ Task3_Tetris.pro.user | 266 +++++++++++++++++++++++++++++++ figure.cpp | 120 ++++++++++++++ figure.h | 41 +++++ griddrawer.cpp | 82 ++++++++++ griddrawer.h | 38 +++++ main.cpp | 13 +- mainwindow.cpp | 74 +++++++++ mainwindow.h | 25 +++ nextfigure.cpp | 67 ++++++++ nextfigure.h | 30 ++++ tetris.cpp | 356 +++++++++++++++++++++++++++++++++++++++++- tetris.h | 57 +++++-- 13 files changed, 1174 insertions(+), 24 deletions(-) create mode 100644 Task3_Tetris.pro create mode 100644 Task3_Tetris.pro.user create mode 100644 figure.cpp create mode 100644 figure.h create mode 100644 griddrawer.cpp create mode 100644 griddrawer.h create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 nextfigure.cpp create mode 100644 nextfigure.h diff --git a/Task3_Tetris.pro b/Task3_Tetris.pro new file mode 100644 index 0000000..9041ee5 --- /dev/null +++ b/Task3_Tetris.pro @@ -0,0 +1,29 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + figure.cpp \ + griddrawer.cpp \ + main.cpp \ + mainwindow.cpp \ + nextfigure.cpp \ + tetris.cpp + +HEADERS += \ + figure.h \ + griddrawer.h \ + mainwindow.h \ + nextfigure.h \ + tetris.h + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/Task3_Tetris.pro.user b/Task3_Tetris.pro.user new file mode 100644 index 0000000..5f3f973 --- /dev/null +++ b/Task3_Tetris.pro.user @@ -0,0 +1,266 @@ + + + + + + EnvironmentId + {df4a09b1-8411-4997-95c2-b475eb81ec4c} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + true + true + *.md, *.MD, Makefile + false + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 4 + true + + + + true + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Qt 6.6.0 for macOS + Qt 6.6.0 for macOS + qt.qt6.660.clang_64_kit + 0 + 0 + 0 + + 0 + /Users/Mura/build-Task3_Tetris-Qt_6_6_0_for_macOS-Debug + /Users/Mura/build-Task3_Tetris-Qt_6_6_0_for_macOS-Debug + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Отладка + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + /Users/Mura/build-Task3_Tetris-Qt_6_6_0_for_macOS-Release + /Users/Mura/build-Task3_Tetris-Qt_6_6_0_for_macOS-Release + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Выпуск + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + + + 0 + /Users/Mura/build-Task3_Tetris-Qt_6_6_0_for_macOS-Profile + /Users/Mura/build-Task3_Tetris-Qt_6_6_0_for_macOS-Profile + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Профилирование + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + 0 + + 3 + + + 0 + Развёртывание + Развёртывание + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + true + + 2 + + false + Qt4ProjectManager.Qt4RunConfiguration:/Users/Mura/Task3_Tetris/Task3_Tetris.pro + /Users/Mura/Task3_Tetris/Task3_Tetris.pro + true + true + true + /Users/Mura/build-Task3_Tetris-Qt_6_6_0_for_macOS-Debug/Task3_Tetris.app/Contents/MacOS + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/figure.cpp b/figure.cpp new file mode 100644 index 0000000..84bfa50 --- /dev/null +++ b/figure.cpp @@ -0,0 +1,120 @@ +#include "figure.h" + +figure::figure() +{ + m_x = 0; + m_y = 0; + m_condition = 0; + m_color = QColor(Qt::red); + m_data = {{{1}}}; +} + +int figure::x() const +{ + return m_x; +} + +void figure::setX(int newX) +{ + m_x = newX; +} + +int figure::y() const +{ + return m_y; +} + +void figure::setY(int newY) +{ + m_y = newY; +} + +int figure::condition() const +{ + return m_condition; +} + +void figure::setCondition(int newCondition) +{ + m_condition = newCondition; +} + +QColor figure::color() const +{ + return m_color; +} + +void figure::setColor() +{ + QRandomGenerator *rg = QRandomGenerator::global(); + int randomNumber = rg->bounded(0, 6); + + QVector colorRand = {Qt::red, Qt::blue, Qt::green, Qt::yellow, Qt::cyan, Qt::magenta}; + m_color = colorRand[randomNumber]; +} + +void figure::setSpecificColor(QColor color_) +{ + m_color = color_; +} + +QVector>> figure::data() const +{ + return m_data; +} + +void figure::setData() +{ + QRandomGenerator *rg = QRandomGenerator::global(); + int randomNumber = rg->bounded(1, 7); + switch (randomNumber) { + case 1: + m_data = {{{1,1,1,1}}, {{1},{1},{1},{1}}}; + break; + case 2: + m_data = {{{1,0},{1,1},{1,0}}, + {{1,1,1},{0,1,0}}, + {{0,1},{1,1},{0,1}}, + {{0,1,0},{1,1,1}} + }; + break; + case 3: + m_data = {{{1,1},{1,0},{1,0}}, + {{1,1,1},{0,0,1}}, + {{0,1},{0,1},{1,1}}, + {{1,0,0},{1,1,1}} + }; + break; + case 4: + m_data = {{{1,1},{0,1},{0,1}}, + {{0,0,1},{1,1,1}}, + {{1,0},{1,0},{1,1}}, + {{1,1,1},{1,0,0}} + }; + break; + case 5: + m_data = {{{1,0},{1,1},{0,1}}, + {{0,1,1},{1,1,0}} + }; + break; + case 6: + m_data = {{{0,1},{1,1},{1,0}}, + {{1,1,0},{0,1,1}} + }; + break; + default: + break; + } +} + +void figure::setSpecificData(QVector>> data_){ + m_data = data_; +} + +void figure::nextCondition(){ + setCondition((condition() + 1)%data().size()); +} + +void figure::prevCondition(){ + setCondition((condition() + data().size()- 1)%data().size()); +} diff --git a/figure.h b/figure.h new file mode 100644 index 0000000..8b83a4e --- /dev/null +++ b/figure.h @@ -0,0 +1,41 @@ +#ifndef FIGURE_H +#define FIGURE_H + +#include +#include +#include +#include + +class figure : public QObject +{ + Q_OBJECT + Q_PROPERTY(int x READ x WRITE setX) + Q_PROPERTY(int y READ y WRITE setY) + Q_PROPERTY(int condition READ condition WRITE setCondition) + +public: + figure(); + int x() const; + void setX(int newX); + int y() const; + void setY(int newY); + int condition() const; + void setCondition(int newCondition); + QColor color() const; + void setColor(); + QVector>> data() const; + void setData(); + void setSpecificColor(QColor color_); + void setSpecificData(QVector>> data_); + void nextCondition(); + void prevCondition(); + +private: + int m_x; + int m_y; + int m_condition; + QColor m_color; + QVector>> m_data; +}; + +#endif // FIGURE_H diff --git a/griddrawer.cpp b/griddrawer.cpp new file mode 100644 index 0000000..862ad04 --- /dev/null +++ b/griddrawer.cpp @@ -0,0 +1,82 @@ +#include "griddrawer.h" + +GridDrawer::GridDrawer(QWidget *parent):QWidget(parent), m_columns(3), m_rows(4), m_sizeCell(30) +{ + for (int i = 0; i < m_columns; i++) { + QVector cellsRow = {}; + for (int j = 0; j < m_rows; j++) { + cellsRow.push_back(Qt::gray); + } + cellColors.push_back(cellsRow); + } +} + +int GridDrawer::columns() const +{ + return m_columns; +} + +void GridDrawer::setColumns(int newColumns) +{ + m_columns = newColumns; + cellColors.clear(); + for (int i = 0; i < m_columns; i++) { + QVector cellsRow = {}; + for (int j = 0; j < rows(); j++) { + cellsRow.push_back(Qt::gray); + } + cellColors.push_back(cellsRow); + } +} + +int GridDrawer::rows() const +{ + return m_rows; +} + +void GridDrawer::setRows(int newRows) +{ + m_rows = newRows; + cellColors.clear(); + for (int i = 0; i < columns(); i++) { + QVector cellsRow = {}; + for (int j = 0; j < m_rows; j++) { + cellsRow.push_back(Qt::gray); + } + cellColors.push_back(cellsRow); + } +} + +int GridDrawer::sizeCell() const +{ + return m_sizeCell; +} + +void GridDrawer::setSizeCell(int newSizeCell) +{ + m_sizeCell = newSizeCell; +} + +QVector> GridDrawer::getCellColors() const +{ + return cellColors; +} + +void GridDrawer::setCellColors(const QVector > &newGetCellColors) +{ + cellColors = newGetCellColors; +} + +void GridDrawer::draw(QPainter *painter) { + int cellWidth = m_sizeCell; + int cellHeight = m_sizeCell; + + for (int i = 0; i < m_columns; i++) { + for (int j = 0; j < m_rows; j++) { + painter->setPen(Qt::white); + painter->setBrush(cellColors[i][j]); + painter->drawRect(i * cellWidth, j * cellHeight, cellWidth, cellHeight); + } + } + this->update(); +} diff --git a/griddrawer.h b/griddrawer.h new file mode 100644 index 0000000..2a97009 --- /dev/null +++ b/griddrawer.h @@ -0,0 +1,38 @@ +#ifndef GRIDDRAWER_H +#define GRIDDRAWER_H + +#include +#include +#include + +class GridDrawer : public QWidget { + Q_OBJECT + Q_PROPERTY(int columns READ columns WRITE setColumns) + Q_PROPERTY(int rows READ rows WRITE setRows) + Q_PROPERTY(int sizeCell READ sizeCell WRITE setSizeCell) + +public: + GridDrawer(QWidget *parent = 0); + + int columns() const; + void setColumns(int newColumns); + + int rows() const; + void setRows(int newRows); + + int sizeCell() const; + void setSizeCell(int newSizeCell); + + QVector > getCellColors() const; + void setCellColors(const QVector > &newGetCellColors); + + void draw(QPainter *painter); + +private: + int m_columns; + int m_rows; + int m_sizeCell; + QVector> cellColors; +}; + +#endif // GRIDDRAWER_H diff --git a/main.cpp b/main.cpp index f1c8bd9..b48f94e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,11 @@ +#include "mainwindow.h" #include -#include "tetris.h" +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); -int main(int argc, char *argv[]) { - QApplication a(argc, argv); - Tetris w; - w.show(); - return a.exec(); + return a.exec(); } diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..b16a979 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,74 @@ +#include "mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + int score = 0; + tetris *tetris_ = new tetris(); + nextfigure *nextfigure_ = new nextfigure(); + + QPushButton *buttonNewGame = new QPushButton("Новая игра"); + QPushButton *buttonExit = new QPushButton("Выход"); + QLabel *labelScore = new QLabel("Cчетчик очков: " + QString::number(score)); + QLabel *labelGameOver = new QLabel(); + + connect(buttonNewGame, &QPushButton::clicked, tetris_, &tetris::newGame); + connect(buttonNewGame, &QPushButton::clicked, [=]() mutable{ + score = 0; + labelScore->setText("Cчетчик очков: " + QString::number(score)); + labelGameOver->setText(""); + }); + connect(buttonExit, &QPushButton::clicked, this, &QMainWindow::close); + connect(tetris_, &tetris::fullRow, [=]() mutable{ + score += 5; + labelScore->setText("Cчетчик очков: " + QString::number(score)); + }); + connect(tetris_, &tetris::gameFinished, [=]() mutable{ + labelGameOver->setText("ИГРА ОКОНЧЕНА"); + }); + connect(tetris_, &tetris::nextStep, nextfigure_, &nextfigure::nextStep); + connect(nextfigure_, &nextfigure::figureChanged, tetris_, &tetris::onFigureChanged); + + + QAction *newGameAction = new QAction("Начать новую игру", this); + connect(newGameAction, &QAction::triggered, tetris_, &tetris::newGame); + + QAction *exitAction = new QAction("Выйти", this); + connect(exitAction, &QAction::triggered, this, &QMainWindow::close); + + QAction *helpAction = new QAction("Справка", this); + connect(helpAction, &QAction::triggered, this, [=](){ + QMessageBox::information(this, "Справка", QString(u8"\u2193") + " - спускает фигуру на одну ячейку вниз\n" + QString(u8"\u2190") + " - сдвигает фигуру на одну ячейку влево\n" + QString(u8"\u2192") + " - сдвигает фигуру на одну ячейку вправо\n" + QString(u8"\u2191") + " - поворачивает фигуру\n Пробел - сбрасывает фигуру вниз"); + }); + + QMenu *gameMenu = menuBar()->addMenu("Игра"); + gameMenu->addAction(newGameAction); + gameMenu->addAction(exitAction); + + QMenu *helpMenu = menuBar()->addMenu("Справка"); + helpMenu->addAction(helpAction); + + QHBoxLayout *hLayout = new QHBoxLayout; + QVBoxLayout *vLayout = new QVBoxLayout; + hLayout->addWidget(tetris_); + vLayout->addWidget(nextfigure_); + vLayout->addWidget(buttonNewGame); + vLayout->addWidget(labelScore); + vLayout->addWidget(labelGameOver); + vLayout->addWidget(buttonExit); + hLayout->addLayout(vLayout); + + + setCentralWidget(new QWidget); + centralWidget()->setLayout(hLayout); + + GridDrawer *tetrisGrid = tetris_->getGrid(); + GridDrawer *nextFigureGrid = nextfigure_->getGridDrawer(); + int width_ = tetrisGrid->columns() * tetrisGrid->sizeCell() + nextFigureGrid->columns()*nextFigureGrid->sizeCell() + 250; + int hieght_ = tetrisGrid->rows() * tetrisGrid->sizeCell() + 300; + resize(width_, hieght_); + + tetris_->setFocus(); +} +MainWindow::~MainWindow() {} + diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..f1bba4e --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,25 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "griddrawer.h" +#include "tetris.h" +#include "nextfigure.h" + +class MainWindow : public QMainWindow { + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); +}; + +#endif // MAINWINDOW_H diff --git a/nextfigure.cpp b/nextfigure.cpp new file mode 100644 index 0000000..0d7f97b --- /dev/null +++ b/nextfigure.cpp @@ -0,0 +1,67 @@ +#include "nextfigure.h" + +nextfigure::nextfigure(QWidget *parent):QWidget(parent), grid(new GridDrawer()), figure_(new figure()) { + figure_->setData(); + figure_->setColor(); + int x = (4 - figure_->data()[0].size())/2; + figure_->setX(x); + int y = (4 - figure_->data()[0][0].size())/2; + figure_->setY(y); + + QVector> cellColors = grid->getCellColors(); + for(int i = 0; i < figure_->data()[0].size(); i++){ + for(int j = 0; j < figure_->data()[0][i].size(); j++){ + if(figure_->data()[0][i][j]){ + cellColors[i + x][j + y] = figure_->color(); + } + } + } + grid->setCellColors(cellColors); +} + +void nextfigure::paintEvent(QPaintEvent *event) { + QPainter painter(this); + grid->draw(&painter); + int height_ = grid->y()*grid->sizeCell() + 10; + painter.drawText(0, 150, "Следующая фигура"); + this->update(); +} + +GridDrawer *nextfigure::getGridDrawer() +{ + return grid; +} + +void nextfigure::nextStep() +{ + figure* nextFigure_ = new figure(); + nextFigure_->setSpecificColor(figure_->color()); + nextFigure_->setSpecificData(figure_->data()); + emit figureChanged(nextFigure_); + updateFigure(); +} + +void nextfigure::updateFigure(){ + figure_->setData(); + figure_->setColor(); + int maximumHeightFigure = 4; + int x = (maximumHeightFigure - figure_->data()[0].size())/2; + figure_->setX(x); + int y = (maximumHeightFigure - figure_->data()[0][0].size())/2; + figure_->setY(y); + + QVector> cellColors = grid->getCellColors(); + for(int i = 0; i < cellColors.size(); i++){ + for(int j = 0; j < cellColors[i].size(); j++){ + cellColors[i][j] = Qt::gray; + } + } + for(int i = 0; i < figure_->data()[0].size(); i++){ + for(int j = 0; j < figure_->data()[0][i].size(); j++){ + if(figure_->data()[0][i][j]){ + cellColors[i + x][j + y] = figure_->color(); + } + } + } + grid->setCellColors(cellColors); +} diff --git a/nextfigure.h b/nextfigure.h new file mode 100644 index 0000000..bffed8e --- /dev/null +++ b/nextfigure.h @@ -0,0 +1,30 @@ +#ifndef NEXTFIGURE_H +#define NEXTFIGURE_H + +#include "griddrawer.h" +#include "figure.h" +#include +#include +#include + +class nextfigure : public QWidget { + Q_OBJECT + +signals: + void figureChanged(figure* newFigure); + +public slots: + void nextStep(); + +public: + nextfigure(QWidget *parent = 0); + GridDrawer *getGridDrawer(); + void updateFigure(); +protected: + void paintEvent(QPaintEvent *event) override; +private: + GridDrawer* grid; + figure* figure_; +}; + +#endif // NEXTFIGURE_H diff --git a/tetris.cpp b/tetris.cpp index 50cf380..9980ad4 100644 --- a/tetris.cpp +++ b/tetris.cpp @@ -1,9 +1,357 @@ #include "tetris.h" -#include "ui_tetris.h" +tetris::tetris(QWidget *parent): QWidget(parent), grid(new GridDrawer()), figure_(new figure()) +{ + int maximumHeightFigure = 4; + gameOn = 1; + grid->setColumns(7); + grid->setRows(10); -Tetris::Tetris(QWidget *parent) : QMainWindow(parent), ui(new Ui::Tetris) { - ui->setupUi(this); + figure_->setData(); + figure_->setColor(); + int cond = figure_->condition(); + int x = (grid->columns() + 2 - figure_->data()[cond].size())/2; + figure_->setX(x); + int y = 2; + if(figure_->data()[cond][0].size() == maximumHeightFigure){ + y = 0; + } + figure_->setY(y); + qDebug() << x << " " << y; + + for(int i = 0; i < grid->columns()+2; i++){ + QVector vectorInt; + gridInt.push_back(vectorInt); + for(int j = 0; j < grid->rows()+5; j++){ + if(i == 0 || i == grid->columns() + 1 || j == grid->rows() + 4){ + gridInt[i].push_back(1); + }else{ + gridInt[i].push_back(0); + } + } + } + + for(int i = 0; i < figure_->data()[cond].size(); i++){ + for(int j = 0; j < figure_->data()[cond][i].size(); j++){ + if(figure_->data()[cond][i][j]){ + gridInt[i + x][j + y] = 2; + } + } + } + + for(int i = 0; i < grid->columns()+2; i++){ + qDebug() << gridInt[i]; + } + + QVector> cellColors = grid->getCellColors(); + + for(int i = 1; i < grid->columns()+1; i++){ + for(int j = maximumHeightFigure; j < grid->rows()+maximumHeightFigure; j++){ + if(gridInt[i][j] == 0){ + cellColors[i-1][j-maximumHeightFigure] = Qt::black; + } + if(gridInt[i][j] == 1){ + cellColors[i-1][j-maximumHeightFigure] = Qt::gray; + } + if(gridInt[i][j] == 2){ + cellColors[i-1][j-maximumHeightFigure] = figure_->color(); + } + } + } + grid->setCellColors(cellColors); + + + timer_ = new QTimer(); + connect(timer_, &QTimer::timeout, this, &tetris::TikTime); + timer_->start(1000); +} + +GridDrawer *tetris::getGridDrawer() +{ + return grid; +} + +void tetris::checkRows() +{ + for(int j = grid->rows() + 3; j >= 0; j--){ + bool ind = true; + for(int i = 0; i < grid->columns()+2; i++){ + if(gridInt[i][j] == 0){ + ind = false; + break; + } + } + if(ind){ + emit fullRow(); + for(int i = 0; i < grid->columns()+2; i++){ + gridInt[i].remove(j); + if(i == 0 || i == grid->columns()+1){ + gridInt[i].push_front(1); + }else{ + gridInt[i].push_front(0); + } + } + j++; + } + } +} + +int tetris::checkLastRow() +{ + int maximumHeightFigure = 4; + for(int i = 1; i < grid->columns()+1; i++){ + if(gridInt[i][maximumHeightFigure] != 0){ + gameOn = 0; + emit gameFinished(); + return 0; + } + } + return 1; +} + +void tetris::newGame() +{ + int maximumHeightFigure = 4; + qDebug() << "new game"; + for(int i = 1; i < grid->columns()+1; i++){ + for(int j = 0; j < grid->rows()+maximumHeightFigure; j++){ + gridInt[i][j] = 0; + } + } + + emit nextStep(); + figure_->setData(); + figure_->setColor(); + figure_->setCondition(0); + int cond = figure_->condition(); + int x = (grid->columns() + 2 - figure_->data()[cond].size())/2; + figure_->setX(x); + int y = 2; + if(figure_->data()[cond][0].size() == maximumHeightFigure){ + y = 0; + } + figure_->setY(y); + + for(int i = 0; i < figure_->data()[cond].size(); i++){ + for(int j = 0; j < figure_->data()[cond][i].size(); j++){ + if(figure_->data()[cond][i][j]){ + gridInt[i + x][j + y] = 2; + } + } + } + + updateCellColors(); + gameOn = 1; } -Tetris::~Tetris() { delete ui; } +void tetris::paintEvent(QPaintEvent *event) { + QPainter painter(this); + grid->draw(&painter); + this->update(); +} + +void tetris::updateCellColors(){ + QVector> cellColors = grid->getCellColors(); + int maximumHeightFigure = 4; + for(int i = 1; i < grid->columns()+1; i++){ + for(int j = maximumHeightFigure; j < grid->rows()+maximumHeightFigure; j++){ + if(gridInt[i][j] == 0){ + cellColors[i-1][j-maximumHeightFigure] = Qt::black; + } + if(gridInt[i][j] == 1){ + cellColors[i-1][j-maximumHeightFigure] = Qt::gray; + } + if(gridInt[i][j] == 2){ + cellColors[i-1][j-maximumHeightFigure] = figure_->color(); + } + } + } + grid->setCellColors(cellColors); +} + +bool tetris::moveFigureDown(){ + int maximumHeightFigure = 4; + bool ind = true; + QVector> tmpGridInt = gridInt; + for(int j = grid->rows()+maximumHeightFigure; j > 0; j--){ + if(ind){ + for(int i = 0; i < grid->columns()+2; i++){ + if(gridInt[i][j-1] != 1 && gridInt[i][j-1] != 0 && gridInt[i][j] == 1){ + ind = false; + break; + }else{ + if(gridInt[i][j] != 1 && gridInt[i][j-1] != 1){ + tmpGridInt[i][j] = gridInt[i][j-1]; + tmpGridInt[i][j-1] = 0; + } + } + } + }else{ + break; + } + } + if(ind){ + gridInt = tmpGridInt; + figure_->setY(figure_->y() + 1); + }else{ + checkRows(); + qDebug() << checkLastRow(); +// перекраска в серый цвет + for(int i = 0; i < grid->columns()+2; i++){ + for(int j = 0; j < grid->rows()+5; j++){ + if(gridInt[i][j] > 1){ + gridInt[i][j] = 1; + } + } + } + if(checkLastRow()){ + emit nextStep(); + figure_->setCondition(0); + int cond = figure_->condition(); + int x = (grid->columns() + 2 - figure_->data()[cond].size())/2; + figure_->setX(x); + int y = 2; + if(figure_->data()[cond][0].size() == maximumHeightFigure){ + y = 0; + } + figure_->setY(y); + + for(int i = 0; i < figure_->data()[cond].size(); i++){ + for(int j = 0; j < figure_->data()[0][i].size(); j++){ + if(figure_->data()[cond][i][j]){ + gridInt[i + x][j + y] = 2; + } + } + } + + for(int i = 0; i < grid->columns()+2; i++){ + qDebug() << gridInt[i]; + } + } + } + updateCellColors(); + return ind; +} + +GridDrawer *tetris::getGrid() +{ + return grid; +} + +void tetris::keyPressEvent(QKeyEvent *event) +{ + qDebug() << "Key event"; + + if (event->key() == Qt::Key_Down){ + qDebug() << "Key Down Pressed"; + moveFigureDown(); + } + + if(event->key() == Qt::Key_Space){ + qDebug() << "Key Space Pressed"; + bool ind = true; + while(ind && gameOn){ + ind = moveFigureDown(); + qDebug() << ind; + } + } + + if (event->key() == Qt::Key_Left){ + qDebug() << "Key Right Pressed"; + bool ind = true; + QVector> tmpGridInt = gridInt; + for(int i = 1; i < grid->columns()+2; i++){ + if(ind){ + for(int j = 0; j < grid->rows() + 5; j++){ + if(gridInt[i][j] != 1 && gridInt[i][j] != 0 && gridInt[i-1][j] == 1){ + ind = false; + break; + }else{ + if(gridInt[i][j] != 1 && gridInt[i-1][j] != 1){ + tmpGridInt[i-1][j] = gridInt[i][j]; + tmpGridInt[i][j] = 0; + } + } + } + }else{ + break; + } + } + if(ind){ + gridInt = tmpGridInt; + figure_->setX(figure_->x() - 1); + } + updateCellColors(); + } + + if (event->key() == Qt::Key_Right){ + qDebug() << "Key Right Pressed"; + bool ind = true; + QVector> tmpGridInt = gridInt; + for(int i = grid->columns()+1; i >0; i--){ + if(ind){ + for(int j = 0; j < grid->rows() + 5; j++){ + if(gridInt[i-1][j] != 1 && gridInt[i-1][j] != 0 && gridInt[i][j] == 1){ + ind = false; + break; + }else{ + if(gridInt[i][j] != 1 && gridInt[i-1][j] != 1){ + tmpGridInt[i][j] = gridInt[i-1][j]; + tmpGridInt[i-1][j] = 0; + } + } + } + }else{ + break; + } + } + if(ind){ + gridInt = tmpGridInt; + figure_->setX(figure_->x() + 1); + } + updateCellColors(); + } + for(int i = 0; i < grid->columns()+2; i++){ + qDebug() << gridInt[i]; + } + + if (event->key() == Qt::Key_Up){ + QVector> tmpGridInt = gridInt; + for(int i = grid->columns()+1; i >0; i--){ + for(int j = 0; j < grid->rows() + 5; j++){ + if(tmpGridInt[i][j] > 1){ + tmpGridInt[i][j] = 0; + } + } + } + figure_->nextCondition(); + int cond = figure_->condition(); + bool ind = 1; + for(int i = 0; i < figure_->data()[cond].size(); i++){ + if(ind){ + for(int j = 0; j < figure_->data()[cond][i].size(); j++){ + if(figure_->data()[cond][i][j] && tmpGridInt[i+figure_->x()][j+figure_->y()] == 0){ + tmpGridInt[i+figure_->x()][j+figure_->y()] = 2; + }else{ + if(figure_->data()[cond][i][j] && tmpGridInt[i+figure_->x()][j+figure_->y()]){ + ind = false; + break; + } + } + } + } + } + if(ind){ + gridInt = tmpGridInt; + updateCellColors(); + }else{ + figure_->prevCondition(); + } + } + this->update(); +} + +void tetris::TikTime() +{ + moveFigureDown(); +} diff --git a/tetris.h b/tetris.h index 1245a26..1b7054b 100644 --- a/tetris.h +++ b/tetris.h @@ -1,22 +1,51 @@ #ifndef TETRIS_H #define TETRIS_H -#include +#include "griddrawer.h" +#include "figure.h" +#include "nextfigure.h" +#include +#include +#include +#include -QT_BEGIN_NAMESPACE -namespace Ui { -class Tetris; -} -QT_END_NAMESPACE +class tetris : public QWidget { + Q_OBJECT -class Tetris : public QMainWindow { - Q_OBJECT +signals: + void nextStep(); + void gameFinished(); + void fullRow(); - public: - Tetris(QWidget *parent = nullptr); - ~Tetris(); +public slots: + void onFigureChanged(figure* newFigure) { + figure_ = newFigure; + } + void newGame(); + +public: + tetris(QWidget *parent = 0); + + GridDrawer *getGridDrawer(); + void checkRows(); + int checkLastRow(); + void updateCellColors(); + bool moveFigureDown(); + GridDrawer *getGrid(); + void TikTime(); + +protected: + void paintEvent(QPaintEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; + +private: + GridDrawer* grid; + figure* figure_; + QVector> gridInt; + bool gameOn; + QTimer *timer_; - private: - Ui::Tetris *ui; }; -#endif // TETRIS_H + + +#endif // TETRIS_H