Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions TetrisTask.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions figure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "figure.h"
#include <time.h>

Figure::Figure() {}

void Figure::generateFigure() {
QVector<QVector<QVector<int>>> 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 index = rand() % figures.size();
figure_ = figures[index];
}

int Figure::getXPoints() const {
return figure_.size();
}

int Figure::getYPoints() const {
return figure_[0].size();
}

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<QVector<int>> Figure::getFigure() {
return figure_;
}
void Figure::rotate() {
int xPoints = getXPoints();
int yPoints = getYPoints();
QVector<QVector<int>> rotated(xPoints, QVector<int> (yPoints));

for (int i = 0; i < yPoints; i++) {
for (int j = 0; j < xPoints; j++) {
rotated[j][yPoints - 1 - i] = figure_[i][j];
}
}
figure_ = rotated;
}
void Figure::generateColor() {
QVector<QColor> 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 index = rand() % colors.size();
color_ = colors[index];
}

QColor Figure::getColor() {
return color_;
}
27 changes: 27 additions & 0 deletions figure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef FIGURE_H
#define FIGURE_H

#include <QVector>;
#include <QObject>;
#include <QColor>
#include <QPoint>

class Figure {
public:
Figure();

void rotate();
void generateFigure();
int getXPoints() const;
int getYPoints() const;
int getFigureAt(QPoint point) const;
QVector<QVector<int>> getFigure();
void generateColor();
QColor getColor();

private:
QVector<QVector<int>> figure_;
QColor color_;
};

#endif // FIGURE_H
168 changes: 162 additions & 6 deletions gamefield.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,188 @@
#include "gamefield.h"

#include <QDebug>
#include <QPainter>
#include <QKeyEvent>

GameField::GameField(QWidget *parent) : QWidget{parent} {
connect(this, &GameField::InitialisationStarted, this, &GameField::SetCells,
setFocusPolicy(Qt::StrongFocus);

connect(this, &GameField::initialisationStarted, this, &GameField::SetCells,
Qt::QueuedConnection);
connect(this, &GameField::moveFigure, this, &GameField::refresh);

emit InitialisationStarted();
timer_ = new QTimer(this);
connect(timer_, &QTimer::timeout, this, &GameField::moveDown);
timer_ -> start(500);
startNewGame();
emit initialisationStarted();
}

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_; }

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 = 11;

QPainter *painter = new QPainter(this);
QVector<QVector<int>> gameField = gameField_;

painter->fillRect(0, 0, gameField[0].size()*blockSize_, gameField.size()*blockSize_, Qt::white);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пробелы вокруг оператора умножения.

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_);
}
}
}

drawFigure(currentFigure_, painter, QPoint(currentXPosition_, currentYPosition_));
drawFigure(nextFigure_, painter, QPoint(nextLayerPositionX, 0));
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.rotate();
if (!isCollision(rotatedFigure, 0, 0)) {
currentFigure_ = rotatedFigure;
emit moveFigure();
}
}

void GameField::moveFigureRight() {
if(!isCollision(currentFigure_, 1, 0)) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пробел после If

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 {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Перед else пробел

for (int i = 0; i < currentFigure_.getYPoints(); i++)
for (int j = 0; j < currentFigure_.getXPoints(); j++)
if (currentFigure_.getFigureAt(QPoint(i, j)) == 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(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)
return true;
}
}
}
return false;
}

void GameField::setNewFigure() {
currentFigure_ = nextFigure_;
nextFigure_.generateFigure();
nextFigure_.generateColor();
currentYPosition_ = 0;
currentXPosition_ = 3;
emit moveFigure();
if(isCollision(currentFigure_, 0, 0)) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пробел после if

startNewGame();
};
}

void GameField::startNewGame() {
int xPoints = 10;
int yPoints = 20;
gameField_ = QVector<QVector<int>> (yPoints, QVector<int> (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<int> (gameField_[0].size(), 0));
changeScore(100);
i--;
}
}
}

void GameField::changeScore(int points){
score_=score_ + points;
}
51 changes: 42 additions & 9 deletions gamefield.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,68 @@
#define GAMEFIELD_H

#include <QWidget>
#include <QTimer>
#include <QPainter>
#include <QPoint>

#include "figure.h"

class GameField : public QWidget {
Q_OBJECT
public:
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:
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<QVector<int>> getGameField();

protected slots:
void refresh();

private:
uint rowsNumber_ = 0;
uint columnsNumber_ = 0;
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<QVector<int>> gameField_;
Figure nextFigure_;
Figure currentFigure_;
uint rowsNumber_ = 0;
uint columnsNumber_ = 0;
int currentXPosition_ = 3;
int currentYPosition_ = 0;
int score_ = 0;
int blockSize_ = 30;
QTimer* timer_;
QPainter painter_;
};

#endif // GAMEFIELD_H
Loading