From ca02d38299531b1ea8969aec796d66240fd4818a Mon Sep 17 00:00:00 2001 From: Augustin Date: Sat, 6 Apr 2024 00:25:16 +0200 Subject: [PATCH] score --- src/jeu.cpp | 4 ++++ src/jeu.hpp | 10 ++++++++++ src/screens/gamearea.hpp | 2 ++ src/screens/gamescreen.cpp | 14 ++++++++++++-- src/screens/gamescreen.hpp | 5 +++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/jeu.cpp b/src/jeu.cpp index 470e6fa..9b15469 100644 --- a/src/jeu.cpp +++ b/src/jeu.cpp @@ -1,4 +1,5 @@ #include "jeu.hpp" +#include using namespace std; @@ -85,6 +86,9 @@ void Jeu::tick() { snake.push_front(posTest); // Add the new head if (posTest == applePos) { + + increaseScore(1); + std::cout << "Snake eats the apple!" << std::endl; // The snake eats the apple, place a new apple std::uniform_int_distribution<> distr(0, map.getWidth() - 1); int attempts = 0; diff --git a/src/jeu.hpp b/src/jeu.hpp index 7482d8a..b6b5e81 100644 --- a/src/jeu.hpp +++ b/src/jeu.hpp @@ -17,6 +17,9 @@ class Jeu { std::mt19937 gen; bool pause = false; +private: + int score = 0; + public: Jeu(); @@ -51,6 +54,13 @@ class Jeu { bool isPaused() const { return pause; } + + int getScore() const { + return score; + } + void increaseScore(int point) { + score=score+point; + } }; #endif diff --git a/src/screens/gamearea.hpp b/src/screens/gamearea.hpp index 2fac5b1..88b4044 100644 --- a/src/screens/gamearea.hpp +++ b/src/screens/gamearea.hpp @@ -1,11 +1,13 @@ #ifndef GAMEAREA_HPP #define GAMEAREA_HPP + #include #include #include "../map.hpp" #include "../constants.hpp" +#include class GameArea final : public QWidget { Q_OBJECT diff --git a/src/screens/gamescreen.cpp b/src/screens/gamescreen.cpp index da01e81..89ebed7 100644 --- a/src/screens/gamescreen.cpp +++ b/src/screens/gamescreen.cpp @@ -1,5 +1,5 @@ #include "gamescreen.hpp" - +#include /** * Construct main game window @@ -12,7 +12,7 @@ GameScreen::GameScreen(QWidget *parent, const QString &file_info): QWidget(paren auto *layout = new QVBoxLayout; auto *mapNameLabel = new QLabel(jeu.getMap().getName() + " by " + jeu.getMap().getAuthor(), this); - auto *scoreLabel = new QLabel("Score: 0", this); + scoreLabel = new QLabel("Score: 0", this); layout->addWidget(mapNameLabel); layout->addWidget(gameArea); @@ -75,13 +75,23 @@ void GameScreen::keyPressEvent(QKeyEvent *event) { } } + update(); } +void GameScreen::updateScoreLabel() { + scoreLabel->setText("Score: " + QString::number(jeu.getScore())); +} + /** * Tick the game */ void GameScreen::handleTimer() { jeu.tick(); + + updateScoreLabel(); + + + update(); } diff --git a/src/screens/gamescreen.hpp b/src/screens/gamescreen.hpp index 694ac2b..c12f533 100644 --- a/src/screens/gamescreen.hpp +++ b/src/screens/gamescreen.hpp @@ -22,7 +22,12 @@ class GameScreen final : public QWidget { void handleTimer(); + protected: void keyPressEvent(QKeyEvent *) override; + +private: + QLabel *scoreLabel; + void updateScoreLabel(); }; #endif //GAMESCREEN_HPP