From 0a7cbb6341bad23c422f850ce5b1d9923538d14f Mon Sep 17 00:00:00 2001
From: DenisD3D
Date: Thu, 21 Mar 2024 17:29:09 +0100
Subject: [PATCH] Add pause screen
---
src/jeu.cpp | 5 +++++
src/jeu.hpp | 9 +++++++++
src/screens/gamescreen.cpp | 36 ++++++++++++++++++++++++++++++++++++
src/screens/gamescreen.hpp | 3 +++
4 files changed, 53 insertions(+)
diff --git a/src/jeu.cpp b/src/jeu.cpp
index a10343e..dc48df6 100644
--- a/src/jeu.cpp
+++ b/src/jeu.cpp
@@ -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() {
@@ -28,6 +29,7 @@ Jeu &Jeu::operator=(const Jeu &jeu) {
dirSnake = jeu.dirSnake;
snake = jeu.snake;
map = jeu.map;
+ pause = jeu.pause;
return *this;
}
@@ -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();
diff --git a/src/jeu.hpp b/src/jeu.hpp
index 467775c..50a0c02 100644
--- a/src/jeu.hpp
+++ b/src/jeu.hpp
@@ -15,6 +15,7 @@ class Jeu {
std::queue directionsBuffer;
Position applePos = Position(-1, -1);
std::mt19937 gen;
+ bool pause = false;
public:
Jeu();
@@ -42,6 +43,14 @@ class Jeu {
const Position *getApplePos() const;
const Map &getMap() const;
+
+ void togglePause() {
+ pause = !pause;
+ }
+
+ bool isPaused() const {
+ return pause;
+ }
};
#endif
diff --git a/src/screens/gamescreen.cpp b/src/screens/gamescreen.cpp
index ad1bf3e..9608a2d 100644
--- a/src/screens/gamescreen.cpp
+++ b/src/screens/gamescreen.cpp
@@ -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());
}
/**
@@ -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();
}
diff --git a/src/screens/gamescreen.hpp b/src/screens/gamescreen.hpp
index 9d129d4..694ac2b 100644
--- a/src/screens/gamescreen.hpp
+++ b/src/screens/gamescreen.hpp
@@ -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: