summaryrefslogtreecommitdiffstats
path: root/demos/declarative/minehunt
diff options
context:
space:
mode:
Diffstat (limited to 'demos/declarative/minehunt')
-rw-r--r--demos/declarative/minehunt/Description.qml34
-rw-r--r--demos/declarative/minehunt/Explosion.qml29
-rw-r--r--demos/declarative/minehunt/main.cpp309
-rw-r--r--demos/declarative/minehunt/minehunt.pro9
-rw-r--r--demos/declarative/minehunt/minehunt.qml195
-rw-r--r--demos/declarative/minehunt/pics/No-Ones-Laughing-3.jpgbin0 -> 30730 bytes
-rw-r--r--demos/declarative/minehunt/pics/back.pngbin0 -> 558 bytes
-rw-r--r--demos/declarative/minehunt/pics/bomb-color.pngbin0 -> 284 bytes
-rw-r--r--demos/declarative/minehunt/pics/bomb.pngbin0 -> 535 bytes
-rw-r--r--demos/declarative/minehunt/pics/face-sad.pngbin0 -> 14844 bytes
-rw-r--r--demos/declarative/minehunt/pics/face-smile-big.pngbin0 -> 13810 bytes
-rw-r--r--demos/declarative/minehunt/pics/face-smile.pngbin0 -> 15408 bytes
-rw-r--r--demos/declarative/minehunt/pics/flag-color.pngbin0 -> 219 bytes
-rw-r--r--demos/declarative/minehunt/pics/flag.pngbin0 -> 196 bytes
-rw-r--r--demos/declarative/minehunt/pics/front.pngbin0 -> 580 bytes
-rw-r--r--demos/declarative/minehunt/pics/star.pngbin0 -> 2677 bytes
16 files changed, 576 insertions, 0 deletions
diff --git a/demos/declarative/minehunt/Description.qml b/demos/declarative/minehunt/Description.qml
new file mode 100644
index 0000000..df4881c
--- /dev/null
+++ b/demos/declarative/minehunt/Description.qml
@@ -0,0 +1,34 @@
+import Qt 4.6
+
+Item {
+ id: Page
+ height: MyText.height + 20
+ property var text
+ MouseRegion {
+ anchors.fill: parent
+ drag.target: Page
+ drag.axis: "XandYAxis"
+ drag.minimumX: 0
+ drag.maximumX: 1000
+ drag.minimumY: 0
+ drag.maximumY: 1000
+ }
+ Rectangle {
+ radius: 10
+ anchors.fill: parent
+ color: "lightsteelblue"
+ }
+ Item {
+ x: 10
+ y: 10
+ width: parent.width - 20
+ height: parent.height - 20
+ Text {
+ id: MyText
+ text: Page.text
+ width: parent.width
+ clip: true
+ wrap: true
+ }
+ }
+}
diff --git a/demos/declarative/minehunt/Explosion.qml b/demos/declarative/minehunt/Explosion.qml
new file mode 100644
index 0000000..a997048
--- /dev/null
+++ b/demos/declarative/minehunt/Explosion.qml
@@ -0,0 +1,29 @@
+import Qt 4.6
+
+Item {
+ property bool explode : false
+
+ Particles {
+ id: particles
+ width: 40
+ height: 40
+ lifeSpan: 1000
+ lifeSpanDeviation: 0
+ source: "pics/star.png"
+ count: 0
+ angle: 270
+ angleDeviation: 360
+ velocity: 100
+ velocityDeviation: 20
+ z: 100
+ opacity: 0
+ streamIn: false
+ }
+ states: [ State { name: "exploding"; when: explode == true
+ PropertyChanges { target: particles; count: 200 }
+ PropertyChanges { target: particles; opacity: 1 }
+ PropertyChanges { target: particles; emitting: false } // i.e. emit only once
+ }
+ ]
+
+}
diff --git a/demos/declarative/minehunt/main.cpp b/demos/declarative/minehunt/main.cpp
new file mode 100644
index 0000000..20f7492
--- /dev/null
+++ b/demos/declarative/minehunt/main.cpp
@@ -0,0 +1,309 @@
+#include "qmlengine.h"
+#include "qmlcontext.h"
+#include "qml.h"
+#include <qfxitem.h>
+#include <qfxview.h>
+
+#include <QWidget>
+#include <QApplication>
+#include <QFile>
+#include <QTime>
+#include <QTimer>
+#include <QVBoxLayout>
+#include <QFileInfo>
+
+QString fileName = "minehunt.qml";
+
+class Tile : public QObject
+{
+ Q_OBJECT
+public:
+ Tile() : _hasFlag(false), _hasMine(false), _hint(-1), _flipped(false) {}
+
+ Q_PROPERTY(bool hasFlag READ hasFlag WRITE setHasFlag NOTIFY hasFlagChanged);
+ bool hasFlag() const { return _hasFlag; }
+
+ Q_PROPERTY(bool hasMine READ hasMine NOTIFY hasMineChanged);
+ bool hasMine() const { return _hasMine; }
+
+ Q_PROPERTY(int hint READ hint NOTIFY hintChanged);
+ int hint() const { return _hint; }
+
+ Q_PROPERTY(bool flipped READ flipped NOTIFY flippedChanged());
+ bool flipped() const { return _flipped; }
+
+ void setHasFlag(bool flag) {if(flag==_hasFlag) return; _hasFlag = flag; emit hasFlagChanged();}
+ void setHasMine(bool mine) {if(mine==_hasMine) return; _hasMine = mine; emit hasMineChanged();}
+ void setHint(int hint) { if(hint == _hint) return; _hint = hint; emit hintChanged(); }
+ void flip() { if (_flipped) return; _flipped = true; emit flippedChanged(); }
+ void unflip() { if(!_flipped) return; _flipped = false; emit flippedChanged(); }
+
+signals:
+ void flippedChanged();
+ void hasFlagChanged();
+ void hintChanged();
+ void hasMineChanged();
+
+private:
+ bool _hasFlag;
+ bool _hasMine;
+ int _hint;
+ bool _flipped;
+};
+
+QML_DECLARE_TYPE(Tile);
+QML_DEFINE_TYPE(0,0,0,0,Tile,Tile);
+
+class MyWidget : public QWidget
+{
+Q_OBJECT
+public:
+ MyWidget(int = 370, int = 480, QWidget *parent=0, Qt::WindowFlags flags=0);
+ ~MyWidget();
+
+ Q_PROPERTY(QList<Tile *> *tiles READ tiles CONSTANT);
+ QList<Tile *> *tiles() { return &_tiles; }
+
+ Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY isPlayingChanged);
+ bool isPlaying() {return playing;}
+
+ Q_PROPERTY(bool hasWon READ hasWon NOTIFY hasWonChanged);
+ bool hasWon() {return won;}
+
+ Q_PROPERTY(int numMines READ numMines NOTIFY numMinesChanged);
+ int numMines() const{return nMines;}
+
+ Q_PROPERTY(int numFlags READ numFlags NOTIFY numFlagsChanged);
+ int numFlags() const{return nFlags;}
+
+public slots:
+ Q_INVOKABLE void flip(int row, int col);
+ Q_INVOKABLE void flag(int row, int col);
+ void setBoard();
+ void reset();
+
+signals:
+ void isPlayingChanged();
+ void hasWonChanged();
+ void numMinesChanged();
+ void numFlagsChanged();
+
+private:
+ bool onBoard( int r, int c ) const { return r >= 0 && r < numRows && c >= 0 && c < numCols; }
+ Tile *tile( int row, int col ) { return onBoard(row, col) ? _tiles[col+numRows*row] : 0; }
+ int getHint(int row, int col);
+ void setPlaying(bool b){if(b==playing) return; playing=b; emit isPlayingChanged();}
+
+ QFxView *canvas;
+
+ QList<Tile *> _tiles;
+ int numCols;
+ int numRows;
+ bool playing;
+ bool won;
+ int remaining;
+ int nMines;
+ int nFlags;
+};
+
+MyWidget::MyWidget(int width, int height, QWidget *parent, Qt::WindowFlags flags)
+: QWidget(parent, flags), canvas(0), numCols(9), numRows(9), playing(true), won(false)
+{
+ setObjectName("mainWidget");
+ srand(QTime(0,0,0).secsTo(QTime::currentTime()));
+
+ //initialize array
+ for(int ii = 0; ii < numRows * numCols; ++ii) {
+ _tiles << new Tile;
+ }
+
+ reset();
+
+ QVBoxLayout *vbox = new QVBoxLayout;
+ vbox->setMargin(0);
+ setLayout(vbox);
+
+ canvas = new QFxView(this);
+ canvas->setFixedSize(width, height);
+ vbox->addWidget(canvas);
+
+ QFile file(fileName);
+ file.open(QFile::ReadOnly);
+ QString qml = file.readAll();
+ canvas->setQml(qml, fileName);
+
+ QmlContext *ctxt = canvas->rootContext();
+ ctxt->addDefaultObject(this);
+
+ canvas->execute();
+}
+
+MyWidget::~MyWidget()
+{
+}
+
+void MyWidget::setBoard()
+{
+ foreach(Tile* t, _tiles){
+ t->setHasMine(false);
+ t->setHint(-1);
+ }
+ //place mines
+ int mines = nMines;
+ remaining = numRows*numCols-mines;
+ while ( mines ) {
+ int col = int((double(rand()) / double(RAND_MAX)) * numCols);
+ int row = int((double(rand()) / double(RAND_MAX)) * numRows);
+
+ Tile* t = tile( row, col );
+
+ if (t && !t->hasMine()) {
+ t->setHasMine( true );
+ mines--;
+ }
+ }
+
+ //set hints
+ for (int r = 0; r < numRows; r++)
+ for (int c = 0; c < numCols; c++) {
+ Tile* t = tile(r, c);
+ if (t && !t->hasMine()) {
+ int hint = getHint(r,c);
+ t->setHint(hint);
+ }
+ }
+
+ setPlaying(true);
+}
+
+void MyWidget::reset()
+{
+ foreach(Tile* t, _tiles){
+ t->unflip();
+ t->setHasFlag(false);
+ }
+ nMines = 12;
+ nFlags = 0;
+ setPlaying(false);
+ QTimer::singleShot(900,this, SLOT(setBoard()));
+}
+
+int MyWidget::getHint(int row, int col)
+{
+ int hint = 0;
+ for (int c = col-1; c <= col+1; c++)
+ for (int r = row-1; r <= row+1; r++) {
+ Tile* t = tile(r, c);
+ if (t && t->hasMine())
+ hint++;
+ }
+ return hint;
+}
+
+void MyWidget::flip(int row, int col)
+{
+ if(!playing)
+ return;
+
+ Tile *t = tile(row, col);
+ if (!t || t->hasFlag())
+ return;
+
+ if(t->flipped()){
+ int flags = 0;
+ for (int c = col-1; c <= col+1; c++)
+ for (int r = row-1; r <= row+1; r++) {
+ Tile *nearT = tile(r, c);
+ if(!nearT || nearT == t)
+ continue;
+ if(nearT->hasFlag())
+ flags++;
+ }
+ if(!t->hint() || t->hint() != flags)
+ return;
+ for (int c = col-1; c <= col+1; c++)
+ for (int r = row-1; r <= row+1; r++) {
+ Tile *nearT = tile(r, c);
+ if (nearT && !nearT->flipped() && !nearT->hasFlag()) {
+ flip( r, c );
+ }
+ }
+ return;
+ }
+
+ t->flip();
+
+ if (t->hint() == 0) {
+ for (int c = col-1; c <= col+1; c++)
+ for (int r = row-1; r <= row+1; r++) {
+ Tile* t = tile(r, c);
+ if (t && !t->flipped()) {
+ flip( r, c );
+ }
+ }
+ }
+
+ if(t->hasMine()){
+ for (int r = 0; r < numRows; r++)//Flip all other mines
+ for (int c = 0; c < numCols; c++) {
+ Tile* t = tile(r, c);
+ if (t && t->hasMine()) {
+ flip(r, c);
+ }
+ }
+ won = false;
+ hasWonChanged();
+ setPlaying(false);
+ }
+
+ remaining--;
+ if(!remaining){
+ won = true;
+ hasWonChanged();
+ setPlaying(false);
+ }
+}
+
+void MyWidget::flag(int row, int col)
+{
+ Tile *t = tile(row, col);
+ if(!t)
+ return;
+
+ t->setHasFlag(!t->hasFlag());
+ nFlags += (t->hasFlag()?1:-1);
+ emit numFlagsChanged();
+}
+/////////////////////////////////////////////////////////
+
+int main(int argc, char ** argv)
+{
+ QApplication app(argc, argv);
+
+ bool frameless = false;
+
+ int width = 370;
+ int height = 480;
+
+ for (int i = 1; i < argc; ++i) {
+ QString arg = argv[i];
+ if (arg == "-frameless") {
+ frameless = true;
+ } else if(arg == "-width" && i < (argc - 1)) {
+ ++i;
+ width = ::atoi(argv[i]);
+ } else if(arg == "-height" && i < (argc - 1)) {
+ ++i;
+ height = ::atoi(argv[i]);
+ } else if (arg[0] != '-') {
+ fileName = arg;
+ }
+ }
+
+ MyWidget wid(width, height, 0, frameless ? Qt::FramelessWindowHint : Qt::Widget);
+ wid.show();
+
+ return app.exec();
+}
+
+#include "main.moc"
diff --git a/demos/declarative/minehunt/minehunt.pro b/demos/declarative/minehunt/minehunt.pro
new file mode 100644
index 0000000..01791b1
--- /dev/null
+++ b/demos/declarative/minehunt/minehunt.pro
@@ -0,0 +1,9 @@
+SOURCES = main.cpp
+
+QT += script declarative
+contains(QT_CONFIG, opengles2)|contains(QT_CONFIG, opengles1): QT += opengl
+
+target.path = $$[QT_INSTALL_EXAMPLES]/declarative/minehunt
+sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS minehunt.pro
+sources.path = $$[QT_INSTALL_EXAMPLES]/declarative/minehunt
+INSTALLS += target sources
diff --git a/demos/declarative/minehunt/minehunt.qml b/demos/declarative/minehunt/minehunt.qml
new file mode 100644
index 0000000..fa169aa
--- /dev/null
+++ b/demos/declarative/minehunt/minehunt.qml
@@ -0,0 +1,195 @@
+import Qt 4.6
+
+Item {
+ id: field
+ width: 370
+ height: 480
+
+ property int clickx : 0
+ property int clicky : 0
+
+ resources: [
+ Component {
+ id: tile
+ Flipable {
+ id: flipable
+ width: 40
+ height: 40
+ property int angle: 0;
+ transform: Rotation {
+ origin.x: 20
+ origin.y: 20
+ axis.x: 1
+ axis.z: 0
+ angle: flipable.angle;
+ }
+ front: Image {
+ source: "pics/front.png"
+ width: 40
+ height: 40
+ Image {
+ anchors.horizontalCenter: parent.horizontalCenter
+ anchors.verticalCenter: parent.verticalCenter
+ source: "pics/flag.png"
+ opacity: modelData.hasFlag
+ opacity: Behavior {
+ NumberAnimation {
+ property: "opacity"
+ duration: 250
+ }
+ }
+ }
+ }
+ back: Image {
+ source: "pics/back.png"
+ width: 40
+ height: 40
+ Text {
+ anchors.horizontalCenter: parent.horizontalCenter
+ anchors.verticalCenter: parent.verticalCenter
+ text: modelData.hint
+ color: "white"
+ font.bold: true
+ opacity: !modelData.hasMine && modelData.hint > 0
+ }
+ Image {
+ anchors.horizontalCenter: parent.horizontalCenter
+ anchors.verticalCenter: parent.verticalCenter
+ source: "pics/bomb.png"
+ opacity: modelData.hasMine
+ }
+ Explosion {
+ id: expl
+ }
+ }
+ states: [
+ State {
+ name: "back"
+ when: modelData.flipped
+ PropertyChanges { target: flipable; angle: 180 }
+ }
+ ]
+ transitions: [
+ Transition {
+ SequentialAnimation {
+ PauseAnimation {
+ duration: {
+ var ret;
+ if(flipable.parent != null)
+ ret = Math.abs(flipable.parent.x-field.clickx)
+ + Math.abs(flipable.parent.y-field.clicky);
+ else
+ ret = 0;
+ if (ret > 0) {
+ if (modelData.hasMine && modelData.flipped) {
+ ret*3;
+ } else {
+ ret;
+ }
+ } else {
+ 0;
+ }
+ }
+ }
+ NumberAnimation {
+ easing: "easeInOutQuad"
+ properties: "angle"
+ }
+ ScriptAction{
+ script: "if(modelData.hasMine && modelData.flipped){expl.explode = true;}"
+ }
+ }
+ }
+ ]
+ MouseRegion {
+ anchors.fill: parent
+ acceptedButtons: Qt.LeftButton | Qt.RightButton
+ onPressed: {
+ field.clickx = flipable.parent.x;
+ field.clicky = flipable.parent.y;
+ row = Math.floor(index/9);
+ col = index - (Math.floor(index/9) * 9);
+ if (mouse.button==undefined || mouse.button==Qt.RightButton) {
+ flag(row,col);
+ } else {
+ flip(row,col);
+ }
+ }
+ }
+ }
+ }
+ ]
+ Image {
+ source: "pics/No-Ones-Laughing-3.jpg"
+ fillMode: "Tile"
+ }
+ Description {
+ text: "Use the 'minehunt' executable to run this demo!"
+ width: 300
+ opacity: tiles?0:1
+ anchors.horizontalCenter: parent.horizontalCenter
+ anchors.verticalCenter: parent.verticalCenter
+ }
+ Repeater {
+ model: tiles
+ x: 1
+ y: 1
+ Component {
+ ComponentInstance {
+ component: tile
+ x: (index - (Math.floor(index/9) * 9)) * 41
+ y: Math.floor(index/9) * 41
+ }
+ }
+ }
+ Row {
+ id: gamedata
+ // width: 370
+ // height: 100
+ y: 400
+ x: 20
+ spacing: 20
+ Column {
+ spacing: 2
+ width: childrenRect.width
+ Image {
+ // x: 100
+ // y: 20
+ source: "pics/bomb-color.png"
+ }
+ Text {
+ // x: 100
+ // y: 60
+ anchors.horizontalCenter: parent.horizontalCenter
+ color: "white"
+ text: numMines
+ }
+ }
+ Column {
+ spacing: 2
+ width: childrenRect.width
+ Image {
+ // x: 140
+ // y: 20
+ source: "pics/flag-color.png"
+ }
+ Text {
+ // x: 140
+ // y: 60
+ anchors.horizontalCenter: parent.horizontalCenter
+ color: "white"
+ text: numFlags
+ }
+ }
+ }
+ Image {
+ y: 390
+ anchors.right: field.right
+ anchors.rightMargin: 20
+ source: isPlaying ? 'pics/face-smile.png' : hasWon ? 'pics/face-smile-big.png': 'pics/face-sad.png'
+ MouseRegion {
+ anchors.fill: parent
+ onPressed: { reset() }
+ }
+ }
+}
diff --git a/demos/declarative/minehunt/pics/No-Ones-Laughing-3.jpg b/demos/declarative/minehunt/pics/No-Ones-Laughing-3.jpg
new file mode 100644
index 0000000..445567f
--- /dev/null
+++ b/demos/declarative/minehunt/pics/No-Ones-Laughing-3.jpg
Binary files differ
diff --git a/demos/declarative/minehunt/pics/back.png b/demos/declarative/minehunt/pics/back.png
new file mode 100644
index 0000000..f6b3f0b
--- /dev/null
+++ b/demos/declarative/minehunt/pics/back.png
Binary files differ
diff --git a/demos/declarative/minehunt/pics/bomb-color.png b/demos/declarative/minehunt/pics/bomb-color.png
new file mode 100644
index 0000000..61ad0a9
--- /dev/null
+++ b/demos/declarative/minehunt/pics/bomb-color.png
Binary files differ
diff --git a/demos/declarative/minehunt/pics/bomb.png b/demos/declarative/minehunt/pics/bomb.png
new file mode 100644
index 0000000..a992575
--- /dev/null
+++ b/demos/declarative/minehunt/pics/bomb.png
Binary files differ
diff --git a/demos/declarative/minehunt/pics/face-sad.png b/demos/declarative/minehunt/pics/face-sad.png
new file mode 100644
index 0000000..cf00aaf
--- /dev/null
+++ b/demos/declarative/minehunt/pics/face-sad.png
Binary files differ
diff --git a/demos/declarative/minehunt/pics/face-smile-big.png b/demos/declarative/minehunt/pics/face-smile-big.png
new file mode 100644
index 0000000..f9c2335
--- /dev/null
+++ b/demos/declarative/minehunt/pics/face-smile-big.png
Binary files differ
diff --git a/demos/declarative/minehunt/pics/face-smile.png b/demos/declarative/minehunt/pics/face-smile.png
new file mode 100644
index 0000000..3d66d72
--- /dev/null
+++ b/demos/declarative/minehunt/pics/face-smile.png
Binary files differ
diff --git a/demos/declarative/minehunt/pics/flag-color.png b/demos/declarative/minehunt/pics/flag-color.png
new file mode 100644
index 0000000..aadad0f
--- /dev/null
+++ b/demos/declarative/minehunt/pics/flag-color.png
Binary files differ
diff --git a/demos/declarative/minehunt/pics/flag.png b/demos/declarative/minehunt/pics/flag.png
new file mode 100644
index 0000000..39cde4d
--- /dev/null
+++ b/demos/declarative/minehunt/pics/flag.png
Binary files differ
diff --git a/demos/declarative/minehunt/pics/front.png b/demos/declarative/minehunt/pics/front.png
new file mode 100644
index 0000000..834331b
--- /dev/null
+++ b/demos/declarative/minehunt/pics/front.png
Binary files differ
diff --git a/demos/declarative/minehunt/pics/star.png b/demos/declarative/minehunt/pics/star.png
new file mode 100644
index 0000000..3772359
--- /dev/null
+++ b/demos/declarative/minehunt/pics/star.png
Binary files differ