From 4548f2f0e2f5d21b0926355acc72f3ae1bb0ab51 Mon Sep 17 00:00:00 2001 From: Augustin Date: Sat, 6 Apr 2024 22:43:24 +0200 Subject: [PATCH] loose --- CMakeLists.txt | 2 +- src/jeu.cpp | 1 + src/jeu.hpp | 2 ++ src/screens/endgamescreen.cpp | 20 ++++++++++++++++++++ src/screens/endgamescreen.hpp | 20 ++++++++++++++++++++ src/screens/gamescreen.cpp | 18 ++++++++++++++++++ src/screens/gamescreen.hpp | 5 +++++ src/screens/snakewindow.cpp | 20 ++++++++++++++++++++ src/screens/snakewindow.hpp | 7 +++++++ 9 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/screens/endgamescreen.cpp create mode 100644 src/screens/endgamescreen.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 84e588c..c7a4bd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,7 @@ add_executable(SnakeQT ${RESOURCE_ADDED} src/screens/snakewindow.cpp src/screens/gamearea.cpp src/screens/browsemapscreen.cpp -) + src/screens/endgamescreen.hpp src/screens/endgamescreen.cpp) target_link_libraries(SnakeQT Qt5::Core diff --git a/src/jeu.cpp b/src/jeu.cpp index 2a11c87..67ec1b1 100644 --- a/src/jeu.cpp +++ b/src/jeu.cpp @@ -108,6 +108,7 @@ void Jeu::tick() { } else { // Game over snake.clear(); // TODO: lose condition + game_over = -1; // perdu } } diff --git a/src/jeu.hpp b/src/jeu.hpp index b6b5e81..619500c 100644 --- a/src/jeu.hpp +++ b/src/jeu.hpp @@ -20,6 +20,7 @@ class Jeu { private: int score = 0; + public: Jeu(); @@ -61,6 +62,7 @@ class Jeu { void increaseScore(int point) { score=score+point; } + int game_over; // 0: not over, 1: win, -1: lose }; #endif diff --git a/src/screens/endgamescreen.cpp b/src/screens/endgamescreen.cpp new file mode 100644 index 0000000..a912597 --- /dev/null +++ b/src/screens/endgamescreen.cpp @@ -0,0 +1,20 @@ + + +#include "endgamescreen.hpp" + +EndGameScreen::EndGameScreen(int score, QWidget *parent) + : QWidget(parent), scoreLabel(new QLabel(this)) { + // Layout vertical pour organiser les widgets + QVBoxLayout *layout = new QVBoxLayout(this); + + // Étiquette de texte pour afficher le score + QLabel *scoreLabel = new QLabel("Score: " + QString::number(score), this); + scoreLabel->setFont(QFont("Arial", 24, QFont::Bold)); // Police en gras et taille 24 + scoreLabel->setAlignment(Qt::AlignCenter); // Centrez le texte + + // Ajouter l'étiquette de score au layout + layout->addWidget(scoreLabel); + + // Définir le layout pour la fenêtre + setLayout(layout); +} \ No newline at end of file diff --git a/src/screens/endgamescreen.hpp b/src/screens/endgamescreen.hpp new file mode 100644 index 0000000..96fe8cd --- /dev/null +++ b/src/screens/endgamescreen.hpp @@ -0,0 +1,20 @@ + + +#ifndef SNAKEQT_ENDGAMESCREEN_HPP +#define SNAKEQT_ENDGAMESCREEN_HPP + +#include +#include +#include + +class EndGameScreen : public QWidget { +Q_OBJECT + +public: + explicit EndGameScreen(int score, QWidget *parent = nullptr); + +private: + QLabel *scoreLabel; +}; + +#endif //SNAKEQT_ENDGAMESCREEN_HPP diff --git a/src/screens/gamescreen.cpp b/src/screens/gamescreen.cpp index 89ebed7..70b074c 100644 --- a/src/screens/gamescreen.cpp +++ b/src/screens/gamescreen.cpp @@ -45,6 +45,8 @@ GameScreen::GameScreen(QWidget *parent, const QString &file_info): QWidget(paren pauseLayout->setSpacing(0); pauseLayout->addWidget(pauseLabel); pauseLayout->addWidget(resumeLabel); + + } void GameScreen::resizeEvent(QResizeEvent* event) { @@ -83,6 +85,10 @@ void GameScreen::updateScoreLabel() { scoreLabel->setText("Score: " + QString::number(jeu.getScore())); } +void GameScreen::endGame() { + emit gameOver(jeu.getScore()); +} + /** * Tick the game */ @@ -91,6 +97,18 @@ void GameScreen::handleTimer() { updateScoreLabel(); + if (jeu.game_over != 0) { + // Game over + if (jeu.game_over == 1) { + // Win + std::cout << "You win!" << std::endl; + } else { + // Lose + std::cout << "You lose!" << std::endl; + emit gameOver(jeu.getScore()); + } + + } update(); diff --git a/src/screens/gamescreen.hpp b/src/screens/gamescreen.hpp index c12f533..15662fa 100644 --- a/src/screens/gamescreen.hpp +++ b/src/screens/gamescreen.hpp @@ -29,5 +29,10 @@ class GameScreen final : public QWidget { private: QLabel *scoreLabel; void updateScoreLabel(); + void endGame(); + +signals: + void gameOver(int score); + }; #endif //GAMESCREEN_HPP diff --git a/src/screens/snakewindow.cpp b/src/screens/snakewindow.cpp index 93dd0fd..6c44f38 100644 --- a/src/screens/snakewindow.cpp +++ b/src/screens/snakewindow.cpp @@ -51,6 +51,8 @@ SnakeWindow::SnakeWindow(QWidget *pParent, Qt::WindowFlags flags) stackedWidget->addWidget(gameScreen); stackedWidget->setCurrentWidget(gameScreen); } + + }); const auto editorMenu = new QMenu(tr("&Editor"), this); @@ -84,6 +86,9 @@ SnakeWindow::SnakeWindow(QWidget *pParent, Qt::WindowFlags flags) // Set the menu bar setMenuBar(menuBar); + + + } void SnakeWindow::handleStartGameClicked() { @@ -91,6 +96,9 @@ void SnakeWindow::handleStartGameClicked() { gameScreen = new GameScreen(this); stackedWidget->addWidget(gameScreen); stackedWidget->setCurrentWidget(gameScreen); + + connect(gameScreen,&GameScreen::gameOver, this, &SnakeWindow::handleGameOver); + setCentralWidget(gameScreen); } void SnakeWindow::handleBrowseMapClicked() { @@ -106,6 +114,7 @@ void SnakeWindow::handleBrowseMapClicked() { gameScreen = new GameScreen(this, fileName); stackedWidget->addWidget(gameScreen); stackedWidget->setCurrentWidget(gameScreen); + }); } @@ -113,3 +122,14 @@ void SnakeWindow::handleExitClicked() { // Exit the application when the "Exit" button is clicked QApplication::quit(); } + +void SnakeWindow::handleGameOver(int score) { + // Switch to the end game screen when the game is over + delete gameScreen; + gameScreen = nullptr; + + endGameScreen = new EndGameScreen(score, this); + setCentralWidget(endGameScreen); + + update(); +} \ No newline at end of file diff --git a/src/screens/snakewindow.hpp b/src/screens/snakewindow.hpp index ddf9b0c..62bc4fc 100644 --- a/src/screens/snakewindow.hpp +++ b/src/screens/snakewindow.hpp @@ -8,6 +8,7 @@ #include "editorscreen.hpp" #include "mainmenu.hpp" #include "gamescreen.hpp" +#include "endgamescreen.hpp" class SnakeWindow final : public QMainWindow { Q_OBJECT @@ -19,6 +20,7 @@ class SnakeWindow final : public QMainWindow { BrowseMapScreen *browseMapScreen{}; EditorScreen *editorScreen{}; + public: explicit SnakeWindow(QWidget *pParent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); @@ -27,6 +29,8 @@ public slots: void handleBrowseMapClicked(); + void handleGameOver(int score); + static void handleExitClicked(); void toggleFullScreen() { @@ -36,6 +40,9 @@ public slots: showFullScreen(); } } +private : + EndGameScreen *endGameScreen; + }; #endif