added more variants of 2048 and selection of them

master
Qiaoyong Zhong 11 years ago
parent d45790cee7
commit 6f43c40a1b

@ -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

Binary file not shown.

@ -1,59 +0,0 @@
<?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 &lt;b&gt;2048 tile&lt;/b&gt;!</source>
<translation>&lt;b&gt;2048&lt;/b&gt;</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>

@ -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;

@ -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;

@ -18,6 +18,7 @@ Rectangle {
id: tileLabel
text: tileText
color: tileColor
font.family: "Sans-serif"
font.pixelSize: tileFontSize
font.bold: true
anchors.centerIn: parent

@ -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("<p style='font-weight: bold; font-size: 24px'>2048-Qt</p><p>Version 0.1</p><p>2014 Qiaoyong Zhong</p>")
standardButtons: StandardButton.Ok
}
MessageDialog {
id: deadMessage
title: qsTr("Game Over")

@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file>ts/2048-qt_zh_CN.qm</file>
<file>qml/main.qml</file>
<file>qml/2048.js</file>
<file>qml/Tile.qml</file>

Binary file not shown.

@ -0,0 +1,113 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
<name>main</name>
<message>
<location filename="../qml/main.qml" line="13"/>
<source>2048 Game</source>
<translation>2048</translation>
</message>
<message>
<location filename="../qml/main.qml" line="22"/>
<source>File</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="24"/>
<location filename="../qml/main.qml" line="230"/>
<source>New Game</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="29"/>
<source>Exit</source>
<translation>退</translation>
</message>
<message>
<location filename="../qml/main.qml" line="36"/>
<source>Settings</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="38"/>
<source>2048</source>
<translation>2048</translation>
</message>
<message>
<location filename="../qml/main.qml" line="50"/>
<source>Degree</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="62"/>
<source>Military Rank</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="74"/>
<source>PRC</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="89"/>
<source>Help</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="91"/>
<source>About</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="95"/>
<source>About Qt</source>
<translation>Qt</translation>
</message>
<message>
<location filename="../qml/main.qml" line="151"/>
<source>SCORE</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="151"/>
<source>BEST</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="211"/>
<source>Join the numbers and get to the &lt;b&gt;2048 tile&lt;/b&gt;!</source>
<translation>&lt;b&gt;2048&lt;/b&gt;</translation>
</message>
<message>
<location filename="../qml/main.qml" line="268"/>
<source>About 2048-Qt</source>
<translation>2048-Qt</translation>
</message>
<message>
<location filename="../qml/main.qml" line="269"/>
<source>&lt;p style=&apos;font-weight: bold; font-size: 24px&apos;&gt;2048-Qt&lt;/p&gt;&lt;p&gt;Version 0.1&lt;/p&gt;&lt;p&gt;2014 Qiaoyong Zhong&lt;/p&gt;</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/main.qml" line="275"/>
<source>Game Over</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="276"/>
<source>Game Over!</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="286"/>
<source>You Win</source>
<translation></translation>
</message>
<message>
<location filename="../qml/main.qml" line="287"/>
<source>You win! Continue playing?</source>
<translation></translation>
</message>
</context>
</TS>
Loading…
Cancel
Save