From 6bd460f88cfc9db56911684ac46dbecaaa2aa34d Mon Sep 17 00:00:00 2001 From: Qiaoyong Zhong Date: Sun, 20 Apr 2014 15:45:02 +0800 Subject: [PATCH] added Chinese translation and the adding score animation --- 2048-qt.pro | 7 ++++++ 2048-qt_zh_CN.qm | Bin 0 -> 734 bytes 2048-qt_zh_CN.ts | 59 +++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 18 +++++++++++---- qml/2048.js | 16 +++++++++---- qml/Tile.qml | 2 -- qml/main.qml | 55 ++++++++++++++++++++++++++++++++++--------- 7 files changed, 135 insertions(+), 22 deletions(-) create mode 100644 2048-qt_zh_CN.qm create mode 100644 2048-qt_zh_CN.ts diff --git a/2048-qt.pro b/2048-qt.pro index 14a5323..6cefea8 100644 --- a/2048-qt.pro +++ b/2048-qt.pro @@ -5,6 +5,11 @@ QT += qml quick widgets SOURCES += main.cpp \ myclass.cpp +lupdate_only { +SOURCES += qml/main.qml \ + qml/2048.js +} + RESOURCES += qml.qrc # Additional import path used to resolve QML modules in Qt Creator's code model @@ -19,3 +24,5 @@ ICON = 2048.ico # On Mac OSX HEADERS += \ myclass.h + +TRANSLATIONS = 2048-qt_zh_CN.ts diff --git a/2048-qt_zh_CN.qm b/2048-qt_zh_CN.qm new file mode 100644 index 0000000000000000000000000000000000000000..5e3784698ec98c8ba42dd20ae9fefdc8f9701fac GIT binary patch literal 734 zcmcE7ks@*G{hX<16=n7(EZlq7iGhJ3gMlTh1xSZ8u$*TB(p3yB7gT}tYzCG$1`G_0 z!VKXpsX+QHLwf8yApM@{rpiYkZNglt2-3fYJxHVwNIzp=@|YD!GjQf4egRT?oUe82 z85o#Ed8S-01*%_`3FO2u17&y^j2H|UOc*TkERy&+Kw8*#6rP6F*lVR$YaS( z%*Bi{U`cKF`-V{9mYIX@8jzdoE~m%C6>)-`tl(dkT7+sB7fj`UMz~roxLQS2#Vm1p zK`Y_PSUgg53Q!ejBz2`0M2DCC2wL!;Cw_X60fP-g5`!HqB!N7A5Fg1dL$CbIJcW{s zRE50K+@#c^Vui%K6ovHE5`~g{u&_;%9XQBKfS$C`PqIUE5?ks9l=$KBOD)F|JuE=S zAi|tA*x5hG71il%{t3NFer<5yu}9{YDuid|p{f#rsQS;?*;x0y?rrhP5b^)=a6M8W dJ>{8siVDv8c_o>7rKt)9If<2-dFl2DV*sk5nPC6` literal 0 HcmV?d00001 diff --git a/2048-qt_zh_CN.ts b/2048-qt_zh_CN.ts new file mode 100644 index 0000000..ff7b020 --- /dev/null +++ b/2048-qt_zh_CN.ts @@ -0,0 +1,59 @@ + + + + + main + + 2048 Game + 2048游戏 + + + File + 文件 + + + New Game + 新游戏 + + + Exit + 退出 + + + Help + 帮助 + + + About Qt + 关于Qt + + + SCORE + 得分 + + + BEST + 最高分 + + + Join the numbers and get to the <b>2048 tile</b>! + 把数字相加,得到<b>2048</b>! + + + Game Over + 游戏结束 + + + Game Over! + 游戏结束! + + + You Win + 你赢了 + + + You win! Continue playing? + 你赢了!要继续玩吗? + + + diff --git a/main.cpp b/main.cpp index 4ea78ff..fbf2355 100644 --- a/main.cpp +++ b/main.cpp @@ -1,20 +1,28 @@ #include #include +#include +#include +#include #include "myclass.h" -//#include int main(int argc, char *argv[]) { QApplication app(argc, argv); + // Localization + QString locale = QLocale::system().name(); + qDebug() << "Locale: " + locale; + + QTranslator translator; + translator.load("2048-qt_" + locale); + app.installTranslator(&translator); + QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml"))); MyClass myClass; - -// qDebug() << engine.rootObjects().length(); - QObject *mainWindow = engine.rootObjects()[0]; - QObject::connect(mainWindow, SIGNAL(helpMenuTriggered()), &myClass, SLOT(aboutQt())); + // Access C++ object "myClass" from QML as "myClass" + engine.rootContext()->setContextProperty("myClass", &myClass); return app.exec(); } diff --git a/qml/2048.js b/qml/2048.js index cb76f3c..6d720df 100644 --- a/qml/2048.js +++ b/qml/2048.js @@ -49,7 +49,8 @@ function startupFunction() { updateAvailableCells(); createNewTileItems(true); - updateScore(); + updateScore(0); + addScoreText.parent = scoreBoard.itemAt(0); console.log("Started a new game"); } @@ -139,7 +140,7 @@ function moveKey(event) { if (bestScore < score) { bestScore = score; } - updateScore(); + updateScore(oldScore); if (checkTargetFlag && maxTileValue() >= targetLevel) { winMessage.open(); } @@ -256,7 +257,12 @@ function createNewTileItems(isStartup) { } } -function updateScore() { +function updateScore(oldScore) { + if (score > oldScore) { + addScoreText.text = "+" + (score-oldScore).toString(); + addScoreAnim.running = true; + } + scoreBoard.itemAt(0).scoreText = MyScript.score.toString(); scoreBoard.itemAt(1).scoreText = MyScript.bestScore.toString(); } @@ -344,7 +350,7 @@ function createTileObject(ind, n, isStartup) { tile.runNewTileAnim = true; } - if (tile == null) { + if (tile === null) { // Error Handling console.log("Error creating a new tile"); } @@ -366,6 +372,7 @@ function moveMergeTilesLeftRight(i, v, v2, indices, left) { // Move and merge tileItems[gridSize*i+j].destroyFlag = true; tileItems[gridSize*i+j].z = -1; + tileItems[gridSize*i+j].opacity = 0; tileItems[gridSize*i+j].x = cells.itemAt(gridSize*i+indices[j]).x; // tileItems[gridSize*i+j].destroy(); @@ -398,6 +405,7 @@ function moveMergeTilesUpDown(i, v, v2, indices, up) { // Move and merge tileItems[gridSize*j+i].destroyFlag = true; tileItems[gridSize*j+i].z = -1; + tileItems[gridSize*j+i].opacity = 0; tileItems[gridSize*j+i].y = cells.itemAt(gridSize*indices[j]+i).y; // tileItems[gridSize*j+i].destroy(); diff --git a/qml/Tile.qml b/qml/Tile.qml index a1a31cd..7998415 100644 --- a/qml/Tile.qml +++ b/qml/Tile.qml @@ -55,7 +55,6 @@ Rectangle { Behavior on y { NumberAnimation { - easing.type: Easing.InQuad duration: moveAnimTime onRunningChanged: { if ((!running) && destroyFlag) { @@ -67,7 +66,6 @@ Rectangle { Behavior on x { NumberAnimation { - easing.type: Easing.InQuad duration: moveAnimTime onRunningChanged: { if ((!running) && destroyFlag) { diff --git a/qml/main.qml b/qml/main.qml index 5b296b2..f36f93a 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -16,8 +16,6 @@ ApplicationWindow { x: (Screen.width - width) / 2 y: (Screen.height - height) / 2 - signal helpMenuTriggered - menuBar: MenuBar { Menu { title: qsTr("File") @@ -36,8 +34,8 @@ ApplicationWindow { id: helpMenu title: qsTr("Help") MenuItem { - text: "About Qt" - onTriggered: mainWindow.helpMenuTriggered() + text: qsTr("About Qt") + onTriggered: myClass.aboutQt(); } } } @@ -85,7 +83,7 @@ ApplicationWindow { color: helper.myColors.bgdark property string scoreText: (index === 0) ? MyScript.score.toString() : MyScript.bestScore.toString() Text { - text: (index == 0) ? "SCORE" : "BEST" + text: (index == 0) ? qsTr("SCORE") : qsTr("BEST") anchors.horizontalCenter: parent.horizontalCenter y: 7 font.pixelSize: 13 @@ -101,13 +99,48 @@ ApplicationWindow { } } } + + Text { + id: addScoreText + font.pixelSize: 25 + font.bold: true + color: Qt.rgba(119/255, 110/255, 101/255, 0.9); +// parent: scoreBoard.itemAt(0) + anchors.horizontalCenter: parent.horizontalCenter + + property bool runAddScore: false + property real yfrom: 0 + property real yto: -(parent.y + parent.height) + property int addScoreAnimTime: 600 + + ParallelAnimation { + id: addScoreAnim + running: false + + NumberAnimation { + target: addScoreText + property: "y" + from: addScoreText.yfrom + to: addScoreText.yto + duration: addScoreText.addScoreAnimTime + + } + NumberAnimation { + target: addScoreText + property: "opacity" + from: 1 + to: 0 + duration: addScoreText.addScoreAnimTime + } + } + } } Text { id: banner y: 90 height: 40 - text: "Join the numbers and get to the 2048 tile!" + text: qsTr("Join the numbers and get to the 2048 tile!") color: helper.myColors.fgdark font.pixelSize: 16 verticalAlignment: Text.AlignVCenter @@ -125,7 +158,7 @@ ApplicationWindow { radius: 3 Text{ anchors.centerIn: parent - text: "New Game" + text: qsTr("New Game") color: helper.myColors.fgbutton font.pixelSize: 18 font.bold: true @@ -162,8 +195,8 @@ ApplicationWindow { MessageDialog { id: deadMessage - title: "Game Over" - text: "Game Over" + title: qsTr("Game Over") + text: qsTr("Game Over!") standardButtons: StandardButton.Retry | StandardButton.Abort onAccepted: { MyScript.startupFunction(); @@ -175,8 +208,8 @@ ApplicationWindow { MessageDialog { id: winMessage - title: "You Win" - text: "You win! Continue playing?" + title: qsTr("You Win") + text: qsTr("You win! Continue playing?") standardButtons: StandardButton.Yes | StandardButton.No onYes: { MyScript.checkTargetFlag = false;