diff --git a/Jeu_2048/Main.qml b/Jeu_2048/Main.qml index b03ce543bbce10402560fb1499c972a9adfcc73d..1320c160cf653a4bdac81d8623234f68ef96c4b8 100644 --- a/Jeu_2048/Main.qml +++ b/Jeu_2048/Main.qml @@ -81,6 +81,7 @@ Window { } Rectangle { + id: scoresRectangle width: 300 height: 60 color: "#bbada0" @@ -88,23 +89,30 @@ Window { Text { anchors.centerIn: parent - text: "Scores" + text: "Reset Best Score" font.pixelSize: 24 - color: "white" + color: "#ffffff" + } + + MouseArea { + anchors.fill: parent + onClicked: { + game.resetHighScore() + } } } Rectangle { width: 300 height: 60 - color: "#bbada0" + color: "#8B0000" radius: 10 Text { anchors.centerIn: parent text: "Quitter" font.pixelSize: 24 - color: "white" + color: "#ffffff" } MouseArea { diff --git a/Jeu_2048/Partie.qml b/Jeu_2048/Partie.qml index 7c5807cc672ce536a2fc4dd4eb7b275bf60b2622..90a82ccc8aa5af00e2d4b4de9d2d0cb4c63cfabf 100644 --- a/Jeu_2048/Partie.qml +++ b/Jeu_2048/Partie.qml @@ -125,8 +125,17 @@ Item { x: 420; y: 50; width: 130; height: 65 color: "#bbada0"; radius: 15 - Text { text: "Best"; y: 2; color: "#c1c1c1"; font.pixelSize: 20; anchors.horizontalCenter: parent.horizontalCenter } - Text { id: best_score; y: 28; color: "#ffffff"; text: game.bestScore; font.pixelSize: 25; font.family: "Tahoma"; anchors.horizontalCenter: parent.horizontalCenter } + Text { + text: "Best"; y: 2; color: "#c1c1c1"; font.pixelSize: 20; + anchors.horizontalCenter: parent.horizontalCenter + } + + Text { + id: best_score; y: 28; color: "#ffffff"; + text: game.highScore; + font.pixelSize: 25; font.family: "Tahoma"; + anchors.horizontalCenter: parent.horizontalCenter + } } Rectangle { @@ -173,4 +182,10 @@ Item { console.log("Signal GAME OVER reçu") } } + Connections { + target: game + function onScoreChanged() { + game.updateHighScore(game.score); + } + } } diff --git a/Jeu_2048/game2048.cpp b/Jeu_2048/game2048.cpp index 063072c9f707de11df9cd188153da6fa5d0bb8e4..b5bcd884ea69e71b0ef6acf985e7b149ca5171bc 100644 --- a/Jeu_2048/game2048.cpp +++ b/Jeu_2048/game2048.cpp @@ -1,12 +1,41 @@ #include "game2048.h" #include <QDebug> #include <QRandomGenerator> - +#include <QSettings> Game2048::Game2048(QObject *parent) : QObject(parent), m_score(0), m_bestScore(0) { resetGame(); + loadHighScore(); +} + +int Game2048::highScore() const { + return m_highScore; +} + +void Game2048::updateHighScore(int score) { + if (score > m_highScore) { + m_highScore = score; + saveHighScore(); + emit highScoreChanged(); + } +} + +void Game2048::loadHighScore() { + QSettings settings("MonJeu2048", "Jeu2048"); + m_highScore = settings.value("highScore", 0).toInt(); +} + +void Game2048::saveHighScore() { + QSettings settings("MonJeu2048", "Jeu2048"); + settings.setValue("highScore", m_highScore); +} + +void Game2048::resetHighScore() { + m_highScore = 0; + saveHighScore(); + emit highScoreChanged(); } void Game2048::resetGame() { diff --git a/Jeu_2048/game2048.h b/Jeu_2048/game2048.h index 9ab072533835d7c20312a3bff214b80b6463444e..731e762eabbe7c421eeea0ee68c3287c6bdb96b7 100644 --- a/Jeu_2048/game2048.h +++ b/Jeu_2048/game2048.h @@ -9,6 +9,7 @@ class Game2048 : public QObject { Q_PROPERTY(int score READ score NOTIFY scoreChanged) Q_PROPERTY(QVector<int> board READ board NOTIFY boardChanged) Q_PROPERTY(int bestScore READ bestScore NOTIFY bestScoreChanged) + Q_PROPERTY(int highScore READ highScore NOTIFY highScoreChanged) public: explicit Game2048(QObject *parent = nullptr); @@ -19,7 +20,10 @@ public: Q_INVOKABLE bool moveRight(); Q_INVOKABLE bool moveUp(); Q_INVOKABLE bool moveDown(); + Q_INVOKABLE void updateHighScore(int score); + Q_INVOKABLE void resetHighScore(); + int highScore() const; int score() const; int bestScore() const; QVector<int> board() const; // Convertir tableau 2D en vecteur plat pour QML @@ -29,6 +33,7 @@ signals: void bestScoreChanged(); void boardChanged(); void gameOver(); + void highScoreChanged(); private: int m_board[4][4]; @@ -39,6 +44,11 @@ private: void spawnTile(); bool canMove() const; void updateGameState(); + + void loadHighScore(); + void saveHighScore(); + + int m_highScore; }; #endif // GAME2048_H