From 6f43c40a1b4795e76d10409eecf78045aabfc670 Mon Sep 17 00:00:00 2001 From: Qiaoyong Zhong Date: Mon, 21 Apr 2014 18:37:42 +0800 Subject: [PATCH] added more variants of 2048 and selection of them --- 2048-qt.pro | 10 ++-- 2048-qt_zh_CN.qm | Bin 740 -> 0 bytes 2048-qt_zh_CN.ts | 59 -------------------- main.cpp | 2 +- qml/2048.js | 99 +++++++++++++++++++++------------- qml/Tile.qml | 1 + qml/main.qml | 65 ++++++++++++++++++++++ qml.qrc => resources.qrc | 1 + ts/2048-qt_zh_CN.qm | Bin 0 -> 1069 bytes ts/2048-qt_zh_CN.ts | 113 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 249 insertions(+), 101 deletions(-) delete mode 100644 2048-qt_zh_CN.qm delete mode 100644 2048-qt_zh_CN.ts rename qml.qrc => resources.qrc (79%) create mode 100644 ts/2048-qt_zh_CN.qm create mode 100644 ts/2048-qt_zh_CN.ts diff --git a/2048-qt.pro b/2048-qt.pro index a0ba4d6..a0d2068 100644 --- a/2048-qt.pro +++ b/2048-qt.pro @@ -8,10 +8,12 @@ SOURCES += main.cpp \ lupdate_only { SOURCES += qml/main.qml \ + qml/Tile.qml \ qml/2048.js } -RESOURCES += qml.qrc +RESOURCES += \ + resources.qrc # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = @@ -20,11 +22,11 @@ QML_IMPORT_PATH = include(deployment.pri) # Setting the application icon -RC_ICONS = 2048.ico # On Windows -ICON = 2048.ico # On Mac OSX +win32: RC_ICONS = 2048.ico # On Windows +macx: ICON = 2048.ico # On Mac OSX HEADERS += \ myclass.h \ settings.h -TRANSLATIONS = 2048-qt_zh_CN.ts +TRANSLATIONS = ts/2048-qt_zh_CN.ts diff --git a/2048-qt_zh_CN.qm b/2048-qt_zh_CN.qm deleted file mode 100644 index 561fa290e20ffc3993e03eecf0d40b2fb6c88c10..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 740 zcmcE7ks@*G{hX<16=n7(EZlq7iGhJ3gMlTh1xSZ8u$*TB(p3yB7gT}tYzCG$1`G_0 z!VKXpsX+QNLwf8yApMo;rpiYkZNglt2-3fYJxHVwNIzp=@|YD!GjQf4egRT?oUe82 z85o$Pd8S-01*%_@3FIU(17&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!g3C3Thk2;nJfNiB#DFZ&U+;6G3N^dJKU8-^qXJ6L!EdHNtel8uI5 z`I&hNB^jv-d8N5YsYS&KiFqjs>8T|OCHY`snGx2iW`*dXxOx;C^I}%r8|4&&)$rB?3|PpRu#C?s?tY;*}xd k|K;I&q(FMgGxHP`ob&TaGV@AP6$)|^D>L)b?GeTR04}MVO#lD@ diff --git a/2048-qt_zh_CN.ts b/2048-qt_zh_CN.ts deleted file mode 100644 index 60221e8..0000000 --- a/2048-qt_zh_CN.ts +++ /dev/null @@ -1,59 +0,0 @@ - - - - - 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 dad4fd9..02e100b 100644 --- a/main.cpp +++ b/main.cpp @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) QString tsFile = "2048-qt_" + locale; QTranslator translator; - if (translator.load(tsFile, ".")) { + if (translator.load(tsFile, ":/ts")) { qDebug() << "Successfully loaded " + tsFile; } else { qDebug() << "Failed to load " + tsFile; diff --git a/qml/2048.js b/qml/2048.js index a8d7988..0b8dc1c 100644 --- a/qml/2048.js +++ b/qml/2048.js @@ -5,25 +5,41 @@ var gridSize = 4; var cellValues; var tileItems = []; var availableCells; -//var labels = "PRC"; -var labels = "2048"; -var labelFunc; var targetLevel = 11; var checkTargetFlag = true; var tileComponent = Qt.createComponent("Tile.qml"); -switch (labels) { -case "2048": - labelFunc = function(n) { +var label = settings.value("label", "2048"); +var labelOptions = ["2048", "Degree", "Military Rank", "PRC"]; +var labelFunc = { + "2048": + function(n) { return Math.pow(2, n).toString(); - }; - break; -case "PRC": - labelFunc = function(n) { - var dynasties = ["商", "周", "秦", "汉", "唐", "宋", "元", "明", "清", "ROC", "PRC"]; - return dynasties[n-1]; - }; - break; + }, + "PRC": + function(n) { + var arr = ["商", "周", "秦", "汉", "唐", "宋", "元", "明", "清", "民国", "天朝", "天庭"]; + if (n > 0 && n < arr.length) + return arr[n-1]; + else + return ""; + }, + "Military Rank": + function(n) { + var arr = ["少尉", "中尉", "上尉", "少校", "中校", "上校", "大校", "少将", "中将", "上将", "元帅", "大元帅"]; + if (n > 0 && n < arr.length) + return arr[n-1]; + else + return ""; + }, + "Degree": + function(n) { + var arr = ["幼儿园", "小学", "初中", "高中", "学士", "硕士", "博士", "博士后", "勇士", "壮士", "烈士", "圣斗士"]; + if (n > 0 && n < arr.length) + return arr[n-1]; + else + return ""; + } } @@ -303,7 +319,7 @@ function isDead() { return dead; } -function computeTileStyle(n) { +function computeTileStyle(n, tileText) { var fgColors = ["#776E62", "#F9F6F2"]; var bgColors = ["#EEE4DA", "#EDE0C8", "#F2B179", "#F59563", "#F67C5F", "#F65E3B", "#EDCF72", "#EDCC61", "#EDC850", "#EDC53F", "#EDC22E", "#3C3A32"]; var sty = {bgColor: helper.myColors.bggray, @@ -318,26 +334,33 @@ function computeTileStyle(n) { sty.bgColor = bgColors[bgColors.length-1]; } - if (labels === "2048") { - /* Adjust font size according to size of the number - [2, 100): 55 - [100, 1000): 45 - [1000, 2048]: 35 - > 2048: 30 - */ - var pv = Math.pow(2, n); - if (pv >= 100 && pv < 1000) - sty.fontSize = 45; - else if (pv >= 1000 && pv <= 2048) - sty.fontSize = 35; - else if (pv > 2048) - sty.fontSize = 30; - - } + /* Adjust font size according to the length of the text + <= 2: 55 + {3, 4}: 45 + {5, 6}: 35 + > 6: 30 + */ + var tlen = getLengthInBytes(tileText); + if (tlen <= 2) + sty.fontSize = 50; + else if (tlen <= 4) + sty.fontSize = 40; + else if (tlen <= 6) + sty.fontSize = 30; + else + sty.fontSize = 20; return sty; } +function getLengthInBytes(str) { + // getLengthInBytes("一二三") = 6 + // getLengthInBytes("123") = 3 + var b = str.match(/[^\x00-\xff]/g); // Multi-byte characters (Chinese) occupy twice more space + return (str.length + (!b ? 0: b.length)); +} + + function maxTileValue() { var mv = 0; for (var i = 0; i < gridSize; i++) { @@ -353,8 +376,8 @@ function maxTileValue() { function createTileObject(ind, n, isStartup) { var tile; - var sty = computeTileStyle(n); - var tileText = labelFunc(n); + var tileText = labelFunc[label](n); + var sty = computeTileStyle(n, 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) { @@ -387,8 +410,9 @@ function moveMergeTilesLeftRight(i, v, v2, indices, left) { tileItems[gridSize*i+j].x = cells.itemAt(gridSize*i+indices[j]).x; // tileItems[gridSize*i+j].destroy(); - var sty = computeTileStyle(v2[indices[j]]); - tileItems[gridSize*i+indices[j]].tileText = labelFunc(v2[indices[j]]); + var tileText = labelFunc[label](v2[indices[j]]); + var sty = computeTileStyle(v2[indices[j]], tileText); + tileItems[gridSize*i+indices[j]].tileText = tileText; tileItems[gridSize*i+indices[j]].color = sty.bgColor; tileItems[gridSize*i+indices[j]].tileColor = sty.fgColor; tileItems[gridSize*i+indices[j]].tileFontSize = sty.fontSize; @@ -420,8 +444,9 @@ function moveMergeTilesUpDown(i, v, v2, indices, up) { tileItems[gridSize*j+i].y = cells.itemAt(gridSize*indices[j]+i).y; // tileItems[gridSize*j+i].destroy(); - var sty = computeTileStyle(v2[indices[j]]); - tileItems[gridSize*indices[j]+i].tileText = labelFunc(v2[indices[j]]); + var tileText = labelFunc[label](v2[indices[j]]); + var sty = computeTileStyle(v2[indices[j]], tileText); + tileItems[gridSize*indices[j]+i].tileText = tileText; tileItems[gridSize*indices[j]+i].color = sty.bgColor; tileItems[gridSize*indices[j]+i].tileColor = sty.fgColor; tileItems[gridSize*indices[j]+i].tileFontSize = sty.fontSize; diff --git a/qml/Tile.qml b/qml/Tile.qml index 5efad01..87d45e8 100644 --- a/qml/Tile.qml +++ b/qml/Tile.qml @@ -18,6 +18,7 @@ Rectangle { id: tileLabel text: tileText color: tileColor + font.family: "Sans-serif" font.pixelSize: tileFontSize font.bold: true anchors.centerIn: parent diff --git a/qml/main.qml b/qml/main.qml index 8b44b6b..a9741e8 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -16,6 +16,7 @@ ApplicationWindow { x: (Screen.width - width) / 2 y: (Screen.height - height) / 2 + ExclusiveGroup { id: labelSettingsGroup } menuBar: MenuBar { Menu { title: qsTr("File") @@ -30,9 +31,66 @@ ApplicationWindow { onTriggered: MyScript.cleanUpAndQuit(); } } + + Menu { + title: qsTr("Settings") + MenuItem { + text: qsTr("2048") + checkable: true + exclusiveGroup: labelSettingsGroup + checked: MyScript.label === MyScript.labelOptions[0] ? true : false + onTriggered: { + if (MyScript.label !== MyScript.labelOptions[0]) { + MyScript.label = MyScript.labelOptions[0]; + MyScript.startupFunction(); + } + } + } + MenuItem { + text: qsTr("Degree") + checkable: true + exclusiveGroup: labelSettingsGroup + checked: MyScript.label === MyScript.labelOptions[1] ? true : false + onTriggered: { + if (MyScript.label !== MyScript.labelOptions[1]) { + MyScript.label = MyScript.labelOptions[1]; + MyScript.startupFunction(); + } + } + } + MenuItem { + text: qsTr("Military Rank") + checkable: true + exclusiveGroup: labelSettingsGroup + checked: MyScript.label === MyScript.labelOptions[2] ? true : false + onTriggered: { + if (MyScript.label !== MyScript.labelOptions[2]) { + MyScript.label = MyScript.labelOptions[2]; + MyScript.startupFunction(); + } + } + } + MenuItem { + text: qsTr("PRC") + checkable: true + exclusiveGroup: labelSettingsGroup + checked: MyScript.label === MyScript.labelOptions[3] ? true : false + onTriggered: { + if (MyScript.label !== MyScript.labelOptions[3]) { + MyScript.label = MyScript.labelOptions[3]; + MyScript.startupFunction(); + } + } + } + } + Menu { id: helpMenu title: qsTr("Help") + MenuItem { + text: qsTr("About") + onTriggered: aboutDialog.open(); + } MenuItem { text: qsTr("About Qt") onTriggered: myClass.aboutQt(); @@ -205,6 +263,13 @@ ApplicationWindow { } } + MessageDialog { + id: aboutDialog + title: qsTr("About 2048-Qt") + text: qsTr("

2048-Qt

Version 0.1

2014 Qiaoyong Zhong

") + standardButtons: StandardButton.Ok + } + MessageDialog { id: deadMessage title: qsTr("Game Over") diff --git a/qml.qrc b/resources.qrc similarity index 79% rename from qml.qrc rename to resources.qrc index daf965b..1873523 100644 --- a/qml.qrc +++ b/resources.qrc @@ -1,5 +1,6 @@ + ts/2048-qt_zh_CN.qm qml/main.qml qml/2048.js qml/Tile.qml diff --git a/ts/2048-qt_zh_CN.qm b/ts/2048-qt_zh_CN.qm new file mode 100644 index 0000000000000000000000000000000000000000..13f2374230bb4d38905a718a59d02c088f4de2c9 GIT binary patch literal 1069 zcmZwFPiWIn7y$6E?Q5Ej7C{DzOiaNYg#D>whaPIh)jAQ@+O$%}a9iS@G1{iJNxJUR zArzg8!-IdoiEiL!h~Q-+i0# z+WG3o-J7pNU8|Ez&p!+Sz#_njQvmM!;K@`m1|VnacwYzdh5)#_fV}O-@c@u-VH{Ur z_|{8&pMhMRV%{n!C+`7xIQNqA3+GMu`@KKWt$WN6)_0AQhhJ83{*O=WJdg35@4+W8 z#@+tBWcf$_uRXJv-xIj8W@5c+;F(Lu@ni7G`~r?i6<_KelspVT7zQE2V*ngpWFR~k z@mUWAN!45y3bc?Gk+(7Ex1caA6{ua4w4R)2%6mvInuc9$w?wNZ^gVV=~?+E;B}s(tODd`s6E>jclM& zr=_u6WkXzS1SW2aVTghZF?R4#=(pl6EABs5R5igkLj}z&$h52rk~SqM)DVnfb2=)= znqS?(tw#Ihn6t6;!d#X$793IYsv(tU1X0q?**oOuW82TfC+YP6{-x1<%5GNBb6kwu z%MjdF`!jnp@syZwh^sGVHCBPoP{U9)MYkg^HF-@=US!NSS~P{6s@YZNrfRq@U#NYq jeb<+>I~sdf&MqrwTGc{Ayr|(CCKXC~X$BWN*e>%A!q?TG literal 0 HcmV?d00001 diff --git a/ts/2048-qt_zh_CN.ts b/ts/2048-qt_zh_CN.ts new file mode 100644 index 0000000..55eea72 --- /dev/null +++ b/ts/2048-qt_zh_CN.ts @@ -0,0 +1,113 @@ + + + + + main + + + 2048 Game + 2048游戏 + + + + File + 文件 + + + + + New Game + 新游戏 + + + + Exit + 退出 + + + + Settings + 设置 + + + + 2048 + 2048 + + + + Degree + 学位 + + + + Military Rank + 军衔 + + + + PRC + 天朝 + + + + Help + 帮助 + + + + About + 关于 + + + + About Qt + 关于Qt + + + + SCORE + 得分 + + + + BEST + 最高分 + + + + Join the numbers and get to the <b>2048 tile</b>! + 把相同的数字相加,得到<b>2048</b>! + + + + About 2048-Qt + 关于2048-Qt + + + + <p style='font-weight: bold; font-size: 24px'>2048-Qt</p><p>Version 0.1</p><p>2014 Qiaoyong Zhong</p> + + + + + Game Over + 游戏结束 + + + + Game Over! + 游戏结束! + + + + You Win + 你赢了 + + + + You win! Continue playing? + 你赢了!要继续玩吗? + + +