From 75f875f2649cce208ea50d77e732ce3e6a1f8353 Mon Sep 17 00:00:00 2001 From: Ruf Quentin <quentin.ruf@etu.ec-lyon.fr> Date: Sat, 12 Apr 2025 23:20:21 +0200 Subject: [PATCH] Scores sauvegarde MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajout de la sauvegarde du meilleur score même lorsque l'on quitte l'application via QSettings --- Jeu_2048/Main.qml | 16 ++++++++++++---- Jeu_2048/Partie.qml | 19 +++++++++++++++++-- Jeu_2048/game2048.cpp | 31 ++++++++++++++++++++++++++++++- Jeu_2048/game2048.h | 10 ++++++++++ 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/Jeu_2048/Main.qml b/Jeu_2048/Main.qml index b03ce54..1320c16 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 7c5807c..90a82cc 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 063072c..b5bcd88 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 9ab0725..731e762 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 -- GitLab