From 27e9a34d13fc2e6d673ae0862524fd8d050bb22b Mon Sep 17 00:00:00 2001 From: pahanmar <109517190+pahanmar@users.noreply.github.com> Date: Mon, 18 Dec 2023 10:54:56 +0300 Subject: [PATCH 1/4] Commit1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1)Реализован тетрис с генерацией фигур и поворотами --- TetrisTask.pro | 6 +- figure.cpp | 58 ++++++++++++++++++ figure.h | 27 +++++++++ gamefield.cpp | 161 ++++++++++++++++++++++++++++++++++++++++++++++++- gamefield.h | 36 +++++++++-- tetris.cpp | 59 +++++++++++++++++- tetris.h | 8 +++ tetris.ui | 20 ++++-- 8 files changed, 361 insertions(+), 14 deletions(-) create mode 100644 figure.cpp create mode 100644 figure.h diff --git a/TetrisTask.pro b/TetrisTask.pro index f4de819..ddf75f8 100644 --- a/TetrisTask.pro +++ b/TetrisTask.pro @@ -11,11 +11,13 @@ CONFIG += c++17 SOURCES += \ gamefield.cpp \ main.cpp \ - tetris.cpp + tetris.cpp \ + figure.cpp HEADERS += \ gamefield.h \ - tetris.h + tetris.h \ + figure.h FORMS += \ tetris.ui diff --git a/figure.cpp b/figure.cpp new file mode 100644 index 0000000..e1331bd --- /dev/null +++ b/figure.cpp @@ -0,0 +1,58 @@ +#include "figure.h" +#include + +Figure::Figure() {} + +void Figure::generateFigure(){ + QVector>> arrayOfFigures; + arrayOfFigures.append( { {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0} } ); + arrayOfFigures.append( { {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0} } ); + arrayOfFigures.append( { {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0} } ); + arrayOfFigures.append( { {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0} } ); + arrayOfFigures.append( { {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0} } ); + arrayOfFigures.append( { {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} } ); + srand(time(NULL)); + int type = rand() % arrayOfFigures.size(); + figure_ = arrayOfFigures[type]; +} +int Figure::getXPoints() const{ + return figure_.size(); +} + +int Figure::getYPoints() const{ + return figure_[0].size(); +} + +int Figure::getFigureAt(int yPoints, int xPoints) const { + if (yPoints >= 0 && yPoints < getYPoints() && xPoints >= 0 && xPoints < getXPoints()) return figure_[yPoints][xPoints]; + return 0; +} +QVector> Figure::getFigure() { + return figure_; +} +void Figure::rotateFigure() { + QVector> rotated(getXPoints(), QVector (getYPoints())); + + for (int i = 0; i < getYPoints(); i++) { + for (int j = 0; j < getXPoints(); j++){ + rotated[j][getYPoints() - 1 - i] = figure_[i][j]; + } + } + figure_ = rotated; +} +void Figure::generateColor() { + QVector arrayOfColors; + arrayOfColors.append( QColor(255, 135, 50) ); + arrayOfColors.append( QColor(168, 50, 168) ); + arrayOfColors.append( QColor(0, 255, 0) ); + arrayOfColors.append( QColor(0, 255, 255) ); + arrayOfColors.append( QColor(127, 0, 255) ); + + srand(time(NULL)); + int type = rand() % arrayOfColors.size(); + color_ = arrayOfColors[type]; +} + +QColor Figure::getColor() { +return color_; +} diff --git a/figure.h b/figure.h new file mode 100644 index 0000000..6e8684f --- /dev/null +++ b/figure.h @@ -0,0 +1,27 @@ +#ifndef FIGURE_H +#define FIGURE_H + +#include ; +#include ; +#include + +class Figure +{ +public: + Figure(); + + void rotateFigure(); + void generateFigure(); + int getXPoints() const; + int getYPoints() const; + int getFigureAt(int row, int col) const; + QVector> getFigure(); + void generateColor(); + QColor getColor(); + +private: + QVector> figure_; + QColor color_; +}; + +#endif // FIGURE_H diff --git a/gamefield.cpp b/gamefield.cpp index 6a5ce13..91c6dd1 100644 --- a/gamefield.cpp +++ b/gamefield.cpp @@ -1,11 +1,20 @@ #include "gamefield.h" #include +#include +#include GameField::GameField(QWidget *parent) : QWidget{parent} { + setFocusPolicy(Qt::StrongFocus); + connect(this, &GameField::InitialisationStarted, this, &GameField::SetCells, Qt::QueuedConnection); + connect(this, &GameField::moveFigure, this, &GameField::refresh); + timer_ = new QTimer(this); + connect(timer_, &QTimer::timeout, this, &GameField::moveDown); + timer_ -> start(500); + startNewGame(); emit InitialisationStarted(); } @@ -13,7 +22,6 @@ uint GameField::GetRowsNumber() const { return rowsNumber_; } void GameField::SetRowsNumber(uint rowsNumber) { if (rowsNumber_ == rowsNumber) return; - rowsNumber_ = rowsNumber; emit RowsNumberChanged(); } @@ -22,11 +30,160 @@ uint GameField::GetColumnsNumber() const { return columnsNumber_; } void GameField::SetColumnNumber(uint columnsCount) { if (columnsNumber_ == columnsCount) return; - columnsNumber_ = columnsCount; emit ColumnsNumberChanged(); } +void GameField::paintEvent(QPaintEvent *event) { + Q_UNUSED(event); + + int nextLayerPositionX = 350; + int blockSize = 30; + + QPainter painter(this); + QVector> gameField = gameField_; + + painter.fillRect(0, 0, gameField[0].size()*blockSize, gameField.size()*blockSize, Qt::white); + for (int i = 0; i < gameField.size(); i++) { + for (int j = 0; j < gameField[i].size(); j++) { + if (gameField[i][j] == 1) { + painter.fillRect(j * blockSize, i * blockSize, blockSize, blockSize, Qt::yellow); + } else { + painter.drawRect(j * blockSize, i * blockSize, blockSize, blockSize); + } + } + } + + for (int i = 0; i < currentFigure_.getFigure().size(); i++) { + for (int j = 0; j < currentFigure_.getFigure()[i].size(); j++) { + if (currentFigure_.getFigure()[j][i] == 1) { + painter.fillRect(( j + currentXPosition_) * blockSize, (i + currentYPosition_)* blockSize, blockSize, blockSize, currentFigure_.getColor()); + } + } + } + +for (int i = 0; i < nextFigure_.getFigure().size(); i++) { + for (int j = 0; j < nextFigure_.getFigure()[i].size(); j++) { + if (nextFigure_.getFigure()[j][i] == 1) { + painter.fillRect( j * blockSize + nextLayerPositionX, i * blockSize, blockSize, blockSize, nextFigure_.getColor()); + } + } + } + + painter.drawText(350, 125, QString("Score: %1").arg(score_)); + +} + void GameField::SetCells() { qDebug() << rowsNumber_ << " " << columnsNumber_ << "|"; } + +void GameField::keyPressEvent(QKeyEvent *event) { + switch( event->key()) { + case Qt::Key_Left: + moveFigureLeft(); + break; + case Qt::Key_Right: + moveFigureRight(); + break; + case Qt::Key_Up: + rotateFigure(); + break; + case Qt::Key_Down: + moveDown(); + break; + } +} + void GameField::rotateFigure() { + Figure rotatedFigure = currentFigure_; + rotatedFigure.rotateFigure(); +if (!isCollision(rotatedFigure, 0, 0)) { + currentFigure_ = rotatedFigure; + emit moveFigure(); + } + } + +void GameField::moveFigureRight() { + if(!isCollision(currentFigure_, 1, 0)) { + currentXPosition_++; + emit moveFigure(); + } +} + +void GameField::moveFigureLeft() { + if(!isCollision(currentFigure_, -1, 0)) { + currentXPosition_--; + emit moveFigure(); + } +} + +void GameField::moveDown() { + if(!isCollision(currentFigure_, 0, 1)) { + currentYPosition_++; + emit moveFigure(); +}else { + for (int i = 0; i < currentFigure_.getYPoints(); i++) + for (int j = 0; j < currentFigure_.getXPoints(); j++) + if (currentFigure_.getFigureAt(j, i) == 1) + gameField_[currentYPosition_ + i ][currentXPosition_ + j] = 1; + setNewFigure(); + clearLines(); + } +} + +void GameField::refresh() { + repaint(); +} + +bool GameField::isCollision(const Figure &movedFigure, int xOffset, int yOffset) { + + for (int i = 0; i <= movedFigure.getYPoints(); i++) + for (int j = 0; j < movedFigure.getXPoints(); j++){ + if (movedFigure.getFigureAt(j, i) == 1) + if (currentXPosition_ + j + xOffset < 0 || currentXPosition_ + j + xOffset >= gameField_[0].size() || + currentYPosition_ + i + yOffset >= gameField_.size() || + gameField_[currentYPosition_ + i + yOffset][currentXPosition_ + j + xOffset] == 1) + return true; +} + return false; +} + +void GameField::setNewFigure() { + currentFigure_ = nextFigure_; + nextFigure_.generateFigure(); + nextFigure_.generateColor(); + currentYPosition_ = 0; + currentXPosition_ = 3; + emit moveFigure(); + if(isCollision(currentFigure_, 0, 0)){ + startNewGame(); + }; +} + +void GameField::startNewGame(){ + int xPoints = 10; + int yPoints = 20; + gameField_ = QVector> (yPoints, QVector (xPoints, 0)); + currentFigure_.generateFigure(); + currentFigure_.generateColor(); + nextFigure_.generateFigure(); + nextFigure_.generateColor(); + currentYPosition_ = 0; + currentXPosition_ = 3; + score_ = 0; +} + +void GameField::clearLines() { + for (int i = 0; i <= gameField_.size( )- 1; i++) { + if (std::all_of(gameField_[i].begin(), gameField_[i].end(), [](int val) { return val == 1; } ) ) { + gameField_.remove(i); + gameField_.prepend(QVector (gameField_[0].size(), 0)); + changeScore(100); + i--; + } + } +} + + void GameField::changeScore(int points){ + score_=score_ + points; + } diff --git a/gamefield.h b/gamefield.h index e36baa8..5c7ebf2 100644 --- a/gamefield.h +++ b/gamefield.h @@ -2,6 +2,9 @@ #define GAMEFIELD_H #include +#include + +#include "figure.h" class GameField : public QWidget { Q_OBJECT @@ -17,20 +20,45 @@ class GameField : public QWidget { void RowsNumberChanged(); void ColumnsNumberChanged(); void InitialisationStarted(); + void moveFigure(); private slots: void SetCells(); public: uint GetRowsNumber() const; - void SetRowsNumber(uint newRowsNumber); - uint GetColumnsNumber() const; void SetColumnNumber(uint newColumnsCount); + void SetRowsNumber(uint newRowsNumber); + + void moveDown(); + void rotateFigure(); + void moveFigureLeft(); + void moveFigureRight(); + void setNewFigure(); + void startNewGame(); + void clearLines(); + void changeScore(int points); + + QVector> getGameField(); + +protected slots: + void paintEvent(QPaintEvent *event); + void keyPressEvent(QKeyEvent *event); + void refresh(); private: - uint rowsNumber_ = 0; - uint columnsNumber_ = 0; + bool isCollision(const Figure &movedFigure, int xOffset, int yOffset); + + QVector> gameField_; + Figure nextFigure_; + Figure currentFigure_; + uint rowsNumber_ = 0; + uint columnsNumber_ = 0; + int currentXPosition_ = 3; + int currentYPosition_ = 0; + int score_ = 0; + QTimer* timer_; }; #endif // GAMEFIELD_H diff --git a/tetris.cpp b/tetris.cpp index 50cf380..722781e 100644 --- a/tetris.cpp +++ b/tetris.cpp @@ -1,9 +1,66 @@ #include "tetris.h" - #include "ui_tetris.h" +#include "gamefield.h" + +#include +#include +#include +#include +#include Tetris::Tetris(QWidget *parent) : QMainWindow(parent), ui(new Ui::Tetris) { ui->setupUi(this); + + auto *gameField = new GameField(this); + setCentralWidget(gameField); + + QMenu *gameMenu = new QMenu(); + gameMenu->setTitle("Menu"); + gameMenu->setVisible(true); + + QAction *startNewGameAction = new QAction("New Game"); + QAction *exitGameAction = new QAction("Exit"); + QAction *rulesAction = new QAction("Rules"); + + connect(startNewGameAction, &QAction::triggered, gameField, &GameField::startNewGame); + connect(exitGameAction, &QAction::triggered, this, &Tetris::close); + connect(rulesAction, &QAction::triggered, this, &Tetris::showRules); + + gameMenu->addAction(startNewGameAction); + gameMenu->addAction(exitGameAction); + gameMenu->addAction(rulesAction); + + ui->menubar->addMenu(gameMenu); + + QLabel *label = new QLabel(this); + label->setGeometry(350, 0, 100, 75); + label->setText("Next Figure"); + label->show(); + + QPushButton *newGameButton = new QPushButton("New Game", this); + newGameButton->setGeometry(350, 200, 100, 25); + QPushButton *exitButton = new QPushButton("Exit", this); + exitButton->setGeometry(350, 250, 100, 25); + + connect(newGameButton, &QPushButton::clicked, gameField, &GameField::startNewGame); + connect(exitButton, &QPushButton::clicked, this, &Tetris::close); } +void Tetris::showRules() { + QMessageBox::information( + this, + "Rules", + "In Tetris, players complete lines by moving differently shaped pieces (tetrominoes), which descend onto the playing field.\n" + "The completed lines disappear and grant the player points, and the player can proceed to fill the vacated spaces.\n" + "The game ends when the uncleared lines reach the top of the playing field.\n" + "\n" + "Game Control\n" + " Up key- rotate figure\n" + " Left key - move the figure Left\n" + " Right key - move the figure Right\n" + " Down key - lower the figure down\n" + ); + } + + Tetris::~Tetris() { delete ui; } diff --git a/tetris.h b/tetris.h index 1245a26..ca7aea7 100644 --- a/tetris.h +++ b/tetris.h @@ -2,6 +2,8 @@ #define TETRIS_H #include +#include +#include QT_BEGIN_NAMESPACE namespace Ui { @@ -16,7 +18,13 @@ class Tetris : public QMainWindow { Tetris(QWidget *parent = nullptr); ~Tetris(); +private slots: + void showRules(); + private: Ui::Tetris *ui; + QMenu *mewGameMenu_; + QMenu *exitMenu_; + QMenu *rulesMenu_; }; #endif // TETRIS_H diff --git a/tetris.ui b/tetris.ui index ae63379..ee960e1 100644 --- a/tetris.ui +++ b/tetris.ui @@ -6,8 +6,8 @@ 0 0 - 800 - 600 + 611 + 723 @@ -21,17 +21,27 @@ 10 + 0 0 - 800 - 21 + 611 + 26 - + + + Exit + + + + + StartNewGame + + From 36a58ce598a1c51420b08a4e588fb30feb107771 Mon Sep 17 00:00:00 2001 From: pahanmar <109517190+pahanmar@users.noreply.github.com> Date: Mon, 18 Dec 2023 10:57:15 +0300 Subject: [PATCH 2/4] commit2 --- figure.cpp | 12 ++++++------ figure.h | 2 +- gamefield.cpp | 1 + tetris.cpp | 2 -- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/figure.cpp b/figure.cpp index e1331bd..23f4f24 100644 --- a/figure.cpp +++ b/figure.cpp @@ -34,7 +34,7 @@ void Figure::rotateFigure() { QVector> rotated(getXPoints(), QVector (getYPoints())); for (int i = 0; i < getYPoints(); i++) { - for (int j = 0; j < getXPoints(); j++){ + for (int j = 0; j < getXPoints(); j++) { rotated[j][getYPoints() - 1 - i] = figure_[i][j]; } } @@ -42,11 +42,11 @@ void Figure::rotateFigure() { } void Figure::generateColor() { QVector arrayOfColors; - arrayOfColors.append( QColor(255, 135, 50) ); - arrayOfColors.append( QColor(168, 50, 168) ); - arrayOfColors.append( QColor(0, 255, 0) ); - arrayOfColors.append( QColor(0, 255, 255) ); - arrayOfColors.append( QColor(127, 0, 255) ); + arrayOfColors.append( QColor(255, 135, 50)); + arrayOfColors.append( QColor(168, 50, 168)); + arrayOfColors.append( QColor(0, 255, 0)); + arrayOfColors.append( QColor(0, 255, 255)); + arrayOfColors.append( QColor(127, 0, 255)); srand(time(NULL)); int type = rand() % arrayOfColors.size(); diff --git a/figure.h b/figure.h index 6e8684f..f4a0a5d 100644 --- a/figure.h +++ b/figure.h @@ -14,7 +14,7 @@ class Figure void generateFigure(); int getXPoints() const; int getYPoints() const; - int getFigureAt(int row, int col) const; + int getFigureAt(int yPoints, int xPoints) const; QVector> getFigure(); void generateColor(); QColor getColor(); diff --git a/gamefield.cpp b/gamefield.cpp index 91c6dd1..c94689b 100644 --- a/gamefield.cpp +++ b/gamefield.cpp @@ -94,6 +94,7 @@ void GameField::keyPressEvent(QKeyEvent *event) { break; } } + void GameField::rotateFigure() { Figure rotatedFigure = currentFigure_; rotatedFigure.rotateFigure(); diff --git a/tetris.cpp b/tetris.cpp index 722781e..e7b0214 100644 --- a/tetris.cpp +++ b/tetris.cpp @@ -61,6 +61,4 @@ void Tetris::showRules() { ); } - - Tetris::~Tetris() { delete ui; } From 978e2f294fad83921c4b7571685763b3e0101f70 Mon Sep 17 00:00:00 2001 From: pahanmar <109517190+pahanmar@users.noreply.github.com> Date: Mon, 18 Dec 2023 23:57:24 +0300 Subject: [PATCH 3/4] Commit F1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1)Исправлена стилистика 2)Создан класс drawFigure --- figure.cpp | 48 +++++++++++----------- figure.h | 5 +-- gamefield.cpp | 111 +++++++++++++++++++++++++------------------------- gamefield.h | 21 ++++++---- 4 files changed, 95 insertions(+), 90 deletions(-) diff --git a/figure.cpp b/figure.cpp index 23f4f24..c4a4ceb 100644 --- a/figure.cpp +++ b/figure.cpp @@ -3,34 +3,36 @@ Figure::Figure() {} -void Figure::generateFigure(){ - QVector>> arrayOfFigures; - arrayOfFigures.append( { {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0} } ); - arrayOfFigures.append( { {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0} } ); - arrayOfFigures.append( { {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0} } ); - arrayOfFigures.append( { {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0} } ); - arrayOfFigures.append( { {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0} } ); - arrayOfFigures.append( { {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} } ); +void Figure::generateFigure() { + QVector>> figures; + figures.append( { {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0} } ); + figures.append( { {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0} } ); + figures.append( { {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0} } ); + figures.append( { {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0} } ); + figures.append( { {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0} } ); + figures.append( { {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} } ); srand(time(NULL)); - int type = rand() % arrayOfFigures.size(); - figure_ = arrayOfFigures[type]; + int index = rand() % figures.size(); + figure_ = figures[index]; } -int Figure::getXPoints() const{ + +int Figure::getXPoints() const { return figure_.size(); } -int Figure::getYPoints() const{ +int Figure::getYPoints() const { return figure_[0].size(); } int Figure::getFigureAt(int yPoints, int xPoints) const { - if (yPoints >= 0 && yPoints < getYPoints() && xPoints >= 0 && xPoints < getXPoints()) return figure_[yPoints][xPoints]; + if (yPoints >= 0 && yPoints < getYPoints() && xPoints >= 0 && xPoints < getXPoints()) + return figure_[yPoints][xPoints]; return 0; } QVector> Figure::getFigure() { return figure_; } -void Figure::rotateFigure() { +void Figure::rotate() { QVector> rotated(getXPoints(), QVector (getYPoints())); for (int i = 0; i < getYPoints(); i++) { @@ -41,18 +43,18 @@ void Figure::rotateFigure() { figure_ = rotated; } void Figure::generateColor() { - QVector arrayOfColors; - arrayOfColors.append( QColor(255, 135, 50)); - arrayOfColors.append( QColor(168, 50, 168)); - arrayOfColors.append( QColor(0, 255, 0)); - arrayOfColors.append( QColor(0, 255, 255)); - arrayOfColors.append( QColor(127, 0, 255)); + QVector colors; + colors.append( QColor(255, 135, 50)); + colors.append( QColor(168, 50, 168)); + colors.append( QColor(0, 255, 0)); + colors.append( QColor(0, 255, 255)); + colors.append( QColor(127, 0, 255)); srand(time(NULL)); - int type = rand() % arrayOfColors.size(); - color_ = arrayOfColors[type]; + int index = rand() % colors.size(); + color_ = colors[index]; } QColor Figure::getColor() { -return color_; + return color_; } diff --git a/figure.h b/figure.h index f4a0a5d..c99d0b8 100644 --- a/figure.h +++ b/figure.h @@ -5,12 +5,11 @@ #include ; #include -class Figure -{ +class Figure { public: Figure(); - void rotateFigure(); + void rotate(); void generateFigure(); int getXPoints() const; int getYPoints() const; diff --git a/gamefield.cpp b/gamefield.cpp index c94689b..bb2fbc7 100644 --- a/gamefield.cpp +++ b/gamefield.cpp @@ -7,7 +7,7 @@ GameField::GameField(QWidget *parent) : QWidget{parent} { setFocusPolicy(Qt::StrongFocus); - connect(this, &GameField::InitialisationStarted, this, &GameField::SetCells, + connect(this, &GameField::initialisationStarted, this, &GameField::SetCells, Qt::QueuedConnection); connect(this, &GameField::moveFigure, this, &GameField::refresh); @@ -15,7 +15,7 @@ GameField::GameField(QWidget *parent) : QWidget{parent} { connect(timer_, &QTimer::timeout, this, &GameField::moveDown); timer_ -> start(500); startNewGame(); - emit InitialisationStarted(); + emit initialisationStarted(); } uint GameField::GetRowsNumber() const { return rowsNumber_; } @@ -23,7 +23,7 @@ uint GameField::GetRowsNumber() const { return rowsNumber_; } void GameField::SetRowsNumber(uint rowsNumber) { if (rowsNumber_ == rowsNumber) return; rowsNumber_ = rowsNumber; - emit RowsNumberChanged(); + emit rowsNumberChanged(); } uint GameField::GetColumnsNumber() const { return columnsNumber_; } @@ -31,46 +31,43 @@ uint GameField::GetColumnsNumber() const { return columnsNumber_; } void GameField::SetColumnNumber(uint columnsCount) { if (columnsNumber_ == columnsCount) return; columnsNumber_ = columnsCount; - emit ColumnsNumberChanged(); + emit columnsNumberChanged(); +} + +void GameField::drawFigure(Figure figure, QPainter *painter, QPoint currentPosition) { + + for (int i = 0; i < figure.getFigure().size(); i++) { + for (int j = 0; j < figure.getFigure()[i].size(); j++) { + if (figure.getFigure()[j][i] == 1) { + painter->fillRect(( j + currentPosition.x()) * blockSize_, (i + currentPosition.y()) * blockSize_, blockSize_, blockSize_, figure.getColor()); + } + } + } } void GameField::paintEvent(QPaintEvent *event) { Q_UNUSED(event); - int nextLayerPositionX = 350; - int blockSize = 30; + int nextLayerPositionX = 11; - QPainter painter(this); + QPainter *painter = new QPainter(this); QVector> gameField = gameField_; - painter.fillRect(0, 0, gameField[0].size()*blockSize, gameField.size()*blockSize, Qt::white); + painter->fillRect(0, 0, gameField[0].size()*blockSize_, gameField.size()*blockSize_, Qt::white); for (int i = 0; i < gameField.size(); i++) { for (int j = 0; j < gameField[i].size(); j++) { if (gameField[i][j] == 1) { - painter.fillRect(j * blockSize, i * blockSize, blockSize, blockSize, Qt::yellow); + painter->fillRect(j * blockSize_, i * blockSize_, blockSize_, blockSize_, Qt::yellow); } else { - painter.drawRect(j * blockSize, i * blockSize, blockSize, blockSize); + painter->drawRect(j * blockSize_, i * blockSize_, blockSize_, blockSize_); } } } - for (int i = 0; i < currentFigure_.getFigure().size(); i++) { - for (int j = 0; j < currentFigure_.getFigure()[i].size(); j++) { - if (currentFigure_.getFigure()[j][i] == 1) { - painter.fillRect(( j + currentXPosition_) * blockSize, (i + currentYPosition_)* blockSize, blockSize, blockSize, currentFigure_.getColor()); - } - } - } - -for (int i = 0; i < nextFigure_.getFigure().size(); i++) { - for (int j = 0; j < nextFigure_.getFigure()[i].size(); j++) { - if (nextFigure_.getFigure()[j][i] == 1) { - painter.fillRect( j * blockSize + nextLayerPositionX, i * blockSize, blockSize, blockSize, nextFigure_.getColor()); - } - } - } + drawFigure(currentFigure_, painter, QPoint(currentXPosition_, currentYPosition_)); + drawFigure(nextFigure_, painter, QPoint(nextLayerPositionX, 0)); - painter.drawText(350, 125, QString("Score: %1").arg(score_)); + painter->drawText(350, 125, QString("Score: %1").arg(score_)); } @@ -80,32 +77,32 @@ void GameField::SetCells() { void GameField::keyPressEvent(QKeyEvent *event) { switch( event->key()) { - case Qt::Key_Left: - moveFigureLeft(); - break; - case Qt::Key_Right: - moveFigureRight(); - break; - case Qt::Key_Up: - rotateFigure(); - break; - case Qt::Key_Down: - moveDown(); - break; + case Qt::Key_Left: + moveFigureLeft(); + break; + case Qt::Key_Right: + moveFigureRight(); + break; + case Qt::Key_Up: + rotateFigure(); + break; + case Qt::Key_Down: + moveDown(); + break; } } - void GameField::rotateFigure() { - Figure rotatedFigure = currentFigure_; - rotatedFigure.rotateFigure(); -if (!isCollision(rotatedFigure, 0, 0)) { +void GameField::rotateFigure() { + Figure rotatedFigure = currentFigure_; + rotatedFigure.rotate(); + if (!isCollision(rotatedFigure, 0, 0)) { currentFigure_ = rotatedFigure; emit moveFigure(); } } void GameField::moveFigureRight() { - if(!isCollision(currentFigure_, 1, 0)) { + if(!isCollision(currentFigure_, 1, 0)) { currentXPosition_++; emit moveFigure(); } @@ -122,13 +119,13 @@ void GameField::moveDown() { if(!isCollision(currentFigure_, 0, 1)) { currentYPosition_++; emit moveFigure(); -}else { + }else { for (int i = 0; i < currentFigure_.getYPoints(); i++) for (int j = 0; j < currentFigure_.getXPoints(); j++) if (currentFigure_.getFigureAt(j, i) == 1) - gameField_[currentYPosition_ + i ][currentXPosition_ + j] = 1; - setNewFigure(); - clearLines(); + gameField_[currentYPosition_ + i][currentXPosition_ + j] = 1; + setNewFigure(); + clearLines(); } } @@ -138,14 +135,16 @@ void GameField::refresh() { bool GameField::isCollision(const Figure &movedFigure, int xOffset, int yOffset) { - for (int i = 0; i <= movedFigure.getYPoints(); i++) - for (int j = 0; j < movedFigure.getXPoints(); j++){ - if (movedFigure.getFigureAt(j, i) == 1) + for (int i = 0; i <= movedFigure.getYPoints(); i++) { + for (int j = 0; j < movedFigure.getXPoints(); j++) { + if (movedFigure.getFigureAt(j, i) == 1) { if (currentXPosition_ + j + xOffset < 0 || currentXPosition_ + j + xOffset >= gameField_[0].size() || currentYPosition_ + i + yOffset >= gameField_.size() || gameField_[currentYPosition_ + i + yOffset][currentXPosition_ + j + xOffset] == 1) return true; -} + } + } + } return false; } @@ -156,12 +155,12 @@ void GameField::setNewFigure() { currentYPosition_ = 0; currentXPosition_ = 3; emit moveFigure(); - if(isCollision(currentFigure_, 0, 0)){ - startNewGame(); + if(isCollision(currentFigure_, 0, 0)) { + startNewGame(); }; } -void GameField::startNewGame(){ +void GameField::startNewGame() { int xPoints = 10; int yPoints = 20; gameField_ = QVector> (yPoints, QVector (xPoints, 0)); @@ -175,8 +174,8 @@ void GameField::startNewGame(){ } void GameField::clearLines() { - for (int i = 0; i <= gameField_.size( )- 1; i++) { - if (std::all_of(gameField_[i].begin(), gameField_[i].end(), [](int val) { return val == 1; } ) ) { + for (int i = 0; i <= gameField_.size() - 1; i++) { + if (std::all_of(gameField_[i].begin(), gameField_[i].end(), [](int val) { return val == 1; })) { gameField_.remove(i); gameField_.prepend(QVector (gameField_[0].size(), 0)); changeScore(100); diff --git a/gamefield.h b/gamefield.h index 5c7ebf2..80f5711 100644 --- a/gamefield.h +++ b/gamefield.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include "figure.h" @@ -12,14 +14,14 @@ class GameField : public QWidget { explicit GameField(QWidget *parent = nullptr); Q_PROPERTY(uint rowsNumber READ GetRowsNumber WRITE SetRowsNumber NOTIFY - RowsNumberChanged FINAL) + rowsNumberChanged FINAL) Q_PROPERTY(uint columnsNumber READ GetColumnsNumber WRITE SetColumnNumber - NOTIFY ColumnsNumberChanged FINAL) + NOTIFY columnsNumberChanged FINAL) signals: - void RowsNumberChanged(); - void ColumnsNumberChanged(); - void InitialisationStarted(); + void rowsNumberChanged(); + void columnsNumberChanged(); + void initialisationStarted(); void moveFigure(); private slots: @@ -43,12 +45,13 @@ class GameField : public QWidget { QVector> getGameField(); protected slots: - void paintEvent(QPaintEvent *event); - void keyPressEvent(QKeyEvent *event); void refresh(); private: - bool isCollision(const Figure &movedFigure, int xOffset, int yOffset); + void paintEvent(QPaintEvent *event); + void keyPressEvent(QKeyEvent *event); + bool isCollision(const Figure &movedFigure, int xOffset, int yOffset); + void drawFigure(Figure figure, QPainter *painter, QPoint currentPosition); QVector> gameField_; Figure nextFigure_; @@ -58,7 +61,9 @@ protected slots: int currentXPosition_ = 3; int currentYPosition_ = 0; int score_ = 0; + int blockSize_ = 30; QTimer* timer_; + QPainter painter_; }; #endif // GAMEFIELD_H From 6bcce464c8a1e7b1e9669db7da0bdfcacf505ab6 Mon Sep 17 00:00:00 2001 From: pahanmar <109517190+pahanmar@users.noreply.github.com> Date: Tue, 19 Dec 2023 00:12:12 +0300 Subject: [PATCH 4/4] Commit F2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1)Добавлена структура QPoint 2)Переделана функция rotate() --- figure.cpp | 16 +++++++++------- figure.h | 3 ++- gamefield.cpp | 5 ++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/figure.cpp b/figure.cpp index c4a4ceb..9ef9161 100644 --- a/figure.cpp +++ b/figure.cpp @@ -24,20 +24,22 @@ int Figure::getYPoints() const { return figure_[0].size(); } -int Figure::getFigureAt(int yPoints, int xPoints) const { - if (yPoints >= 0 && yPoints < getYPoints() && xPoints >= 0 && xPoints < getXPoints()) - return figure_[yPoints][xPoints]; +int Figure::getFigureAt(QPoint point) const { + if (point.y() >= 0 && point.y() < getYPoints() && point.x() >= 0 && point.x() < getXPoints()) + return figure_[point.y()][point.x()]; return 0; } QVector> Figure::getFigure() { return figure_; } void Figure::rotate() { - QVector> rotated(getXPoints(), QVector (getYPoints())); + int xPoints = getXPoints(); + int yPoints = getYPoints(); + QVector> rotated(xPoints, QVector (yPoints)); - for (int i = 0; i < getYPoints(); i++) { - for (int j = 0; j < getXPoints(); j++) { - rotated[j][getYPoints() - 1 - i] = figure_[i][j]; + for (int i = 0; i < yPoints; i++) { + for (int j = 0; j < xPoints; j++) { + rotated[j][yPoints - 1 - i] = figure_[i][j]; } } figure_ = rotated; diff --git a/figure.h b/figure.h index c99d0b8..ba72b2c 100644 --- a/figure.h +++ b/figure.h @@ -4,6 +4,7 @@ #include ; #include ; #include +#include class Figure { public: @@ -13,7 +14,7 @@ class Figure { void generateFigure(); int getXPoints() const; int getYPoints() const; - int getFigureAt(int yPoints, int xPoints) const; + int getFigureAt(QPoint point) const; QVector> getFigure(); void generateColor(); QColor getColor(); diff --git a/gamefield.cpp b/gamefield.cpp index bb2fbc7..112cdfc 100644 --- a/gamefield.cpp +++ b/gamefield.cpp @@ -66,7 +66,6 @@ void GameField::paintEvent(QPaintEvent *event) { drawFigure(currentFigure_, painter, QPoint(currentXPosition_, currentYPosition_)); drawFigure(nextFigure_, painter, QPoint(nextLayerPositionX, 0)); - painter->drawText(350, 125, QString("Score: %1").arg(score_)); } @@ -122,7 +121,7 @@ void GameField::moveDown() { }else { for (int i = 0; i < currentFigure_.getYPoints(); i++) for (int j = 0; j < currentFigure_.getXPoints(); j++) - if (currentFigure_.getFigureAt(j, i) == 1) + if (currentFigure_.getFigureAt(QPoint(i, j)) == 1) gameField_[currentYPosition_ + i][currentXPosition_ + j] = 1; setNewFigure(); clearLines(); @@ -137,7 +136,7 @@ bool GameField::isCollision(const Figure &movedFigure, int xOffset, int yOffset) for (int i = 0; i <= movedFigure.getYPoints(); i++) { for (int j = 0; j < movedFigure.getXPoints(); j++) { - if (movedFigure.getFigureAt(j, i) == 1) { + if (movedFigure.getFigureAt(QPoint(i, j)) == 1) { if (currentXPosition_ + j + xOffset < 0 || currentXPosition_ + j + xOffset >= gameField_[0].size() || currentYPosition_ + i + yOffset >= gameField_.size() || gameField_[currentYPosition_ + i + yOffset][currentXPosition_ + j + xOffset] == 1)