1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtDeclarative module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | import QtQuick 1.0
|
---|
43 | import "content" as Content
|
---|
44 | import "content/snake.js" as Logic
|
---|
45 |
|
---|
46 | Rectangle {
|
---|
47 | id: screen;
|
---|
48 | SystemPalette { id: activePalette }
|
---|
49 | color: activePalette.window
|
---|
50 | property bool activeGame: false
|
---|
51 |
|
---|
52 | property int gridSize : 34
|
---|
53 | property int margin: 4
|
---|
54 | property int numRowsAvailable: Math.floor((height-32-2*margin)/gridSize)
|
---|
55 | property int numColumnsAvailable: Math.floor((width-2*margin)/gridSize)
|
---|
56 |
|
---|
57 | property int lastScore : 0
|
---|
58 |
|
---|
59 | property int score: 0;
|
---|
60 | property int heartbeatInterval: 200
|
---|
61 | property int halfbeatInterval: 160
|
---|
62 |
|
---|
63 | width: 480
|
---|
64 | height: 750
|
---|
65 |
|
---|
66 | property int direction
|
---|
67 | property int headDirection
|
---|
68 |
|
---|
69 | property variant head;
|
---|
70 |
|
---|
71 | Content.HighScoreModel {
|
---|
72 | id: highScores
|
---|
73 | game: "Snake"
|
---|
74 | }
|
---|
75 |
|
---|
76 | Timer {
|
---|
77 | id: heartbeat;
|
---|
78 | interval: heartbeatInterval;
|
---|
79 | running: activeGame && runtime.isActiveWindow
|
---|
80 | repeat: true
|
---|
81 | onTriggered: { Logic.move() }
|
---|
82 | }
|
---|
83 | Timer {
|
---|
84 | id: halfbeat;
|
---|
85 | interval: halfbeatInterval;
|
---|
86 | repeat: true
|
---|
87 | running: heartbeat.running
|
---|
88 | onTriggered: { Logic.moveSkull() }
|
---|
89 | }
|
---|
90 | Timer {
|
---|
91 | id: startNewGameTimer;
|
---|
92 | interval: 700;
|
---|
93 | onTriggered: { Logic.startNewGame(); }
|
---|
94 | }
|
---|
95 |
|
---|
96 | Timer {
|
---|
97 | id: startHeartbeatTimer;
|
---|
98 | interval: 1000 ;
|
---|
99 | onTriggered: { state = "running"; activeGame = true; }
|
---|
100 | }
|
---|
101 |
|
---|
102 | Image{
|
---|
103 | id: pauseDialog
|
---|
104 | z: 1
|
---|
105 | source: "content/pics/pause.png"
|
---|
106 | anchors.centerIn: parent;
|
---|
107 | //opacity is deliberately not animated
|
---|
108 | opacity: activeGame && !runtime.isActiveWindow
|
---|
109 | }
|
---|
110 |
|
---|
111 | Image {
|
---|
112 | Image {
|
---|
113 | id: title
|
---|
114 | source: "content/pics/snake.jpg"
|
---|
115 | fillMode: Image.PreserveAspectCrop
|
---|
116 | anchors.fill: parent
|
---|
117 | anchors.horizontalCenter: parent.horizontalCenter
|
---|
118 | anchors.verticalCenter: parent.verticalCenter
|
---|
119 |
|
---|
120 | Text {
|
---|
121 | color: "white"
|
---|
122 | font.pointSize: 24
|
---|
123 | horizontalAlignment: Text.AlignHCenter
|
---|
124 | text: "Last Score:\t" + lastScore + "\nHighscore:\t" + highScores.topScore;
|
---|
125 | anchors.horizontalCenter: parent.horizontalCenter
|
---|
126 | anchors.bottom: parent.bottom
|
---|
127 | anchors.bottomMargin: gridSize
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | source: "content/pics/background.png"
|
---|
132 | fillMode: Image.PreserveAspectCrop
|
---|
133 |
|
---|
134 | anchors.left: parent.left
|
---|
135 | anchors.right: parent.right
|
---|
136 | anchors.top: parent.top
|
---|
137 | anchors.bottom: toolbar.top
|
---|
138 |
|
---|
139 | Rectangle {
|
---|
140 | id: playfield
|
---|
141 | border.width: 1
|
---|
142 | border.color: "white"
|
---|
143 | color: "transparent"
|
---|
144 | anchors.horizontalCenter: parent.horizontalCenter
|
---|
145 | y: (screen.height - 32 - height)/2;
|
---|
146 | width: numColumnsAvailable * gridSize + 2*margin
|
---|
147 | height: numRowsAvailable * gridSize + 2*margin
|
---|
148 |
|
---|
149 |
|
---|
150 | Content.Skull {
|
---|
151 | id: skull
|
---|
152 | }
|
---|
153 |
|
---|
154 | MouseArea {
|
---|
155 | anchors.fill: parent
|
---|
156 | onPressed: {
|
---|
157 | if (!head || !heartbeat.running) {
|
---|
158 | Logic.startNewGame();
|
---|
159 | return;
|
---|
160 | }
|
---|
161 | if (direction == 0 || direction == 2)
|
---|
162 | Logic.scheduleDirection((mouseX > (head.x + head.width/2)) ? 1 : 3);
|
---|
163 | else
|
---|
164 | Logic.scheduleDirection((mouseY > (head.y + head.height/2)) ? 2 : 0);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | }
|
---|
170 |
|
---|
171 | Rectangle {
|
---|
172 | id: progressBar
|
---|
173 | opacity: 0
|
---|
174 | Behavior on opacity { NumberAnimation { duration: 200 } }
|
---|
175 | color: "transparent"
|
---|
176 | border.width: 2
|
---|
177 | border.color: "#221edd"
|
---|
178 | x: 50
|
---|
179 | y: 50
|
---|
180 | width: 200
|
---|
181 | height: 30
|
---|
182 | anchors.horizontalCenter: parent.horizontalCenter
|
---|
183 | anchors.verticalCenter: parent.verticalCenter
|
---|
184 | anchors.verticalCenterOffset: 40
|
---|
185 |
|
---|
186 | Rectangle {
|
---|
187 | id: progressIndicator
|
---|
188 | color: "#221edd";
|
---|
189 | width: 0;
|
---|
190 | height: 30;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | Rectangle {
|
---|
195 | id: toolbar
|
---|
196 | color: activePalette.window
|
---|
197 | height: 32; width: parent.width
|
---|
198 | anchors.bottom: screen.bottom
|
---|
199 |
|
---|
200 | Content.Button {
|
---|
201 | id: btnA; text: "New Game"; onClicked: Logic.startNewGame();
|
---|
202 | anchors.left: parent.left; anchors.leftMargin: 3
|
---|
203 | anchors.verticalCenter: parent.verticalCenter
|
---|
204 | }
|
---|
205 |
|
---|
206 | Content.Button {
|
---|
207 | text: "Quit"
|
---|
208 | anchors { left: btnA.right; leftMargin: 3; verticalCenter: parent.verticalCenter }
|
---|
209 | onClicked: Qt.quit();
|
---|
210 | }
|
---|
211 |
|
---|
212 | Text {
|
---|
213 | color: activePalette.text
|
---|
214 | text: "Score: " + score; font.bold: true
|
---|
215 | anchors.right: parent.right; anchors.rightMargin: 3
|
---|
216 | anchors.verticalCenter: parent.verticalCenter
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | focus: true
|
---|
221 | Keys.onSpacePressed: Logic.startNewGame();
|
---|
222 | Keys.onLeftPressed: if (state == "starting" || direction != 1) Logic.scheduleDirection(3);
|
---|
223 | Keys.onRightPressed: if (state == "starting" || direction != 3) Logic.scheduleDirection(1);
|
---|
224 | Keys.onUpPressed: if (state == "starting" || direction != 2) Logic.scheduleDirection(0);
|
---|
225 | Keys.onDownPressed: if (state == "starting" || direction != 0) Logic.scheduleDirection(2);
|
---|
226 |
|
---|
227 | states: [
|
---|
228 | State {
|
---|
229 | name: "starting"
|
---|
230 | PropertyChanges {target: progressIndicator; width: 200}
|
---|
231 | PropertyChanges {target: title; opacity: 0}
|
---|
232 | PropertyChanges {target: progressBar; opacity: 1}
|
---|
233 | },
|
---|
234 | State {
|
---|
235 | name: "running"
|
---|
236 | PropertyChanges {target: progressIndicator; width: 200}
|
---|
237 | PropertyChanges {target: title; opacity: 0}
|
---|
238 | PropertyChanges {target: skull; row: 0; column: 0; }
|
---|
239 | PropertyChanges {target: skull; spawned: 1}
|
---|
240 | }
|
---|
241 | ]
|
---|
242 |
|
---|
243 | transitions: [
|
---|
244 | Transition {
|
---|
245 | from: "*"
|
---|
246 | to: "starting"
|
---|
247 | NumberAnimation { target: progressIndicator; property: "width"; duration: 1000 }
|
---|
248 | NumberAnimation { target: title; property: "opacity"; duration: 500 }
|
---|
249 | },
|
---|
250 | Transition {
|
---|
251 | NumberAnimation { target: title; property: "opacity"; duration: 500 }
|
---|
252 | }
|
---|
253 | ]
|
---|
254 |
|
---|
255 | }
|
---|