diff --git a/2048-qt.pro b/2048-qt.pro index 6cefea8..a0ba4d6 100644 --- a/2048-qt.pro +++ b/2048-qt.pro @@ -3,7 +3,8 @@ TEMPLATE = app QT += qml quick widgets SOURCES += main.cpp \ - myclass.cpp + myclass.cpp \ + settings.cpp lupdate_only { SOURCES += qml/main.qml \ @@ -23,6 +24,7 @@ RC_ICONS = 2048.ico # On Windows ICON = 2048.ico # On Mac OSX HEADERS += \ - myclass.h + myclass.h \ + settings.h TRANSLATIONS = 2048-qt_zh_CN.ts diff --git a/main.cpp b/main.cpp index fbf2355..b800816 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include #include #include "myclass.h" +#include "settings.h" int main(int argc, char *argv[]) { @@ -18,11 +19,16 @@ int main(int argc, char *argv[]) app.installTranslator(&translator); QQmlApplicationEngine engine; - engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml"))); - MyClass myClass; // Access C++ object "myClass" from QML as "myClass" + MyClass myClass; engine.rootContext()->setContextProperty("myClass", &myClass); + // Access C++ object "settings" from QML as "settings" + Settings settings(0, "xiaoyong", "2048-Qt"); + engine.rootContext()->setContextProperty("settings", &settings); + + engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml"))); + return app.exec(); } diff --git a/qml/2048.js b/qml/2048.js index 6d720df..744ae07 100644 --- a/qml/2048.js +++ b/qml/2048.js @@ -1,5 +1,6 @@ var score = 0; -var bestScore = 0; +var bestScore = settings.value("bestScore", 0); + var gridSize = 4; var cellValues; var tileItems = []; @@ -9,6 +10,7 @@ var labels = "2048"; var labelFunc; var targetLevel = 11; var checkTargetFlag = true; +var tileComponent = Qt.createComponent("Tile.qml"); switch (labels) { case "2048": @@ -51,6 +53,13 @@ function startupFunction() { createNewTileItems(true); updateScore(0); addScoreText.parent = scoreBoard.itemAt(0); + + // Save the currently achieved best score + if (bestScore > settings.value("bestScore", 0)) { + console.log("Updating new high score..."); + settings.setValue("bestScore", bestScore); + } + console.log("Started a new game"); } @@ -339,13 +348,11 @@ function maxTileValue() { } function createTileObject(ind, n, isStartup) { - var component; var tile; var sty = computeTileStyle(n); var tileText = labelFunc(n); - component = Qt.createComponent("Tile.qml"); - tile = component.createObject(tileGrid, {"x": cells.itemAt(ind).x, "y": cells.itemAt(ind).y, "color": sty.bgColor, "tileColor": sty.fgColor, "tileFontSize": sty.fontSize, "tileText": tileText}); + tile = tileComponent.createObject(tileGrid, {"x": cells.itemAt(ind).x, "y": cells.itemAt(ind).y, "color": sty.bgColor, "tileColor": sty.fgColor, "tileFontSize": sty.fontSize, "tileText": tileText}); if (! isStartup) { tile.runNewTileAnim = true; } @@ -423,3 +430,11 @@ function moveMergeTilesUpDown(i, v, v2, indices, up) { } } } + +function cleanUpAndQuit() { + if (bestScore > settings.value("bestScore", 0)) { + console.log("Updating new high score..."); + settings.setValue("bestScore", bestScore); + } + Qt.quit(); +} diff --git a/qml/Tile.qml b/qml/Tile.qml index 7998415..5efad01 100644 --- a/qml/Tile.qml +++ b/qml/Tile.qml @@ -22,7 +22,10 @@ Rectangle { font.bold: true anchors.centerIn: parent Behavior on text { - PropertyAnimation { + PropertyAnimation { target: tileContainer + property: "opacity" + from: 0.5 + to: 1 duration: moveAnimTime } } diff --git a/qml/main.qml b/qml/main.qml index f36f93a..a10dffc 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -27,7 +27,7 @@ ApplicationWindow { MenuItem { text: qsTr("Exit") shortcut: "Ctrl+Q" - onTriggered: Qt.quit(); + onTriggered: MyScript.cleanUpAndQuit(); } } Menu { @@ -201,9 +201,7 @@ ApplicationWindow { onAccepted: { MyScript.startupFunction(); } - onRejected: { - Qt.quit(); - } + onRejected: MyScript.cleanUpAndQuit(); } MessageDialog { @@ -222,6 +220,7 @@ ApplicationWindow { } } - Component.onCompleted: MyScript.startupFunction() } + + Component.onCompleted: MyScript.startupFunction(); } diff --git a/settings.cpp b/settings.cpp new file mode 100644 index 0000000..5af4579 --- /dev/null +++ b/settings.cpp @@ -0,0 +1,17 @@ +#include "settings.h" + +Settings::Settings(QObject *parent, const QString &organization, const QString &application) : + QObject(parent), settings_(new QSettings(organization, application)) { +} + +Settings::~Settings() { + delete settings_; +} + +void Settings::setValue(const QString &key, const QVariant &value) { + settings_->setValue(key, value); +} + +QVariant Settings::value(const QString &key, const QVariant &defaultValue) const { + return settings_->value(key, defaultValue); +} diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..f39763c --- /dev/null +++ b/settings.h @@ -0,0 +1,26 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include +#include + +class Settings : public QObject +{ + Q_OBJECT +public: + explicit Settings(QObject *parent = 0, const QString &organization = QString(), const QString &application = QString()); + ~Settings(); + + Q_INVOKABLE void setValue(const QString &key, const QVariant &value); + Q_INVOKABLE QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const; + +signals: + +public slots: + +private: + QSettings *settings_; + +}; + +#endif // SETTINGS_H