added Settings function. The best score is saved on disk
This commit is contained in:
parent
6bd460f88c
commit
8f2378ada1
@ -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
|
||||
|
10
main.cpp
10
main.cpp
@ -4,6 +4,7 @@
|
||||
#include <QTranslator>
|
||||
#include <QDebug>
|
||||
#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();
|
||||
}
|
||||
|
23
qml/2048.js
23
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();
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
17
settings.cpp
Normal file
17
settings.cpp
Normal file
@ -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);
|
||||
}
|
26
settings.h
Normal file
26
settings.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef SETTINGS_H
|
||||
#define SETTINGS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QSettings>
|
||||
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user