QT-QML2048小游戏
- 一、演示效果
- 二、关键程序
- 三、下载链接
一、演示效果
二、关键程序
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
import QtQuick.Dialogs 1.1
import QtQuick.Window 2.1
import "2048.js" as MyScriptApplicationWindow {id: mainWindowvisible: truewidth: 550height: 740title: qsTr("2048 Game");
// flags: Qt.Window | Qt.WindowTitleHint | Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint | Qt.CustomizeWindowHintx: (Screen.width - width) / 2y: (Screen.height - height) / 2ExclusiveGroup { id: labelSettingsGroup }ExclusiveGroup { id: languageSettingsGroup }menuBar: MenuBar {Menu {title: qsTr("File")MenuItem {text: qsTr("New Game")shortcut: "Ctrl+N"onTriggered: MyScript.startupFunction();}MenuItem {text: qsTr("Exit")shortcut: "Ctrl+Q"onTriggered: MyScript.cleanUpAndQuit();}}Menu {title: qsTr("Settings")Menu {title: qsTr("Labeling")MenuItem {text: qsTr("2048")checkable: trueexclusiveGroup: labelSettingsGroupchecked: MyScript.label === MyScript.labelOptions[0] ? true : falseonTriggered: {if (MyScript.label !== MyScript.labelOptions[0]) {MyScript.label = MyScript.labelOptions[0];MyScript.startupFunction();}}}MenuItem {text: qsTr("Degree")checkable: trueexclusiveGroup: labelSettingsGroupchecked: MyScript.label === MyScript.labelOptions[1] ? true : falseonTriggered: {if (MyScript.label !== MyScript.labelOptions[1]) {MyScript.label = MyScript.labelOptions[1];MyScript.startupFunction();}}}MenuItem {text: qsTr("Military Rank")checkable: trueexclusiveGroup: labelSettingsGroupchecked: MyScript.label === MyScript.labelOptions[2] ? true : falseonTriggered: {if (MyScript.label !== MyScript.labelOptions[2]) {MyScript.label = MyScript.labelOptions[2];MyScript.startupFunction();}}}MenuItem {text: qsTr("PRC")checkable: trueexclusiveGroup: labelSettingsGroupchecked: MyScript.label === MyScript.labelOptions[3] ? true : falseonTriggered: {if (MyScript.label !== MyScript.labelOptions[3]) {MyScript.label = MyScript.labelOptions[3];MyScript.startupFunction();}}}}Menu {title: qsTr("Language")MenuItem {text: "English"checkable: trueexclusiveGroup: languageSettingsGroupchecked: settings.value("language") === "en_US" ? true : falseonTriggered: {if (settings.value("language") !== "en_US") {settings.setValue("language", "en_US");changeLanguageDialog.open();}}}MenuItem {text: "Français"checkable: trueexclusiveGroup: languageSettingsGroupchecked: settings.value("language") === "fr_FR" ? true : falseonTriggered: {if (settings.value("language") !== "fr_FR") {settings.setValue("language", "fr_FR");changeLanguageDialog.open();}}}MenuItem {text: "简体中文"checkable: trueexclusiveGroup: languageSettingsGroupchecked: settings.value("language") === "zh_CN" ? true : falseonTriggered: {if (settings.value("language") !== "zh_CN") {settings.setValue("language", "zh_CN");changeLanguageDialog.open();}}}MenuItem {text: "Polski"checkable: trueexclusiveGroup: languageSettingsGroupchecked: settings.value("language") === "pl_PL" ? true : falseonTriggered: {if (settings.value("language") !== "pl_PL") {settings.setValue("language", "pl_PL");changeLanguageDialog.open();}}}MenuItem {text: "Русский"checkable: trueexclusiveGroup: languageSettingsGroupchecked: settings.value("language") === "ru_RU" ? true : falseonTriggered: {if (settings.value("language") !== "ru_RU") {settings.setValue("language", "ru_RU");changeLanguageDialog.open();}}}MenuItem {text: "German"checkable: trueexclusiveGroup: languageSettingsGroupchecked: settings.value("language") == "de_DE" ? true : falseonTriggered: {if (settings.value("language") != "de_DE") {settings.setValue("language", "de_DE");changeLanguageDialog.open();}}}}}Menu {id: helpMenutitle: qsTr("Help")MenuItem {text: qsTr("About")onTriggered: aboutDialog.open();}MenuItem {text: qsTr("About Qt")onTriggered: myClass.aboutQt();}}}Item {id: helperfocus: falseproperty var myColors: {"bglight": "#FAF8EF","bggray": Qt.rgba(238/255, 228/255, 218/255, 0.35),"bgdark": "#BBADA0","fglight": "#EEE4DA","fgdark": "#776E62","bgbutton": "#8F7A66", // Background color for the "New Game" button"fgbutton": "#F9F6F2" // Foreground color for the "New Game" button}}color: helper.myColors.bglightItem {width: 500height: 670anchors.centerIn: parentfocus: trueKeys.onPressed: MyScript.moveKey(event)MouseArea {anchors.fill: parentonClicked: parent.forceActiveFocus()}FontLoader { id: localFont; source: "qrc:///res/fonts/DroidSansFallback.ttf" }Text {id: gameNamefont.family: localFont.namefont.pixelSize: 55font.bold: truetext: "2048"color: helper.myColors.fgdark}Row {anchors.right: parent.rightspacing: 5Repeater {id: scoreBoardmodel: 2Rectangle {width: (index == 0) ? 95 : 125height: 55radius: 3color: helper.myColors.bgdarkproperty string scoreText: (index === 0) ? MyScript.score.toString() : MyScript.bestScore.toString()Text {text: (index == 0) ? qsTr("SCORE") : qsTr("BEST")anchors.horizontalCenter: parent.horizontalCentery: 6font.family: localFont.namefont.pixelSize: 13color: helper.myColors.fglight}Text {text: scoreTextanchors.horizontalCenter: parent.horizontalCentery: 22font.family: localFont.namefont.pixelSize: 25font.bold: truecolor: "white"}}}Text {id: addScoreTextfont.family: localFont.namefont.pixelSize: 25font.bold: truecolor: Qt.rgba(119/255, 110/255, 101/255, 0.9);
// parent: scoreBoard.itemAt(0)anchors.horizontalCenter: parent.horizontalCenterproperty bool runAddScore: falseproperty real yfrom: 0property real yto: -(parent.y + parent.height)property int addScoreAnimTime: 600ParallelAnimation {id: addScoreAnimrunning: falseNumberAnimation {target: addScoreTextproperty: "y"from: addScoreText.yfromto: addScoreText.ytoduration: addScoreText.addScoreAnimTime}NumberAnimation {target: addScoreTextproperty: "opacity"from: 1to: 0duration: addScoreText.addScoreAnimTime}}}}Text {id: bannery: 90height: 40text: qsTr("Join the numbers and get to the <b>2048 tile</b>!")color: helper.myColors.fgdarkfont.family: localFont.namefont.pixelSize: 16verticalAlignment: Text.AlignVCenter}Button {width: 129height: 40y: 90anchors.right: parent.rightstyle: ButtonStyle {background: Rectangle {color: helper.myColors.bgbuttonradius: 3Text{anchors.centerIn: parenttext: qsTr("New Game")color: helper.myColors.fgbuttonfont.family: localFont.namefont.pixelSize: 18font.bold: true}}}onClicked: MyScript.startupFunction()}Rectangle {y: 170width: 500height: 500color: helper.myColors.bgdarkradius: 6Grid {id: tileGridx: 15;y: 15;rows: 4; columns: 4; spacing: 15Repeater {id: cellsmodel: 16Rectangle {width: 425/4; height: 425/4radius: 3color: helper.myColors.bggray}}}}MessageDialog {id: changeLanguageDialogtitle: qsTr("Language Setting Hint")text: qsTr("Please restart the program to make the language setting take effect.")standardButtons: StandardButton.Ok}MessageDialog {id: aboutDialogtitle: qsTr("About 2048-Qt")text: qsTr("<p style='font-weight: bold; font-size: 24px'>2048-Qt</p><p>Version " + settings.getVersion() + "</p><p>2015 Qiaoyong Zhong <solary.sh@gmail.com></p>")standardButtons: StandardButton.Ok}MessageDialog {id: deadMessagetitle: qsTr("Game Over")text: qsTr("Game Over!")standardButtons: StandardButton.Retry | StandardButton.AbortonAccepted: {MyScript.startupFunction();}onRejected: MyScript.cleanUpAndQuit();}MessageDialog {id: winMessagetitle: qsTr("You Win")text: qsTr("You win! Continue playing?")standardButtons: StandardButton.Yes | StandardButton.NoonYes: {MyScript.checkTargetFlag = false;close()}onNo: MyScript.startupFunction()onRejected: {MyScript.checkTargetFlag = false;close()}}}Component.onCompleted: MyScript.startupFunction();
}
三、下载链接
https://download.csdn.net/download/u013083044/88758829