From 4a30e784182a41d54d8b908ba47d948ed4172236 Mon Sep 17 00:00:00 2001 From: Wolfgang Beck Date: Fri, 23 Sep 2011 15:05:58 +1000 Subject: MTMW-288 add Maze (TiltSensor Example) Change-Id: I023e4baeb82facfb7d2f97c3b6076d7f1e816e16 Reviewed-on: http://codereview.qt-project.org/5434 Reviewed-by: Wolfgang Beck --- examples/sensors/maze/Button.qml | 89 ++++++++ examples/sensors/maze/Congratulation.qml | 21 ++ examples/sensors/maze/LabyrinthSquare.qml | 40 ++++ examples/sensors/maze/Maze.qml | 233 ++++++++++++++++++++ examples/sensors/maze/Mouse.qml | 76 +++++++ examples/sensors/maze/icon.png | Bin 0 -> 4693 bytes examples/sensors/maze/images/00.png | Bin 0 -> 186 bytes examples/sensors/maze/images/01.png | Bin 0 -> 708 bytes examples/sensors/maze/images/cheese.png | Bin 0 -> 317 bytes examples/sensors/maze/images/cheeseeating.gif | Bin 0 -> 650 bytes examples/sensors/maze/images/congratulations.gif | Bin 0 -> 10811 bytes examples/sensors/maze/images/mouse_down.gif | Bin 0 -> 974 bytes examples/sensors/maze/images/mouse_left.gif | Bin 0 -> 970 bytes examples/sensors/maze/images/mouse_leftdown.gif | Bin 0 -> 976 bytes examples/sensors/maze/images/mouse_leftup.gif | Bin 0 -> 976 bytes examples/sensors/maze/images/mouse_right.gif | Bin 0 -> 971 bytes examples/sensors/maze/images/mouse_rightdown.gif | Bin 0 -> 977 bytes examples/sensors/maze/images/mouse_rightup.gif | Bin 0 -> 975 bytes examples/sensors/maze/images/mouse_up.gif | Bin 0 -> 970 bytes examples/sensors/maze/images/start.png | Bin 0 -> 677 bytes examples/sensors/maze/info.json | 14 ++ examples/sensors/maze/lib.js | 264 +++++++++++++++++++++++ examples/sensors/maze/maze.qmlproject | 20 ++ 23 files changed, 757 insertions(+) create mode 100644 examples/sensors/maze/Button.qml create mode 100644 examples/sensors/maze/Congratulation.qml create mode 100644 examples/sensors/maze/LabyrinthSquare.qml create mode 100644 examples/sensors/maze/Maze.qml create mode 100644 examples/sensors/maze/Mouse.qml create mode 100755 examples/sensors/maze/icon.png create mode 100644 examples/sensors/maze/images/00.png create mode 100644 examples/sensors/maze/images/01.png create mode 100644 examples/sensors/maze/images/cheese.png create mode 100644 examples/sensors/maze/images/cheeseeating.gif create mode 100644 examples/sensors/maze/images/congratulations.gif create mode 100644 examples/sensors/maze/images/mouse_down.gif create mode 100644 examples/sensors/maze/images/mouse_left.gif create mode 100644 examples/sensors/maze/images/mouse_leftdown.gif create mode 100644 examples/sensors/maze/images/mouse_leftup.gif create mode 100644 examples/sensors/maze/images/mouse_right.gif create mode 100644 examples/sensors/maze/images/mouse_rightdown.gif create mode 100644 examples/sensors/maze/images/mouse_rightup.gif create mode 100644 examples/sensors/maze/images/mouse_up.gif create mode 100644 examples/sensors/maze/images/start.png create mode 100644 examples/sensors/maze/info.json create mode 100644 examples/sensors/maze/lib.js create mode 100644 examples/sensors/maze/maze.qmlproject (limited to 'examples') diff --git a/examples/sensors/maze/Button.qml b/examples/sensors/maze/Button.qml new file mode 100644 index 00000000..bbb63a2c --- /dev/null +++ b/examples/sensors/maze/Button.qml @@ -0,0 +1,89 @@ +//Import the declarative plugins +import QtQuick 2.0 + +//Implementation of the Button control. +Item { + id: button + width: 30 + height: 30 + property alias buttonText: innerText.text; + property color color: "white" + property color hoverColor: "#aaaaaa" + property color pressColor: "slategray" + property int fontSize: 10 + property int borderWidth: 1 + property int borderRadius: 2 + scale: state === "Pressed" ? 0.96 : 1.0 + onEnabledChanged: state = "" + signal clicked + + //define a scale animation + Behavior on scale { + NumberAnimation { + duration: 100 + easing.type: Easing.InOutQuad + } + } + + //Rectangle to draw the button + Rectangle { + id: rectangleButton + anchors.fill: parent + radius: borderRadius + color: button.enabled ? button.color : "grey" + border.width: borderWidth + border.color: "black" + + Text { + id: innerText + font.pointSize: fontSize + anchors.centerIn: parent + } + } + + //change the color of the button in differen button states + states: [ + State { + name: "Hovering" + PropertyChanges { + target: rectangleButton + color: hoverColor + } + }, + State { + name: "Pressed" + PropertyChanges { + target: rectangleButton + color: pressColor + } + } + ] + + //define transmission for the states + transitions: [ + Transition { + from: ""; to: "Hovering" + ColorAnimation { duration: 200 } + }, + Transition { + from: "*"; to: "Pressed" + ColorAnimation { duration: 10 } + } + ] + + //Mouse area to react on click events + MouseArea { + hoverEnabled: true + anchors.fill: button + onEntered: { button.state='Hovering'} + onExited: { button.state=''} + onClicked: { button.clicked();} + onPressed: { button.state="Pressed" } + onReleased: { + if (containsMouse) + button.state="Hovering"; + else + button.state=""; + } + } +} diff --git a/examples/sensors/maze/Congratulation.qml b/examples/sensors/maze/Congratulation.qml new file mode 100644 index 00000000..2f5ffb1a --- /dev/null +++ b/examples/sensors/maze/Congratulation.qml @@ -0,0 +1,21 @@ +//Import the declarative plugins +import QtQuick 2.0 + +//Import the javascript functions for this game +import "lib.js" as Lib + +//Implementation of the Congratulation control +Item { + x: Lib.cellDimension + y: Lib.cellDimension + width: Lib.cellDimension * (Lib.dimension - 2) + height: Lib.cellDimension * (Lib.dimension - 2) + + //Containing a animated gif image + AnimatedImage { + id: img + anchors.fill: parent + visible: true + source: "images/congratulations.gif" + } +} diff --git a/examples/sensors/maze/LabyrinthSquare.qml b/examples/sensors/maze/LabyrinthSquare.qml new file mode 100644 index 00000000..b82c8b2f --- /dev/null +++ b/examples/sensors/maze/LabyrinthSquare.qml @@ -0,0 +1,40 @@ +//Import the declarative plugins +import QtQuick 2.0 + +//Import the javascript functions for this game +import "lib.js" as Lib + +//Implementation of the Labyrinth square control. +Rectangle { + id: sq + x: 0 + y: 0 + width: Lib.cellDimension + height: Lib.cellDimension + color: "white" + property int val: 0 + property AnimatedImage picture: img + + //Dependend of its position and the labyrinth value a square can be the start, cheese, empty or a wall + AnimatedImage { + id: img + anchors.fill: parent + source: "" + } + + function updateImage() + { + if (sq.val == -1) + sq.picture.source = "images/start.png"; + else if (sq.val == 3) + sq.picture.source = "images/cheese.png"; + else if (sq.val == 4){ + sq.picture.source = "images/cheeseeating.gif"; + sq.picture.playing = true; + } + else if (sq.val == 1) + sq.picture.source = "images/01.png"; + else + sq.picture.source = "images/00.png"; + } +} diff --git a/examples/sensors/maze/Maze.qml b/examples/sensors/maze/Maze.qml new file mode 100644 index 00000000..95bceeff --- /dev/null +++ b/examples/sensors/maze/Maze.qml @@ -0,0 +1,233 @@ +//Import the declarative plugins +import QtQuick 2.0 +import QtSensors 5.0 + +//Import the javascript functions for this game +import "lib.js" as Lib + +/* Layout + mainWnd + / +------------------------------/ +| +|----------------------------- +|||M| | +|| \ | +|| mouseCtrl | +|| | +|| | +|| Labyrinth | +|| | +|| | +|| cheeseSquare | +|| \ | +|| |C|| +|----------------------------- +| +|---------- -------------- +|| | | | +|---------- -------------- +| \ \ +| \ timePlayingLabel +| newGameButton +------------------------------ + +*/ + +Rectangle { + id: mainWnd + x: 0 + y: 30 + width: 320 + height: 440 + color: "white" + + property Mouse mouseCtrl; + property LabyrinthSquare cheeseSquare; + property Congratulation congratulation; + + //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; + + //Start the Tilt reader timer + tiltTimer.running = true; + } + } + + TiltSensor{ + id: tiltSensor + radian: false + measureFrom: TiltSensor.FaceUp + running: true + } + + //Timer to read out the x and y rotation of the TiltSensor + Timer { + id: tiltTimer + interval: 50; running: false; repeat: true + onTriggered: { + if (!tiltSensor.running) + tiltSensor.running = true; + + if (mouseCtrl === null) + return; + + //check if already solved + if (Lib.won !== true){ + Lib.sec += 0.05; + timePlayingLabel.text = Math.floor(Lib.sec) + " secounds"; + + //check if we can move the mouse + var xval = -1; + var yval = -1; + var xstep = 0; + if (tiltSensor.yRotation > 0) + xstep = 1; + else if (tiltSensor.yRotation < 0) + xstep = -1; + var ystep = 0; + if (tiltSensor.xRotation > 0) + ystep = 1; + else if (tiltSensor.xRotation < 0) + ystep = -1; + + if (xstep < 0){ + if (mouseCtrl.x > 0){ + if (Lib.canMove(mouseCtrl.x + xstep, mouseCtrl.y)){ + xval = mouseCtrl.x + xstep; + } + } + } + else if (xstep > 0){ + if (mouseCtrl.x < (Lib.cellDimension * (Lib.dimension - 1))){ + if (Lib.canMove(mouseCtrl.x + xstep, mouseCtrl.y)){ + xval = mouseCtrl.x + xstep; + } + } + } + if (ystep < 0){ + if (mouseCtrl.y > 0){ + if (Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)){ + yval = mouseCtrl.y + ystep; + } + } + } + else if (ystep > 0){ + if (mouseCtrl.y < (Lib.cellDimension * (Lib.dimension - 1))){ + if (Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)){ + yval = mouseCtrl.y + ystep; + } + } + } + if (xval >= 0 && yval >= 0) + mouseCtrl.move(xval, yval); + + //move the mouse in the allwed position + else{ + if (xval >= 0){ + mouseCtrl.move(xval, mouseCtrl.y); + } + if (yval >= 0){ + mouseCtrl.move(mouseCtrl.x, yval); + } + } + } + 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 + x: 0 + y: mainWnd.x + (Lib.dimension * Lib.cellDimension) + 10 + height: 30 + width: 100 + buttonText: "new game" + enabled: false; + onClicked: { + newGameButton.enabled = false; + startTimer.start(); + } + } + + //Label to print out the game time + Text{ + id: timePlayingLabel + x: newGameButton.x + newGameButton.width + 20 + y: newGameButton.y + 10 + } +} + diff --git a/examples/sensors/maze/Mouse.qml b/examples/sensors/maze/Mouse.qml new file mode 100644 index 00000000..32489b50 --- /dev/null +++ b/examples/sensors/maze/Mouse.qml @@ -0,0 +1,76 @@ +//Import the declarative plugins +import QtQuick 2.0 + +//Import the javascript functions for this game +import "lib.js" as Lib + +//Implementation of the Mouse control. +Item { + id: mouse + x: 0 + y: 0 + width: Lib.cellDimension + height: Lib.cellDimension + state: 'right' + + AnimatedImage { + id: img + anchors.fill: parent + visible: true + } + + //for different moves we have different gif animations + states: [ + State { + name: "up" + PropertyChanges { target: img; source: "images/mouse_up.gif" } + }, + State { + name: "down" + PropertyChanges { target: img; source: "images/mouse_down.gif" } + }, + State { + name: "left" + PropertyChanges { target: img; source: "images/mouse_left.gif" } + }, + State { + name: "right" + PropertyChanges { target: img; source: "images/mouse_right.gif" } + }, + + State { + name: "rightup" + PropertyChanges { target: img; source: "images/mouse_rightup.gif" } + }, + State { + name: "rightdown" + PropertyChanges { target: img; source: "images/mouse_rightdown.gif" } + }, + State { + name: "leftup" + PropertyChanges { target: img; source: "images/mouse_leftup.gif" } + }, + State { + name: "leftdown" + PropertyChanges { target: img; source: "images/mouse_leftdown.gif" } + } + + ] + + //Function for moving the mouse + function move(newx, newy) + { + var newstatus = ""; + if (mouse.x < newx) + newstatus += "right"; + else if (mouse.x > newx) + newstatus += "left"; + if (mouse.y < newy) + newstatus += "down"; + else if (mouse.y > newy) + newstatus += "up"; + mouse.state = newstatus; + mouse.x = newx; + mouse.y = newy; + } +} diff --git a/examples/sensors/maze/icon.png b/examples/sensors/maze/icon.png new file mode 100755 index 00000000..1e7ff8d8 Binary files /dev/null and b/examples/sensors/maze/icon.png differ diff --git a/examples/sensors/maze/images/00.png b/examples/sensors/maze/images/00.png new file mode 100644 index 00000000..ab29a622 Binary files /dev/null and b/examples/sensors/maze/images/00.png differ diff --git a/examples/sensors/maze/images/01.png b/examples/sensors/maze/images/01.png new file mode 100644 index 00000000..0e70d1b2 Binary files /dev/null and b/examples/sensors/maze/images/01.png differ diff --git a/examples/sensors/maze/images/cheese.png b/examples/sensors/maze/images/cheese.png new file mode 100644 index 00000000..ecef4503 Binary files /dev/null and b/examples/sensors/maze/images/cheese.png differ diff --git a/examples/sensors/maze/images/cheeseeating.gif b/examples/sensors/maze/images/cheeseeating.gif new file mode 100644 index 00000000..6b2cb47f Binary files /dev/null and b/examples/sensors/maze/images/cheeseeating.gif differ diff --git a/examples/sensors/maze/images/congratulations.gif b/examples/sensors/maze/images/congratulations.gif new file mode 100644 index 00000000..70204176 Binary files /dev/null and b/examples/sensors/maze/images/congratulations.gif differ diff --git a/examples/sensors/maze/images/mouse_down.gif b/examples/sensors/maze/images/mouse_down.gif new file mode 100644 index 00000000..5491c18f Binary files /dev/null and b/examples/sensors/maze/images/mouse_down.gif differ diff --git a/examples/sensors/maze/images/mouse_left.gif b/examples/sensors/maze/images/mouse_left.gif new file mode 100644 index 00000000..265d2bab Binary files /dev/null and b/examples/sensors/maze/images/mouse_left.gif differ diff --git a/examples/sensors/maze/images/mouse_leftdown.gif b/examples/sensors/maze/images/mouse_leftdown.gif new file mode 100644 index 00000000..bdcd708f Binary files /dev/null and b/examples/sensors/maze/images/mouse_leftdown.gif differ diff --git a/examples/sensors/maze/images/mouse_leftup.gif b/examples/sensors/maze/images/mouse_leftup.gif new file mode 100644 index 00000000..a8b1e8f6 Binary files /dev/null and b/examples/sensors/maze/images/mouse_leftup.gif differ diff --git a/examples/sensors/maze/images/mouse_right.gif b/examples/sensors/maze/images/mouse_right.gif new file mode 100644 index 00000000..a95d02cc Binary files /dev/null and b/examples/sensors/maze/images/mouse_right.gif differ diff --git a/examples/sensors/maze/images/mouse_rightdown.gif b/examples/sensors/maze/images/mouse_rightdown.gif new file mode 100644 index 00000000..00bc3e18 Binary files /dev/null and b/examples/sensors/maze/images/mouse_rightdown.gif differ diff --git a/examples/sensors/maze/images/mouse_rightup.gif b/examples/sensors/maze/images/mouse_rightup.gif new file mode 100644 index 00000000..e0153eb4 Binary files /dev/null and b/examples/sensors/maze/images/mouse_rightup.gif differ diff --git a/examples/sensors/maze/images/mouse_up.gif b/examples/sensors/maze/images/mouse_up.gif new file mode 100644 index 00000000..19220ebe Binary files /dev/null and b/examples/sensors/maze/images/mouse_up.gif differ diff --git a/examples/sensors/maze/images/start.png b/examples/sensors/maze/images/start.png new file mode 100644 index 00000000..698c0b1c Binary files /dev/null and b/examples/sensors/maze/images/start.png differ diff --git a/examples/sensors/maze/info.json b/examples/sensors/maze/info.json new file mode 100644 index 00000000..d31a2f0b --- /dev/null +++ b/examples/sensors/maze/info.json @@ -0,0 +1,14 @@ +{ + "info-version": "1.0", + "dict": { + "Category": "application", + "Runtime": "qml", + "DisplayName": "Maze", + "Subcategory": "utility", + "MainQML": "Maze.qml", + "Version": "1.0", + "Identifier": "com.noklab.nrcc.maze.demo", + "Summary": "Labyrinth game using TitlSensor", + "Author": "Qt" + } +} diff --git a/examples/sensors/maze/lib.js b/examples/sensors/maze/lib.js new file mode 100644 index 00000000..fccce18f --- /dev/null +++ b/examples/sensors/maze/lib.js @@ -0,0 +1,264 @@ +//global variables +var labyrinth = null; +var dimension = 24; +var cellDimension = 13; +var won; +var objectArray = null; +var sec = 0.0 + +//Allocate labyrinth arrays and create labyrinth and way reflected in the labyrinth array +function createLabyrinth() +{ + won = false; + //create the labyrinth matrix + labyrinth = null; + labyrinth = new Array(dimension); + for (var x = 0; x < dimension; x++ ){ + labyrinth[x] = new Array(dimension); + for (var y = 0; y < dimension; y++ ){ + labyrinth[x][y] = 0; + } + } + createWay(); + createLab(); +} + +//Create a way where the mouse can reach the cheese +function createWay() +{ + //Create rnd way to have at least one solution + //A way square is marked as a 2 in the labyrinth array + var x = 0; + var y = 0; + var ox = x; + var oy = y; + labyrinth[0][0] = 2; + while (x < dimension && y < dimension){ + var rnd = Math.floor(Math.random()*5); + if (Math.floor(Math.random()*2) == 1){ + if (rnd == 0) x--; + if (rnd >= 1) x++; + if (x < 0) x++; + if (x >= dimension){ + x = ox; + break; + } + } + else { + if (rnd == 0) y--; + if (rnd >= 1) y++; + if (y < 0) y++; + if (y >= dimension){ + y = oy; + break; + } + } + + /*avoid to have [2]2| + |2|2|*/ + if (x < (dimension - 1) && y < (dimension - 1)){ + if (labyrinth[x + 1][y] == 2 + && labyrinth[x][y + 1] == 2 + && labyrinth[x + 1][y + 1] == 2){ + y = oy; + x = ox; + continue; + } + } + /*avoid to have |2[2] + |2|2|*/ + if (x > 0 && y < (dimension - 1)){ + if (labyrinth[x - 1][y] == 2 + && labyrinth[x][y + 1] == 2 + && labyrinth[x - 1][y + 1] == 2){ + y = oy; + x = ox; + continue; + } + } + /*avoid to have |2|2| + [2]2|*/ + if (x < (dimension - 1) && y > 0){ + if (labyrinth[x + 1][y] == 2 + && labyrinth[x][y - 1] == 2 + && labyrinth[x + 1][y - 1] == 2){ + y = oy; + x = ox; + continue; + } + } + /*avoid to have |2|2| + |2[2]*/ + if (x > 0 && y > 0){ + if (labyrinth[x - 1][y] == 2 + && labyrinth[x][y - 1] == 2 + && labyrinth[x - 1][y - 1] == 2){ + y = oy; + x = ox; + continue; + } + } + + labyrinth[x][y] = 2; + ox = x; + oy = y; + } + //finish way + while (x < (dimension - 1)){ + labyrinth[x][y] = 2; + x++; + } + while (y < (dimension - 1)){ + labyrinth[x][y] = 2; + y++; + } +} + +//Create the labyrinth with rnd values +function createLab() +{ + //A wall square is marked as a 1 in the labyrinth array + //Not a wall square is marked as a 0 in the labyrinth array + //The Cheese square is marked as a 3 in the labyrinth array + //The start is marked as a -1 in the labyrinth array + for (var x = 0; x < dimension; x++ ){ + var rnd = 0; + for (var y = 0; y < dimension; y++){ + //But don't overwrite the way + if (labyrinth[x][y] != 2){ + var rnd = Math.floor(Math.random()*2); + var xy = 0; + var xxy = 0; + var xyy = 0; + var xxyy = 0; + + if (x > 0 && y > 0){ + xy = labyrinth[x - 1][y - 1]; + if (xy == 2) + xy = 0; + + xyy = labyrinth[x - 1][y]; + if (xyy == 2) + xyy = 0; + + xxy = labyrinth[x][y - 1]; + if (xxy == 2) + xxy = 0; + + xxyy = rnd; + if (xxyy == 2) + xxyy = 0; + + //avoid to have to many |0|1| or |1|0| [xy ][xxy ] + // |1[0] |0[1] [xyy ][xxyy] + if (xyy == xxy && xy == xxyy && xy != xxy){ + if (rnd == 1) + rnd = 0; + else rnd = 1; + } + + //avoid to have to many |1|1| or |0|0| + // |1[1] |0[0] + if (xy == xxy && xxy == xxyy && xxyy == xyy){ + if (rnd == 1) + rnd = 0; + else rnd = 1; + } + } + else if (x == 0 && y > 0){ + xy = labyrinth[x][y - 1]; + if (xy == 2) + xy = 0; + + xyy = rnd; + if (xyy == 2) + xyy = 0; + + xxy = labyrinth[x + 1][y - 1]; + if (xxy == 2) + xxy = 0; + + xxyy = labyrinth[x + 1][y]; + if (xxyy == 2) + xxyy = 0; + + //avoid to have to many |1|1| or |0|0| + // |1[1] |0[0] + if (xy == xxy && xxy == xxyy && xxyy == xyy){ + if (rnd == 1) + rnd = 0; + else rnd = 1; + } + + //avoid to have to many |0|1| or |1|0| [xy ][xxy ] + // |1[0] |0[1] [xyy ][xxyy] + if (xyy == xxy && xy == xxyy && xy != xxy){ + if (rnd == 1) + rnd = 0; + else rnd = 1; + } + } + labyrinth[x][y] = rnd; + } + + } + } + //set start and end + labyrinth[0][0] = -1; + labyrinth[0][1] = 0; + labyrinth[1][0] = 0; + labyrinth[1][1] = 0; + + labyrinth[dimension - 2][dimension - 2] = 0; + labyrinth[dimension - 2][dimension - 1] = 0; + labyrinth[dimension - 1][dimension - 2] = 0; + labyrinth[dimension - 1][dimension - 1] = 3; +} + +//Function that checks if the mouse can be moved in x and y +function canMove(x, y) +{ + //Check if movement is allowed + var xcenter = x + (cellDimension / 2); + var ycenter = y + (cellDimension / 2); + //try to get the index + var idx = Math.floor(xcenter / cellDimension); + var idy = Math.floor(ycenter / cellDimension); + var dx = xcenter - (idx * cellDimension + ( cellDimension / 2 )); + var dy = ycenter - (idy * cellDimension + ( cellDimension / 2 )); + + if (dx > 0){ + if (labyrinth[idx + 1][idy] == 1) + return false; + } + if (dx < 0){ + if (labyrinth[idx - 1][idy] == 1) + return false; + } + if (dy > 0){ + if (labyrinth[idx][idy + 1] == 1) + return false; + } + if (dy < 0){ + if (labyrinth[idx][idy - 1] == 1) + return false; + } + //check if won + if (idx == (dimension - 1) && idy == (dimension - 1)) + won = true; + return true; +} + +//Function that prints out the labyrith array values in the console +function printLab() +{ + //for debug purposes print out lab n console + var iy = 0; + for (var y = 0; y < dimension; y++ ){ + var line = ""; + for (var x = 0; x < dimension; x++ ){ + line += labyrinth[x][y]; + } + console.log(line); + } +} diff --git a/examples/sensors/maze/maze.qmlproject b/examples/sensors/maze/maze.qmlproject new file mode 100644 index 00000000..0134a31c --- /dev/null +++ b/examples/sensors/maze/maze.qmlproject @@ -0,0 +1,20 @@ +/* File generated by Qt Creator, version 2.2.0 */ + +import QmlProject 1.1 + +Project { + mainFile: "Maze.qml" + + /* Include .qml, .js, and image files from current directory and subdirectories */ + QmlFiles { + directory: "." + } + JavaScriptFiles { + directory: "." + } + ImageFiles { + directory: "." + } + /* List of plugin directories passed to QML runtime */ + // importPaths: [ "../exampleplugin" ] +} -- cgit v1.2.3