summaryrefslogtreecommitdiffstats
path: root/QtDemo/qml/QtDemo/demos/slidepuzzle/slidepuzzle.qml
blob: 7bddc70a738b0a6fdcaa9d888c06e613f2749031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import QtQuick 2.0

Rectangle {
    id: game
    anchors.fill: parent
    gradient: Gradient {
        GradientStop { position: 0.0; color: "#222222" }
        GradientStop { position: 0.3; color: "#666666" }
        GradientStop { position: 0.8; color: "#444444" }
    }
    property int freeX: 2;
    property int freeY: 2;
    property int time: 0;
    property int clicks: 0;
    property bool gameRunning: false;

    Toolbar { id: toolbar }

    // Game table
    Rectangle {
        id: gameTable
        color: "transparent"
        smooth: true
        anchors.fill: parent
        anchors.centerIn: parent
        anchors.leftMargin: 0.3*parent.width;

        Image {
            id: backgroundImage
            anchors.fill: parent
            source: "images/QtLogo_background.png"
            smooth: true
            opacity: 0.2

            MouseArea {
                anchors.fill: parent
                onPressed: backgroundImage.opacity = 0.5
                onReleased: backgroundImage.opacity = 0.2
            }
        }

        ButtonList { id: buttons; }
        Button {
            index: 8
            posX:2
            posY:2
            visible: !game.gameRunning
            opacity: 1.0
        }
    }

    // Click clock timer
    Timer {
        id: clockTimer
        interval: 1000
        running: false
        repeat: true
        onTriggered: {
            toolbar.setElapsedSeconds(++time);
        }
    }

    // Start timer
    Timer {
        id: startTimer
        interval: 10
        running: false
        repeat: false
        onTriggered: game.startGame()
    }

    // Move button to the x,y and set free position
    function moveButton(button, x, y)
    {
        var tempX = button.posX;
        var tempY = button.posY;
        button.posX = x;
        button.posY = y;
        freeX = tempX;
        freeY = tempY;
    }

    function move(button)
    {
        // Move button to the free place
        if (((freeX == button.posX-1 || freeX == button.posX+1) && freeY === button.posY) ||
                ((freeY == button.posY-1 || freeY == button.posY+1) && freeX === button.posX))
        {
            if (game.gameRunning)
                toolbar.setClicks(++clicks);
            moveButton(button, freeX, freeY);
        }
    }

    function buttonWidth() {
        return 0.33*gameTable.width;
    }
    function buttonHeight() {
        return 0.33*gameTable.height;
    }

    // Check if our game is over
    function checkGameOver()
    {
        if (game.gameRunning)
        {
            var startX = buttonWidth()/2;
            var startY = buttonHeight()/2;
            var stepX = buttonWidth();
            var stepY = buttonHeight();
            var index = 0;
            var ready = true;

            // Check if we are ready. Set the ready-flag in the buttons
            // from the start to the first wrong button
            for (var y=0; y<3; y++)
            {
                for (var x=0; x<3; x++)
                {
                    var xx = startX + x*stepX;
                    var yy = startY + y*stepY;
                    var btn = buttons.childAt(xx,yy);
                    if (btn !== null)
                    {
                        ready = ready & index === btn.index;
                        btn.ready = ready;
                    }
                    else if (y != 2 || x != 2)
                    {
                        ready = false;
                    }

                    index++;
                }
            }

            if (ready)
            {
                game.gameRunning = false;
                clockTimer.stop();
                // show particles
            }
        }
    }

    // Here we change buttons position
    function shakeButtons()
    {
        var startX = buttonWidth()/2;
        var startY = buttonHeight()/2;
        var stepX = buttonWidth();
        var stepY = buttonHeight();

        var now = new Date();
        var seed = now.getMilliseconds();

        for (var i=0; i<200; i++)
        {
            seed++;
            var num = (Math.floor(4 * Math.random(seed)));

            var xNew = freeX;
            var yNew = freeY;
            switch (num)
            {
            case 0: xNew = Math.max(0,freeX-1); break; //move left
            case 1: xNew = Math.min(2,freeX+1); break; //move right
            case 2: yNew = Math.max(0,freeY-1); break; //move up
            default: yNew = Math.min(2,freeX+1); break; //move down
            }

            var xx = startX + xNew*stepX;
            var yy = startY + yNew*stepY;
            if (buttons.childAt(xx,yy) !== null)
                game.move(buttons.childAt(xx,yy));
        }

    }

    function startNewGame()
    {
        game.gameRunning = false;
        clockTimer.stop();
        startTimer.start();
    }

    // Start game with shaking buttons
    function startGame()
    {
        // Shake buttons and start new game
        shakeButtons();

        game.gameRunning = true;
        clicks = 0;
        time = 0;
        toolbar.setClicks(0);
        toolbar.setElapsedSeconds(0);
        clockTimer.start();
        checkGameOver();
    }
}