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

SOURCES += \
customgraphicsview.cpp \
directions.cpp \
gameelement.cpp \
hostile.cpp \
main.cpp \
mainwindow.cpp
mainwindow.cpp \
player.cpp \
redactor.cpp \
startgame.cpp

HEADERS += \
mainwindow.h
customgraphicsview.h \
directions.h \
gameelement.h \
hostile.h \
mainwindow.h \
player.h \
redactor.h \
startgame.h

FORMS += \
mainwindow.ui
mainwindow.ui \
redactor.ui \
startgame.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
resource.qrc
Binary file added coin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions customgraphicsview.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "customgraphicsview.h"

#include <QMouseEvent>
#include <QDebug>

CustomGraphicsView::CustomGraphicsView(QGraphicsScene *scene, QWidget *parent, int gridSize)
: QGraphicsView(scene, parent), cellSize_(gridSize)
{

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.

Раз начали не переносить скобки в части проекта, то не переносите их везде. Касается всего проекта. + Комментарий.

setMouseTracking(true);
}

void CustomGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if (isDrawing_)
{
int halfGridSize = cellSize_ / 2;
QPoint localMousePos = event->pos();
center_ = this->mapToScene(localMousePos);
center_.setX(this->center_.x() - halfGridSize);
center_.setY(this->center_.y() - halfGridSize);
update();
qDebug() << "cursor Coordinates: (" << center_.x() << ", " << center_.y() << ")";
}
}

void CustomGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && isDrawing_)
{
isDrawing_ = false;
setCursorStyle();
emit customMouseRelease();
}
}

void CustomGraphicsView::setCursorStyle()
{
if (isDrawing_)
{
setCursor(Qt::CrossCursor);
}
else
{
setCursor(Qt::ArrowCursor);
}
}

int CustomGraphicsView::getCenterX() const
{
return center_.x();
}

int CustomGraphicsView::getCenterY() const
{
return center_.y();
}

void CustomGraphicsView::setCenterX(int newX)
{
center_.setX(newX);
}

void CustomGraphicsView::setCenterY(int newY)
{
center_.setY(newY);
}

void CustomGraphicsView::setCenter(QPointF newCenter)
{
center_ = newCenter;
}

void CustomGraphicsView::setDrawing(bool isDrawing)
{
isDrawing_ = isDrawing;
}
35 changes: 35 additions & 0 deletions customgraphicsview.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef CUSTOMGRAPHICSVIEW_H
#define CUSTOMGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QMouseEvent>

class CustomGraphicsView : public QGraphicsView
{
Q_OBJECT

public:
CustomGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr, int gridSize = 64);

void setCursorStyle();
int getCenterX() const;
int getCenterY() const;
void setCenterX(int newX);
void setCenterY(int newY);
void setCenter(QPointF isDrawing);
void setDrawing(bool newDrawing);

protected:
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;

private:
int cellSize_;
QPointF center_;
bool isDrawing_ = false;

signals:
void customMouseRelease();
};

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

const std::string Directions::Left = "Left";
const std::string Directions::Right = "Right";
const std::string Directions::Up = "Up";
const std::string Directions::Down = "Down";

Directions::Directions(const std::string& type) : directionsType_(type) {}

std::string Directions::getType() const
{
return directionsType_;
}

std::ostream& operator<<(std::ostream& os, const Directions& direction)
{
os << direction.getType();
return os;
}

bool operator==(const Directions& left, const Directions& right)
{
return left.getType() == right.getType();
}
24 changes: 24 additions & 0 deletions directions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef DIRECTIONS_H
#define DIRECTIONS_H

#include <string>

class Directions {
public:
static const std::string Left;
static const std::string Right;
static const std::string Up;
static const std::string Down;

Directions(const std::string& type);

std::string getType() const;

friend std::ostream& operator<<(std::ostream& os, const Directions& direction);
friend bool operator==(const Directions& left, const Directions& right);

private:
std::string directionsType_;
};

#endif // DIRECTIONS_H
8 changes: 8 additions & 0 deletions gameelement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "gameelement.h"

GameElement::GameElement(ElementType type) : elementType_(type) {}

GameElement::ElementType GameElement::getType() const
{
return elementType_;
}
22 changes: 22 additions & 0 deletions gameelement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef GAMEELEMENT_H
#define GAMEELEMENT_H

class GameElement {
public:
enum ElementType {
Empty = 0,
Wall = 1,
Puckman = 2,
Hostile = 3,
Coin = 4
};

GameElement(ElementType type);

ElementType getType() const;

private:
ElementType elementType_;
};

#endif // GAMEELEMENT_H
Loading