Skip to content

Commit

Permalink
Add pause screen
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisD3D committed Mar 21, 2024
1 parent 912f93b commit 0a7cbb6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/jeu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Jeu::Jeu(const Jeu &jeu): map(jeu.map), snake(jeu.snake) {
dirSnake = jeu.dirSnake;
std::random_device rd;
gen.seed(rd());
pause = jeu.pause;
}

Jeu::~Jeu() {
Expand All @@ -28,6 +29,7 @@ Jeu &Jeu::operator=(const Jeu &jeu) {
dirSnake = jeu.dirSnake;
snake = jeu.snake;
map = jeu.map;
pause = jeu.pause;

return *this;
}
Expand Down Expand Up @@ -58,6 +60,9 @@ bool Jeu::init() {
}

void Jeu::evolue() {
if (pause) // Game is paused, don't do anything
return;

if (!directionsBuffer.empty()) {
dirSnake = directionsBuffer.front();
directionsBuffer.pop();
Expand Down
9 changes: 9 additions & 0 deletions src/jeu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Jeu {
std::queue<Direction> directionsBuffer;
Position applePos = Position(-1, -1);
std::mt19937 gen;
bool pause = false;

public:
Jeu();
Expand Down Expand Up @@ -42,6 +43,14 @@ class Jeu {
const Position *getApplePos() const;

const Map &getMap() const;

void togglePause() {
pause = !pause;
}

bool isPaused() const {
return pause;
}
};

#endif
36 changes: 36 additions & 0 deletions src/screens/gamescreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ GameScreen::GameScreen(QWidget *parent, const QString &file_info): QWidget(paren
auto *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &GameScreen::handleTimer);
timer->start(100);


// Pause overlay
pauseOverlay = new QWidget(this);
pauseOverlay->setStyleSheet("background-color: rgba(0, 0, 0, 127); color: white; font-size: 24px;");
pauseOverlay->hide(); // hide it initially

auto *pauseLabel = new QLabel("Game Paused", pauseOverlay);
pauseLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);

auto *resumeLabel = new QLabel("Press P to resume", pauseOverlay); // New label
resumeLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
resumeLabel->setStyleSheet("font-size: 18px;");

auto *pauseLayout = new QVBoxLayout(pauseOverlay);
pauseLayout->setContentsMargins(0, 0, 0, 0);
pauseLayout->setSpacing(0);
pauseLayout->addWidget(pauseLabel);
pauseLayout->addWidget(resumeLabel);
}

void GameScreen::resizeEvent(QResizeEvent* event) {
QWidget::resizeEvent(event);
pauseOverlay->resize(event->size());
}

/**
Expand All @@ -40,6 +64,18 @@ void GameScreen::keyPressEvent(QKeyEvent *event) {
jeu.setDirection(HAUT);
if (event->key() == Qt::Key_Down)
jeu.setDirection(BAS);

if (event->key() == Qt::Key_P) {
jeu.togglePause();

if (jeu.isPaused()) {
pauseOverlay->show();
} else {
pauseOverlay->hide();
}
}


update();
}

Expand Down
3 changes: 3 additions & 0 deletions src/screens/gamescreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ class GameScreen final : public QWidget {
Q_OBJECT

GameArea *gameArea;
QWidget *pauseOverlay;

public:
Jeu jeu;

explicit GameScreen(QWidget *parent = nullptr, const QString &file_info = "map1.skm");

void resizeEvent(QResizeEvent *event);

void handleTimer();

protected:
Expand Down

0 comments on commit 0a7cbb6

Please sign in to comment.