1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 import "content" as Content
44 import "content/snake.js" as Logic
48 SystemPalette { id: activePalette }
49 color: activePalette.window
50 property bool activeGame: false
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)
57 property int lastScore : 0
59 property int score: 0;
60 property int heartbeatInterval: 200
61 property int halfbeatInterval: 160
66 property int direction
67 property int headDirection
69 property variant head;
71 Content.HighScoreModel {
78 interval: heartbeatInterval;
79 running: activeGame && Qt.application.active
81 onTriggered: { Logic.move() }
85 interval: halfbeatInterval;
87 running: heartbeat.running
88 onTriggered: { Logic.moveSkull() }
91 id: startNewGameTimer;
93 onTriggered: { Logic.startNewGame(); }
97 id: startHeartbeatTimer;
99 onTriggered: { state = "running"; activeGame = true; }
105 source: "content/pics/pause.png"
106 anchors.centerIn: parent;
107 //opacity is deliberately not animated
108 opacity: activeGame && !Qt.application.active
115 source: "content/pics/snake.jpg"
116 fillMode: Image.PreserveAspectCrop
118 anchors.horizontalCenter: parent.horizontalCenter
119 anchors.verticalCenter: parent.verticalCenter
123 anchors.verticalCenter: parent.verticalCenter;
124 anchors.left: parent.left;
125 anchors.right: parent.right;
133 anchors.horizontalCenter: parent.horizontalCenter;
139 anchors.horizontalCenter: parent.horizontalCenter;
140 //horizontalAlignment: Text.AlignHCenter
141 text: "Last Score:\t" + lastScore + "\nHighscore:\t" + highScores.topScore;
146 source: "content/pics/background.png"
147 fillMode: Image.PreserveAspectCrop
149 anchors.left: parent.left
150 anchors.right: parent.right
151 anchors.top: parent.top
152 anchors.bottom: toolbar.top
157 border.color: "white"
159 anchors.horizontalCenter: parent.horizontalCenter
160 y: (screen.height - 32 - height)/2;
161 width: numColumnsAvailable * gridSize + 2*margin
162 height: numRowsAvailable * gridSize + 2*margin
172 if (screen.state == "") {
173 Logic.startNewGame();
176 if (direction == 0 || direction == 2)
177 Logic.scheduleDirection((mouseX > (head.x + head.width/2)) ? 1 : 3);
179 Logic.scheduleDirection((mouseY > (head.y + head.height/2)) ? 2 : 0);
189 Behavior on opacity { NumberAnimation { duration: 200 } }
192 border.color: "#221edd"
197 anchors.horizontalCenter: parent.horizontalCenter
198 anchors.verticalCenter: parent.verticalCenter
199 anchors.verticalCenterOffset: 40
202 id: progressIndicator
211 color: activePalette.window
212 height: 32; width: parent.width
213 anchors.bottom: screen.bottom
216 id: btnA; text: "New Game"; onClicked: Logic.startNewGame();
217 anchors.left: parent.left; anchors.leftMargin: 3
218 anchors.verticalCenter: parent.verticalCenter
223 anchors { left: btnA.right; leftMargin: 3; verticalCenter: parent.verticalCenter }
224 onClicked: Qt.quit();
228 color: activePalette.text
229 text: "Score: " + score; font.bold: true
230 anchors.right: parent.right; anchors.rightMargin: 3
231 anchors.verticalCenter: parent.verticalCenter
236 Keys.onSpacePressed: Logic.startNewGame();
237 Keys.onLeftPressed: if (state == "starting" || direction != 1) Logic.scheduleDirection(3);
238 Keys.onRightPressed: if (state == "starting" || direction != 3) Logic.scheduleDirection(1);
239 Keys.onUpPressed: if (state == "starting" || direction != 2) Logic.scheduleDirection(0);
240 Keys.onDownPressed: if (state == "starting" || direction != 0) Logic.scheduleDirection(2);
245 PropertyChanges {target: progressIndicator; width: 200}
246 PropertyChanges {target: title; opacity: 0}
247 PropertyChanges {target: progressBar; opacity: 1}
251 PropertyChanges {target: progressIndicator; width: 200}
252 PropertyChanges {target: title; opacity: 0}
253 PropertyChanges {target: skull; row: 0; column: 0; }
254 PropertyChanges {target: skull; spawned: 1}
262 NumberAnimation { target: progressIndicator; property: "width"; duration: 1000 }
263 NumberAnimation { property: "opacity"; duration: 200 }
267 NumberAnimation { target: progressIndicator; property: "width"; duration: 1000 }
268 NumberAnimation { property: "opacity"; duration: 200 }