summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/sensors/maze/doc/src/maze.qdoc18
-rw-r--r--examples/sensors/maze/maze.qml289
2 files changed, 152 insertions, 155 deletions
diff --git a/examples/sensors/maze/doc/src/maze.qdoc b/examples/sensors/maze/doc/src/maze.qdoc
index 2cc69644..57e75955 100644
--- a/examples/sensors/maze/doc/src/maze.qdoc
+++ b/examples/sensors/maze/doc/src/maze.qdoc
@@ -35,29 +35,27 @@
\brief The Maze example demonstrates the TiltSensor QML type.
- To write a QML application that will use the TiltSensor QML sensors type you need to do the following steps:
+ To write a QML application that will use the TiltSensor QML sensors type
+ you need to do the following steps:
- Import the QtSensors 5.x declarative plugin:
+ To import the Qt Sensors QML types into your application, use the following
+ import statement in your .qml file:
\snippet maze/maze.qml 0
- Add the Sensor QML types into your qml file.
+ Then, add the Sensor QML types into your qml file.
- In this example we use the TiltSensor with values based in degrees and an accuracy of 5 degree:
+ In this example we use the TiltSensor:
\snippet maze/maze.qml 1
- Starting the sensor can be done by setting the 'enabled' property to true:
-
- \snippet maze/maze.qml 2
-
The mouse should move by a factor of the tilt value:
- \snippet maze/maze.qml 3
+ \snippet maze/maze.qml 2
The walk direction of the mouse takes into account some collision detection:
- \snippet maze/maze.qml 4
+ \snippet maze/maze.qml 3
The rotation of the mouse image is determined according to the angle that the mouse is moving.
diff --git a/examples/sensors/maze/maze.qml b/examples/sensors/maze/maze.qml
index e5b4b0fe..12078447 100644
--- a/examples/sensors/maze/maze.qml
+++ b/examples/sensors/maze/maze.qml
@@ -91,10 +91,65 @@ import "lib.js" as Lib
ApplicationWindow {
id: mainWnd
+ property bool gameRunning: false
- property Mouse mouseCtrl;
- property LabyrinthSquare cheeseSquare;
- property Congratulation congratulation;
+ Component.onCompleted: {
+ initializeMaze()
+ newGame()
+ }
+
+ function initializeMaze() {
+ Lib.objectArray = new Array(Lib.dimension * Lib.dimension);
+ Lib.createLabyrinth();
+ var idx = 0;
+ var component = Qt.createComponent("LabyrinthSquare.qml");
+ for (var y = 0; y < Lib.dimension; y++ ) {
+ for (var x = 0; x < Lib.dimension; x++ ) {
+ var square = component.createObject(gameRect);
+ if (!square) {
+ console.log("error loading labyrinth square: " + component.errorString())
+ return
+ }
+ square.x = x * square.width;
+ square.y = y * square.height;
+ square.val = Lib.labyrinth[x][y];
+ Lib.objectArray[idx] = square;
+ idx++;
+ }
+ }
+ }
+
+ function newGame() {
+ congratulation.visible = false;
+
+ // Reset game time
+ timePlayingLabel.text = "--";
+ Lib.sec = 0.0;
+
+ // Create new labyrinth
+ Lib.createLabyrinth();
+ // Update maze tiles to match the new labyrinth
+ var idx = 0;
+ for (var y = 0; y < Lib.dimension; y++ ) {
+ for (var x = 0; x < Lib.dimension; x++ ) {
+ Lib.objectArray[idx].val = Lib.labyrinth[x][y];
+ Lib.objectArray[idx].updateImage();
+ idx++;
+ }
+ }
+ // Reset mouse position and start the game
+ mouseCtrl.x = 0;
+ mouseCtrl.y = 0;
+ mainWnd.gameRunning = true;
+ }
+
+ function gameWon() {
+ // Update the cheese square at the bottom right (win animation)
+ Lib.objectArray[Lib.dimension * Lib.dimension - 1].val = 4
+ Lib.objectArray[Lib.dimension * Lib.dimension - 1].updateImage()
+ congratulation.visible = true;
+ mainWnd.gameRunning = false;
+ }
Rectangle {
id: gameRect
@@ -104,78 +159,15 @@ ApplicationWindow {
height: Lib.dimension * Lib.cellDimension
color: "transparent"
border.width: 2
+ }
- //timer for starting the labyrinth game
- Timer {
- id: startTimer
- interval: 50; running: true; repeat: false
- onTriggered: {
-
- //reset game time
- timePlayingLabel.text = "--";
- Lib.sec = 0.0;
- Lib.createLabyrinth();
-
- //create labyrinth elements (only at the first time)
- var needloadcomponent = false;
- if (Lib.objectArray === null) {
- needloadcomponent = true;
- Lib.objectArray = new Array(Lib.dimension * Lib.dimension);
- }
- var idx = 0;
- for (var y = 0; y < Lib.dimension; y++ ) {
- for (var x = 0; x < Lib.dimension; x++ ) {
- var component = null;
-
- //create labyrinth components (only at the first time)
- if (needloadcomponent) {
- component = Qt.createComponent("LabyrinthSquare.qml");
- if (component.status == Component.Ready) {
- var square = component.createObject(parent);
- square.x = x * square.width;
- square.y = y * square.height;
- square.val = Lib.labyrinth[x][y];
- square.updateImage();
- Lib.objectArray[idx] = square;
- if (x == (Lib.dimension - 1) && y == (Lib.dimension - 1)) {
- cheeseSquare = square;
- var component1 = Qt.createComponent("Congratulation.qml");
- if (component1.status == Component.Ready) {
- congratulation = component1.createObject(parent);
- congratulation.visible = false;
- }
- }
- }
- }
- else{
- Lib.objectArray[idx].val = Lib.labyrinth[x][y];
- Lib.objectArray[idx].updateImage();
- if (x == (Lib.dimension - 1) && y == (Lib.dimension - 1)) {
- cheeseSquare = Lib.objectArray[idx];
- congratulation.visible = false;
- }
- }
- idx++;
- }
- }
-
- //Lib.printLab(); //this is for debug. Labyrinth will be printed out in the console
-
- //Create the mouse control (only at the first time)
- if (mouseCtrl === null) {
- var component = Qt.createComponent("Mouse.qml");
- if (component.status == Component.Ready) {
- mouseCtrl = component.createObject(parent);
- }
- }
- mouseCtrl.x = 0;
- mouseCtrl.y = 0;
- newGameButton.enabled = true;
+ Mouse {
+ id: mouseCtrl
+ }
- //Start the Tilt reader timer
- tiltTimer.running = true;
- }
- }
+ Congratulation {
+ id: congratulation
+ visible: false
}
//! [1]
@@ -188,78 +180,64 @@ ApplicationWindow {
//Timer to read out the x and y rotation of the TiltSensor
Timer {
id: tiltTimer
- interval: 50; running: false; repeat: true
+ interval: 50
+ repeat: true
+ running: tiltSensor.active && mainWnd.gameRunning
-//! [2]
onTriggered: {
- if (!tiltSensor.enabled)
- tiltSensor.active = true;
-//! [2]
-
- if (mouseCtrl === null)
- return;
+ // Update the maze unless game is already won
+ if (Lib.won === true) {
+ gameWon()
+ return
+ }
+ Lib.sec += 0.05;
+ timePlayingLabel.text = Math.floor(Lib.sec) + " seconds";
- //check if already solved
- if (Lib.won !== true) {
- Lib.sec += 0.05;
- timePlayingLabel.text = Math.floor(Lib.sec) + " seconds";
+ //check if we can move the mouse
+ var xval = -1;
+ var yval = -1;
- //check if we can move the mouse
- var xval = -1;
- var yval = -1;
+//! [2]
+ var xstep = 0;
+ xstep = tiltSensor.reading.yRotation * 0.1 //acceleration
+ var ystep = 0;
+ ystep = tiltSensor.reading.xRotation * 0.1 //acceleration
+//! [2]
//! [3]
- var xstep = 0;
- xstep = tiltSensor.reading.yRotation * 0.1 //acceleration
-
- var ystep = 0;
- ystep = tiltSensor.reading.xRotation * 0.1 //acceleration
+ if (xstep < 1 && xstep > 0)
+ xstep = 0
+ else if (xstep > -1 && xstep < 0)
+ xstep = 0
+
+ if (ystep < 1 && ystep > 0)
+ ystep = 0;
+ else if (ystep > -1 && ystep < 0)
+ ystep = 0;
+
+ if ((xstep < 0 && mouseCtrl.x > 0
+ && Lib.canMove(mouseCtrl.x + xstep,mouseCtrl.y))) {
+ xval = mouseCtrl.x + xstep;
+
+ } else if (xstep > 0 && mouseCtrl.x < (Lib.cellDimension * (Lib.dimension - 1))
+ && Lib.canMove(mouseCtrl.x + xstep,mouseCtrl.y)) {
+ xval = mouseCtrl.x + xstep;
+ } else
+ xval = mouseCtrl.x;
+
+ if (ystep < 0 && mouseCtrl.y > 0
+ && Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)) {
+ yval = mouseCtrl.y + ystep;
+ } else if (ystep > 0 && (mouseCtrl.y < (Lib.cellDimension * (Lib.dimension - 1)))
+ && Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)) {
+ yval = mouseCtrl.y + ystep;
+ } else
+ yval = mouseCtrl.y
+ mouseCtrl.move(xval, yval);
//! [3]
-//! [4]
- if (xstep < 1 && xstep > 0)
- xstep = 0
- else if (xstep > -1 && xstep < 0)
- xstep = 0
-
- if (ystep < 1 && ystep > 0)
- ystep = 0;
- else if (ystep > -1 && ystep < 0)
- ystep = 0;
-
- if ((xstep < 0 && mouseCtrl.x > 0
- && Lib.canMove(mouseCtrl.x + xstep,mouseCtrl.y))) {
- xval = mouseCtrl.x + xstep;
-
- } else if (xstep > 0 && mouseCtrl.x < (Lib.cellDimension * (Lib.dimension - 1))
- && Lib.canMove(mouseCtrl.x + xstep,mouseCtrl.y)) {
- xval = mouseCtrl.x + xstep;
- } else
- xval = mouseCtrl.x;
-
- if (ystep < 0 && mouseCtrl.y > 0
- && Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)) {
- yval = mouseCtrl.y + ystep;
- } else if (ystep > 0 && (mouseCtrl.y < (Lib.cellDimension * (Lib.dimension - 1)))
- && Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)) {
- yval = mouseCtrl.y + ystep;
- } else
- yval = mouseCtrl.y
-
- mouseCtrl.move(xval, yval);
-//! [4]
-
- } else {
- //game won, stop the tilt meter
- mainWnd.cheeseSquare.val = 4;
- mainWnd.cheeseSquare.updateImage();
- mainWnd.congratulation.visible = true;
- newGameButton.enabled = true;
- tiltTimer.running = false;
- }
}
}
-
//Button to start a new Game
Button{
id: newGameButton
@@ -268,12 +246,8 @@ ApplicationWindow {
anchors.topMargin: 5
height: 30
width: 100
- text: "new game"
- enabled: false;
- onClicked: {
- newGameButton.enabled = false;
- startTimer.start();
- }
+ text: qsTr("New game")
+ onClicked: newGame()
}
Button{
id: calibrateButton
@@ -282,10 +256,17 @@ ApplicationWindow {
anchors.topMargin: 5
height: 30
width: 100
- text: "calibrate"
- onClicked: {
- tiltSensor.calibrate();
- }
+ text: qsTr("Calibrate")
+ onClicked: tiltSensor.calibrate();
+ }
+
+ Text {
+ id: tiltSensorInfo
+ visible: tiltSensor.active
+ anchors.left: gameRect.left
+ anchors.top: calibrateButton.bottom
+ anchors.topMargin: 5
+ text: qsTr("Tilt sensor ID: ") + tiltSensor.identifier
}
//Label to print out the game time
@@ -295,5 +276,23 @@ ApplicationWindow {
anchors.top: gameRect.bottom
anchors.topMargin: 5
}
+
+ Rectangle {
+ id: tiltSensorMissing
+ visible: !tiltSensor.active
+ anchors.fill: parent
+ color: "#AACCCCCC" // slightly transparent
+ Text {
+ anchors.centerIn: parent
+ text: qsTr("Tilt sensor\nnot found")
+ font.pixelSize: 24
+ font.bold: true
+ color: "black"
+ }
+ MouseArea {
+ // prevent interaction with the game
+ anchors.fill: parent
+ }
+ }
}