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
11 changes: 5 additions & 6 deletions TetrisTask.pro
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ CONFIG += c++17
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
gamefield.cpp \
main.cpp \
tetris.cpp
mainwindow.cpp \
tetrisgrid.cpp

HEADERS += \
gamefield.h \
tetris.h
mainwindow.h \
tetrisgrid.h

FORMS += \
tetris.ui
FORMS +=

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
Expand Down
32 changes: 0 additions & 32 deletions gamefield.cpp

This file was deleted.

36 changes: 0 additions & 36 deletions gamefield.h

This file was deleted.

13 changes: 7 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <QApplication>

#include "tetris.h"
#include <QMessageBox>
#include "mainwindow.h"

int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Tetris w;
w.show();
return a.exec();
QApplication a(argc, argv);
MainWindow w;
w.resize(400, 500);
w.show();
return a.exec();
}
112 changes: 112 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QApplication>
#include <QStyleFactory>
#include <QMessageBox>
#include <QtWidgets/QGraphicsDropShadowEffect>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), score_(0) {
isGameOverHandled_ = false;
tetrisGrid_ = new TetrisGrid(this);
startButton_ =new QPushButton("Начать новую игру", this);
exitButton_ = new QPushButton("Выход", this);
scoreLabel_ = new QLabel("Счетчик очков: 0", this);
helpButton_ = new QPushButton("Справка", this);

qApp->setStyle(QStyleFactory::create("Fusion"));
QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
shadowEffect->setBlurRadius(15);
shadowEffect->setColor(QColor(0, 0, 0, 150));
shadowEffect->setOffset(0, 5);

QString buttonStyle = "QPushButton {"
" background-color: #3498db;"
" border-style: outset;"
" border-width: 2px;"
" border-radius: 10px;"
" border-color: #2980b9;"
" color: #ecf0f1;"
" padding: 5px;"
"}"
"QPushButton:hover {"
" background-color: #2980b9;"
" color: #bdc3c7;"
"}";
startButton_->setStyleSheet(buttonStyle);
startButton_->setGraphicsEffect(shadowEffect);
helpButton_->setStyleSheet(buttonStyle);
helpButton_->setGraphicsEffect(shadowEffect);
exitButton_->setStyleSheet(buttonStyle);
exitButton_->setGraphicsEffect(shadowEffect);

connect(helpButton_, &QPushButton::clicked, this, &MainWindow::showHelp);
connect(startButton_, &QPushButton::clicked, this, &MainWindow::restartGame);
connect(exitButton_, &QPushButton::clicked, this, &MainWindow::exitGame);
connect(tetrisGrid_, SIGNAL(scoreChanged(int)), this, SLOT(updateScore(int)));
connect(tetrisGrid_, &TetrisGrid::gameOver, [=]() {
if (!isGameOverHandled_) {
isGameOverHandled_ = true;
exitGame();
}
});

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(tetrisGrid_, Qt::AlignHCenter);
layout->addWidget(scoreLabel_);
layout->addWidget(startButton_);
layout->addWidget(helpButton_);
layout->addWidget(exitButton_);

QWidget *centralWidget = new QWidget(this);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
}

MainWindow::~MainWindow() {}

void MainWindow::showHelp() {
QString helpText = "Правила игры:\n\n"
"1. Сдвигайте фигуры влево/вправо, используя стрелки клавиатуры.\n"
"2. Поворачивайте фигуры при помощи клавиши 'Вверх'.\n"
"3. Ускоряйте падение фигур с помощью кнопки 'Вниз'.\n"
"4. Заполняйте горизонтальные линии, чтобы они исчезали и приносили очки.\n"
"\nУправление:\n"
"- Сдвиг влево: Стрелка влево\n"
"- Сдвиг вправо: Стрелка вправо\n"
"- Поворот: Стрелка вверх\n"
"- Ускорение падения: Стрелка вниз'\n";
QMessageBox::information(this, "Справка", helpText);
}

void MainWindow::restartGame() {
tetrisGrid_->startGame();
setupGame();
}

void MainWindow::startGame() {
setFocus();
score_ = 0;
updateScore(score_);
}

void MainWindow::exitGame() {
isGameOverHandled_ = true;
QMessageBox messageBox;
messageBox.setWindowTitle("Игра окончена");
messageBox.setText("Игра завершена. Набрано очков: " + QString::number(score_));
messageBox.setStandardButtons(QMessageBox::Ok);
messageBox.exec();
close();
}

void MainWindow::updateScore(int newScore) {
score_ = newScore;
scoreLabel_->setText("Счетчик очков: " + QString::number(score_));
}

void MainWindow::keyPressEvent(QKeyEvent *event) {
tetrisGrid_->handleKeyPressEvent(event);
}

void MainWindow::setupGame() {}
37 changes: 37 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include "tetrisgrid.h"

class MainWindow : public QMainWindow {
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private:
void setupGame();
int score_;
bool isGameOverHandled_ = false;
TetrisGrid *tetrisGrid_;
QPushButton *startButton_;
QPushButton *exitButton_;
QPushButton *helpButton_;
QLabel *scoreLabel_;

private slots:
void startGame();
void exitGame();
void updateScore(int newScore);
void restartGame();
void showHelp();

protected:
void keyPressEvent(QKeyEvent *event);

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.

keyPressEvent - это не слот, а обычный метод, при чем protected

};

#endif // MAINWINDOW_H
46 changes: 0 additions & 46 deletions tetris.ui

This file was deleted.

Loading