Skip to content
Snippets Groups Projects
Commit 75f875f2 authored by Ruf Quentin's avatar Ruf Quentin
Browse files

Scores sauvegarde

Ajout de la sauvegarde du meilleur score même lorsque l'on quitte l'application via QSettings
parent 27205992
Branches
No related tags found
No related merge requests found
...@@ -81,6 +81,7 @@ Window { ...@@ -81,6 +81,7 @@ Window {
} }
Rectangle { Rectangle {
id: scoresRectangle
width: 300 width: 300
height: 60 height: 60
color: "#bbada0" color: "#bbada0"
...@@ -88,23 +89,30 @@ Window { ...@@ -88,23 +89,30 @@ Window {
Text { Text {
anchors.centerIn: parent anchors.centerIn: parent
text: "Scores" text: "Reset Best Score"
font.pixelSize: 24 font.pixelSize: 24
color: "white" color: "#ffffff"
}
MouseArea {
anchors.fill: parent
onClicked: {
game.resetHighScore()
}
} }
} }
Rectangle { Rectangle {
width: 300 width: 300
height: 60 height: 60
color: "#bbada0" color: "#8B0000"
radius: 10 radius: 10
Text { Text {
anchors.centerIn: parent anchors.centerIn: parent
text: "Quitter" text: "Quitter"
font.pixelSize: 24 font.pixelSize: 24
color: "white" color: "#ffffff"
} }
MouseArea { MouseArea {
......
...@@ -125,8 +125,17 @@ Item { ...@@ -125,8 +125,17 @@ Item {
x: 420; y: 50; width: 130; height: 65 x: 420; y: 50; width: 130; height: 65
color: "#bbada0"; radius: 15 color: "#bbada0"; radius: 15
Text { text: "Best"; y: 2; color: "#c1c1c1"; font.pixelSize: 20; anchors.horizontalCenter: parent.horizontalCenter } Text {
Text { id: best_score; y: 28; color: "#ffffff"; text: game.bestScore; font.pixelSize: 25; font.family: "Tahoma"; anchors.horizontalCenter: parent.horizontalCenter } 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 { Rectangle {
...@@ -173,4 +182,10 @@ Item { ...@@ -173,4 +182,10 @@ Item {
console.log("Signal GAME OVER reçu") console.log("Signal GAME OVER reçu")
} }
} }
Connections {
target: game
function onScoreChanged() {
game.updateHighScore(game.score);
}
}
} }
#include "game2048.h" #include "game2048.h"
#include <QDebug> #include <QDebug>
#include <QRandomGenerator> #include <QRandomGenerator>
#include <QSettings>
Game2048::Game2048(QObject *parent) Game2048::Game2048(QObject *parent)
: QObject(parent), m_score(0), m_bestScore(0) : QObject(parent), m_score(0), m_bestScore(0)
{ {
resetGame(); 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() { void Game2048::resetGame() {
......
...@@ -9,6 +9,7 @@ class Game2048 : public QObject { ...@@ -9,6 +9,7 @@ class Game2048 : public QObject {
Q_PROPERTY(int score READ score NOTIFY scoreChanged) Q_PROPERTY(int score READ score NOTIFY scoreChanged)
Q_PROPERTY(QVector<int> board READ board NOTIFY boardChanged) Q_PROPERTY(QVector<int> board READ board NOTIFY boardChanged)
Q_PROPERTY(int bestScore READ bestScore NOTIFY bestScoreChanged) Q_PROPERTY(int bestScore READ bestScore NOTIFY bestScoreChanged)
Q_PROPERTY(int highScore READ highScore NOTIFY highScoreChanged)
public: public:
explicit Game2048(QObject *parent = nullptr); explicit Game2048(QObject *parent = nullptr);
...@@ -19,7 +20,10 @@ public: ...@@ -19,7 +20,10 @@ public:
Q_INVOKABLE bool moveRight(); Q_INVOKABLE bool moveRight();
Q_INVOKABLE bool moveUp(); Q_INVOKABLE bool moveUp();
Q_INVOKABLE bool moveDown(); Q_INVOKABLE bool moveDown();
Q_INVOKABLE void updateHighScore(int score);
Q_INVOKABLE void resetHighScore();
int highScore() const;
int score() const; int score() const;
int bestScore() const; int bestScore() const;
QVector<int> board() const; // Convertir tableau 2D en vecteur plat pour QML QVector<int> board() const; // Convertir tableau 2D en vecteur plat pour QML
...@@ -29,6 +33,7 @@ signals: ...@@ -29,6 +33,7 @@ signals:
void bestScoreChanged(); void bestScoreChanged();
void boardChanged(); void boardChanged();
void gameOver(); void gameOver();
void highScoreChanged();
private: private:
int m_board[4][4]; int m_board[4][4];
...@@ -39,6 +44,11 @@ private: ...@@ -39,6 +44,11 @@ private:
void spawnTile(); void spawnTile();
bool canMove() const; bool canMove() const;
void updateGameState(); void updateGameState();
void loadHighScore();
void saveHighScore();
int m_highScore;
}; };
#endif // GAME2048_H #endif // GAME2048_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment