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
21 changes: 18 additions & 3 deletions Pacman.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@ QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17
CONFIG += c++11
CONFIG += c++14

# 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 += \
pacmanarea/dragitem/coindragitem.cpp \
pacmanarea/dragitem/dragitemfabric.cpp \
pacmanarea/dragitem/walldragitem.cpp \
pacmanarea/pacman/pacmanitem.cpp \
pacmanarea\dragitem\abstractdragitem.cpp \
pacmanarea\droppablearea\droppablearea.cpp \
main.cpp \
mainwindow.cpp
mainwindow.cpp \
pacmanarea\pacmanarea.cpp

HEADERS += \
mainwindow.h
pacmanarea/dragitem/coindragitem.h \
pacmanarea/dragitem/dragitemfabric.h \
pacmanarea/dragitem/walldragitem.h \
pacmanarea/pacman/pacmanitem.h \
pacmanarea\dragitem\abstractdragitem.h \
pacmanarea\droppablearea\droppablearea.h \
mainwindow.h \
pacmanarea\pacmanarea.h

FORMS += \
mainwindow.ui
Expand Down
23 changes: 23 additions & 0 deletions gamewindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "gamewindow.h"

GameWindow::GameWindow(QVector<QVector<int>> boxArea, QWidget *parent) : QWidget (parent) {
boxArea_ = boxArea;
initializeGame();
QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &GameWindow::updateGame);
timer->start(500);
}

void GameWindow::paintEvent(QPaintEvent* event) {
Q_UNUSED(event);
QPainter painter(this);
drawGameArea(&painter);
pacman_->paint(&painter, nullptr, nullptr);
}

void GameWindow::updateGame() {
movePacman();
checkCollision();
update();
}

26 changes: 26 additions & 0 deletions gamewindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef GAMEWINDOW_H
#define GAMEWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QPainter>
#include <QTimer>
#include "pacmanarea/pacman/pacmanitem.h"

class GameWindow : public QWidget
{
Q_OBJECT
public:
explicit GameWindow(QVector<QVector<int>> boxArea, QWidget *parent = nullptr);
protected:
void updateGame();
void paintEvent(QPaintEvent* 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.

Переопределенные методы лучше убрать в protected, как минимум.


signals:

private:
QVector<QVector<int>> boxArea_;
PacManItem* pacman_;
};

#endif // GAMEWINDOW_H
8 changes: 4 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
16 changes: 13 additions & 3 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->setupUi(this);
pacman_ = new PacmanArea();
ui->sceneLayout->addWidget(pacman_);

connect(ui->startBtn, &QPushButton::clicked, pacman_, &PacmanArea::startGame);
connect(ui->startBtn, &QPushButton::clicked, this, &MainWindow::setFocuse);
connect(ui->stopBtn, &QPushButton::clicked, pacman_, &PacmanArea::stopGame);
}

void MainWindow::setFocuse() {
pacman_->setFocus();
}

MainWindow::~MainWindow()
{
delete ui;
delete ui;
}

17 changes: 11 additions & 6 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
#define MAINWINDOW_H

#include <QMainWindow>
#include "./pacmanarea/pacmanarea.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT
Q_OBJECT

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

private:
Ui::MainWindow *ui;
public slots:
void setFocuse();

private:
Ui::MainWindow *ui;
PacmanArea * pacman_;
};
#endif // MAINWINDOW_H
108 changes: 104 additions & 4 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,115 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
<width>588</width>
<height>567</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<property name="styleSheet">
<string notr="true">QWidget {
background-color: #fff;
}
QLabel {
color: #464d55;
font-weight: 600;
}
QLabel#heading {
color: #0f1925;
font-size: 18px;
margin-bottom: 10px;
}

QLabel#subheading {
color: #0f1925;
font-size: 12px;
font-weight: normal;
margin-bottom: 10px;
}
QLineEdit {
border-radius: 8px;
border: 1px solid #e0e4e7;
padding: 5px 15px;
}

QLineEdit:focus {
border: 1px solid #d0e3ff;
}

QLineEdit::placeholder {
color: #767e89;
}
QPushButton {
background-color: #0d6efd;
color: #fff;
font-weight: 600;
border-radius: 8px;
border: 1px solid #0d6efd;
padding: 20px 15px;
margin: 10px;
outline: 0px;
}
QPushButton:hover,
QPushButton:focus {
background-color: #0b5ed7;
border: 3px solid #9ac3fe;
}</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="sceneLayout"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<widget class="QPushButton" name="stopBtn">
<property name="text">
<string>Закончить игру</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="startBtn">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>Начать игру</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>588</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
Expand Down
32 changes: 32 additions & 0 deletions pacmanarea/dragitem/abstractdragitem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "abstractdragitem.h"
#include <QDebug>

AbstractDragItem::AbstractDragItem(){};

AbstractDragItem::AbstractDragItem(qreal x, qreal y, qreal boxSize, QString name, QColor color) {
setBrush(color);
setFlag(QGraphicsItem::ItemIsMovable);
this->name_ = name;
}

void AbstractDragItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
if (event->button() == Qt::LeftButton) {
QMimeData* mimeData = new QMimeData;
mimeData->setText(name_);

QDrag* drag = new QDrag(this);
drag->setMimeData(mimeData);

drag->exec(Qt::MoveAction);
}

QGraphicsRectItem::mousePressEvent(event);
}

QString AbstractDragItem::getName() {
return name_;
};

QPair<int, int> AbstractDragItem::getSize() {
return QPair<int, int>{ width_, height_ };
}
33 changes: 33 additions & 0 deletions pacmanarea/dragitem/abstractdragitem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef ABSTRACTDRAGITEM_H
#define ABSTRACTDRAGITEM_H

#include <QGraphicsRectItem>
#include <QApplication>
#include <QDrag>
#include <QMimeData>
#include <QGraphicsSceneMouseEvent>
#include <QObject>
#include <QBrush>
#include <QPainter>

class AbstractDragItem : public QObject, public QGraphicsRectItem {
Q_OBJECT
public:
AbstractDragItem();
AbstractDragItem(qreal x, qreal y, qreal boxSize, QString name, QColor color);
void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
QPair<int, int> getSize();
QString getName();

public slots:
virtual void touch() = 0;

protected:
int width_ = 1;
int height_ = 1;

private:
QString name_;
};

#endif // ABSTRACTDRAGITEM_H
24 changes: 24 additions & 0 deletions pacmanarea/dragitem/coindragitem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "coindragitem.h"

CoinDragItem::CoinDragItem(qreal x, qreal y, qreal boxSize) :
AbstractDragItem::AbstractDragItem(x, y, boxSize, DragItemFabric::getNameByType(DragItemFabric::COIN), Qt::yellow)
{

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.

Не переносим скобку

setRect(x, y, boxSize * WIDTH_, boxSize * HEIGHT_);
AbstractDragItem::WIDTH_ = WIDTH_;
AbstractDragItem::HEIGHT_ = HEIGHT_;
}

void CoinDragItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
Q_UNUSED(option);
Q_UNUSED(widget);

painter->setBrush(Qt::yellow);
painter->setRenderHint(QPainter::Antialiasing, true);

QRectF rect = boundingRect();
painter->drawEllipse(rect);
}

void CoinDragItem::touch(){
delete this;
};
20 changes: 20 additions & 0 deletions pacmanarea/dragitem/coindragitem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef COINDRAGITEM_H
#define COINDRAGITEM_H

#include "abstractdragitem.h"
#include "dragitemfabric.h"

class CoinDragItem : public AbstractDragItem{
public:
CoinDragItem(qreal x, qreal y, qreal boxSize);

public slots:
void touch() override;
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;

private:
int width_ = 1;
int height_ = 1;
};

#endif // COINDRAGITEM_H
Loading