added Chinese translation and the adding score animation
This commit is contained in:
parent
828e0ba144
commit
6bd460f88c
@ -5,6 +5,11 @@ QT += qml quick widgets
|
|||||||
SOURCES += main.cpp \
|
SOURCES += main.cpp \
|
||||||
myclass.cpp
|
myclass.cpp
|
||||||
|
|
||||||
|
lupdate_only {
|
||||||
|
SOURCES += qml/main.qml \
|
||||||
|
qml/2048.js
|
||||||
|
}
|
||||||
|
|
||||||
RESOURCES += qml.qrc
|
RESOURCES += qml.qrc
|
||||||
|
|
||||||
# Additional import path used to resolve QML modules in Qt Creator's code model
|
# 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 += \
|
HEADERS += \
|
||||||
myclass.h
|
myclass.h
|
||||||
|
|
||||||
|
TRANSLATIONS = 2048-qt_zh_CN.ts
|
||||||
|
BIN
2048-qt_zh_CN.qm
Normal file
BIN
2048-qt_zh_CN.qm
Normal file
Binary file not shown.
59
2048-qt_zh_CN.ts
Normal file
59
2048-qt_zh_CN.ts
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!DOCTYPE TS>
|
||||||
|
<TS version="2.1" language="zh_CN">
|
||||||
|
<context>
|
||||||
|
<name>main</name>
|
||||||
|
<message>
|
||||||
|
<source>2048 Game</source>
|
||||||
|
<translation>2048游戏</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>File</source>
|
||||||
|
<translation>文件</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>New Game</source>
|
||||||
|
<translation>新游戏</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>Exit</source>
|
||||||
|
<translation>退出</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>Help</source>
|
||||||
|
<translation>帮助</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>About Qt</source>
|
||||||
|
<translation>关于Qt</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>SCORE</source>
|
||||||
|
<translation>得分</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>BEST</source>
|
||||||
|
<translation>最高分</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>Join the numbers and get to the <b>2048 tile</b>!</source>
|
||||||
|
<translation>把数字相加,得到<b>2048</b>!</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>Game Over</source>
|
||||||
|
<translation>游戏结束</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>Game Over!</source>
|
||||||
|
<translation>游戏结束!</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>You Win</source>
|
||||||
|
<translation>你赢了</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>You win! Continue playing?</source>
|
||||||
|
<translation>你赢了!要继续玩吗?</translation>
|
||||||
|
</message>
|
||||||
|
</context>
|
||||||
|
</TS>
|
18
main.cpp
18
main.cpp
@ -1,20 +1,28 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QQmlApplicationEngine>
|
#include <QQmlApplicationEngine>
|
||||||
|
#include <QQmlContext>
|
||||||
|
#include <QTranslator>
|
||||||
|
#include <QDebug>
|
||||||
#include "myclass.h"
|
#include "myclass.h"
|
||||||
//#include <QDebug>
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QApplication app(argc, 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;
|
QQmlApplicationEngine engine;
|
||||||
engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
|
engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
|
||||||
|
|
||||||
MyClass myClass;
|
MyClass myClass;
|
||||||
|
// Access C++ object "myClass" from QML as "myClass"
|
||||||
// qDebug() << engine.rootObjects().length();
|
engine.rootContext()->setContextProperty("myClass", &myClass);
|
||||||
QObject *mainWindow = engine.rootObjects()[0];
|
|
||||||
QObject::connect(mainWindow, SIGNAL(helpMenuTriggered()), &myClass, SLOT(aboutQt()));
|
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
16
qml/2048.js
16
qml/2048.js
@ -49,7 +49,8 @@ function startupFunction() {
|
|||||||
|
|
||||||
updateAvailableCells();
|
updateAvailableCells();
|
||||||
createNewTileItems(true);
|
createNewTileItems(true);
|
||||||
updateScore();
|
updateScore(0);
|
||||||
|
addScoreText.parent = scoreBoard.itemAt(0);
|
||||||
console.log("Started a new game");
|
console.log("Started a new game");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,7 +140,7 @@ function moveKey(event) {
|
|||||||
if (bestScore < score) {
|
if (bestScore < score) {
|
||||||
bestScore = score;
|
bestScore = score;
|
||||||
}
|
}
|
||||||
updateScore();
|
updateScore(oldScore);
|
||||||
if (checkTargetFlag && maxTileValue() >= targetLevel) {
|
if (checkTargetFlag && maxTileValue() >= targetLevel) {
|
||||||
winMessage.open();
|
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(0).scoreText = MyScript.score.toString();
|
||||||
scoreBoard.itemAt(1).scoreText = MyScript.bestScore.toString();
|
scoreBoard.itemAt(1).scoreText = MyScript.bestScore.toString();
|
||||||
}
|
}
|
||||||
@ -344,7 +350,7 @@ function createTileObject(ind, n, isStartup) {
|
|||||||
tile.runNewTileAnim = true;
|
tile.runNewTileAnim = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tile == null) {
|
if (tile === null) {
|
||||||
// Error Handling
|
// Error Handling
|
||||||
console.log("Error creating a new tile");
|
console.log("Error creating a new tile");
|
||||||
}
|
}
|
||||||
@ -366,6 +372,7 @@ function moveMergeTilesLeftRight(i, v, v2, indices, left) {
|
|||||||
// Move and merge
|
// Move and merge
|
||||||
tileItems[gridSize*i+j].destroyFlag = true;
|
tileItems[gridSize*i+j].destroyFlag = true;
|
||||||
tileItems[gridSize*i+j].z = -1;
|
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].x = cells.itemAt(gridSize*i+indices[j]).x;
|
||||||
// tileItems[gridSize*i+j].destroy();
|
// tileItems[gridSize*i+j].destroy();
|
||||||
|
|
||||||
@ -398,6 +405,7 @@ function moveMergeTilesUpDown(i, v, v2, indices, up) {
|
|||||||
// Move and merge
|
// Move and merge
|
||||||
tileItems[gridSize*j+i].destroyFlag = true;
|
tileItems[gridSize*j+i].destroyFlag = true;
|
||||||
tileItems[gridSize*j+i].z = -1;
|
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].y = cells.itemAt(gridSize*indices[j]+i).y;
|
||||||
// tileItems[gridSize*j+i].destroy();
|
// tileItems[gridSize*j+i].destroy();
|
||||||
|
|
||||||
|
@ -55,7 +55,6 @@ Rectangle {
|
|||||||
|
|
||||||
Behavior on y {
|
Behavior on y {
|
||||||
NumberAnimation {
|
NumberAnimation {
|
||||||
easing.type: Easing.InQuad
|
|
||||||
duration: moveAnimTime
|
duration: moveAnimTime
|
||||||
onRunningChanged: {
|
onRunningChanged: {
|
||||||
if ((!running) && destroyFlag) {
|
if ((!running) && destroyFlag) {
|
||||||
@ -67,7 +66,6 @@ Rectangle {
|
|||||||
|
|
||||||
Behavior on x {
|
Behavior on x {
|
||||||
NumberAnimation {
|
NumberAnimation {
|
||||||
easing.type: Easing.InQuad
|
|
||||||
duration: moveAnimTime
|
duration: moveAnimTime
|
||||||
onRunningChanged: {
|
onRunningChanged: {
|
||||||
if ((!running) && destroyFlag) {
|
if ((!running) && destroyFlag) {
|
||||||
|
55
qml/main.qml
55
qml/main.qml
@ -16,8 +16,6 @@ ApplicationWindow {
|
|||||||
x: (Screen.width - width) / 2
|
x: (Screen.width - width) / 2
|
||||||
y: (Screen.height - height) / 2
|
y: (Screen.height - height) / 2
|
||||||
|
|
||||||
signal helpMenuTriggered
|
|
||||||
|
|
||||||
menuBar: MenuBar {
|
menuBar: MenuBar {
|
||||||
Menu {
|
Menu {
|
||||||
title: qsTr("File")
|
title: qsTr("File")
|
||||||
@ -36,8 +34,8 @@ ApplicationWindow {
|
|||||||
id: helpMenu
|
id: helpMenu
|
||||||
title: qsTr("Help")
|
title: qsTr("Help")
|
||||||
MenuItem {
|
MenuItem {
|
||||||
text: "About Qt"
|
text: qsTr("About Qt")
|
||||||
onTriggered: mainWindow.helpMenuTriggered()
|
onTriggered: myClass.aboutQt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,7 +83,7 @@ ApplicationWindow {
|
|||||||
color: helper.myColors.bgdark
|
color: helper.myColors.bgdark
|
||||||
property string scoreText: (index === 0) ? MyScript.score.toString() : MyScript.bestScore.toString()
|
property string scoreText: (index === 0) ? MyScript.score.toString() : MyScript.bestScore.toString()
|
||||||
Text {
|
Text {
|
||||||
text: (index == 0) ? "SCORE" : "BEST"
|
text: (index == 0) ? qsTr("SCORE") : qsTr("BEST")
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
anchors.horizontalCenter: parent.horizontalCenter
|
||||||
y: 7
|
y: 7
|
||||||
font.pixelSize: 13
|
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 {
|
Text {
|
||||||
id: banner
|
id: banner
|
||||||
y: 90
|
y: 90
|
||||||
height: 40
|
height: 40
|
||||||
text: "Join the numbers and get to the <b>2048 tile!</b>"
|
text: qsTr("Join the numbers and get to the <b>2048 tile</b>!")
|
||||||
color: helper.myColors.fgdark
|
color: helper.myColors.fgdark
|
||||||
font.pixelSize: 16
|
font.pixelSize: 16
|
||||||
verticalAlignment: Text.AlignVCenter
|
verticalAlignment: Text.AlignVCenter
|
||||||
@ -125,7 +158,7 @@ ApplicationWindow {
|
|||||||
radius: 3
|
radius: 3
|
||||||
Text{
|
Text{
|
||||||
anchors.centerIn: parent
|
anchors.centerIn: parent
|
||||||
text: "New Game"
|
text: qsTr("New Game")
|
||||||
color: helper.myColors.fgbutton
|
color: helper.myColors.fgbutton
|
||||||
font.pixelSize: 18
|
font.pixelSize: 18
|
||||||
font.bold: true
|
font.bold: true
|
||||||
@ -162,8 +195,8 @@ ApplicationWindow {
|
|||||||
|
|
||||||
MessageDialog {
|
MessageDialog {
|
||||||
id: deadMessage
|
id: deadMessage
|
||||||
title: "Game Over"
|
title: qsTr("Game Over")
|
||||||
text: "Game Over"
|
text: qsTr("Game Over!")
|
||||||
standardButtons: StandardButton.Retry | StandardButton.Abort
|
standardButtons: StandardButton.Retry | StandardButton.Abort
|
||||||
onAccepted: {
|
onAccepted: {
|
||||||
MyScript.startupFunction();
|
MyScript.startupFunction();
|
||||||
@ -175,8 +208,8 @@ ApplicationWindow {
|
|||||||
|
|
||||||
MessageDialog {
|
MessageDialog {
|
||||||
id: winMessage
|
id: winMessage
|
||||||
title: "You Win"
|
title: qsTr("You Win")
|
||||||
text: "You win! Continue playing?"
|
text: qsTr("You win! Continue playing?")
|
||||||
standardButtons: StandardButton.Yes | StandardButton.No
|
standardButtons: StandardButton.Yes | StandardButton.No
|
||||||
onYes: {
|
onYes: {
|
||||||
MyScript.checkTargetFlag = false;
|
MyScript.checkTargetFlag = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user