demos/declarative/snake/snake.qml
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 import Qt 4.7
       
    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 
       
    51     property int gridSize : 34
       
    52     property int margin: 4
       
    53     property int numRowsAvailable: Math.floor((height-32-2*margin)/gridSize)
       
    54     property int numColumnsAvailable: Math.floor((width-2*margin)/gridSize)
       
    55 
       
    56     property int lastScore : 0
       
    57 
       
    58     property int score: 0;
       
    59     property int heartbeatInterval: 200
       
    60     property int halfbeatInterval: 160
       
    61 
       
    62     width: 480
       
    63     height: 750
       
    64 
       
    65     property int direction
       
    66     property int headDirection
       
    67 
       
    68     property variant head;
       
    69 
       
    70     Content.HighScoreModel {
       
    71         id: highScores
       
    72         game: "Snake"
       
    73     }
       
    74 
       
    75     Timer {
       
    76         id: heartbeat;
       
    77         interval: heartbeatInterval;
       
    78         repeat: true
       
    79         onTriggered: { Logic.move() }
       
    80     }
       
    81     Timer {
       
    82         id: halfbeat;
       
    83         interval: halfbeatInterval;
       
    84         repeat: true
       
    85         running: heartbeat.running
       
    86         onTriggered: { Logic.moveSkull() }
       
    87     }
       
    88     Timer {
       
    89 
       
    90         interval: 700;
       
    91         onTriggered: { Logic.startNewGame(); }
       
    92     }
       
    93 
       
    94     Timer {
       
    95         id: startHeartbeatTimer;
       
    96         interval: 1000 ;
       
    97     }
       
    98 
       
    99 
       
   100     Image {
       
   101         Image {
       
   102             id: title
       
   103             source: "content/pics/snake.jpg"
       
   104             fillMode: Image.PreserveAspectCrop
       
   105             anchors.fill: parent
       
   106             anchors.horizontalCenter: parent.horizontalCenter
       
   107             anchors.verticalCenter: parent.verticalCenter
       
   108             Behavior on opacity { NumberAnimation { duration: 500 } }
       
   109 
       
   110             Text {
       
   111                 color: "white"
       
   112                 font.pointSize: 24
       
   113                 horizontalAlignment: Text.AlignHCenter
       
   114                 text: "Last Score:\t" + lastScore + "\nHighscore:\t" + highScores.topScore;
       
   115                 anchors.horizontalCenter: parent.horizontalCenter
       
   116                 anchors.bottom: parent.bottom
       
   117                 anchors.bottomMargin: gridSize
       
   118             }
       
   119         }
       
   120 
       
   121         source: "content/pics/background.png"
       
   122         fillMode: Image.PreserveAspectCrop
       
   123 
       
   124         anchors.left: parent.left
       
   125         anchors.right: parent.right
       
   126         anchors.top: parent.top
       
   127         anchors.bottom: toolbar.top
       
   128 
       
   129         Rectangle {
       
   130             id: playfield
       
   131             border.width: 1
       
   132             border.color: "white"
       
   133             color: "transparent"
       
   134             anchors.horizontalCenter: parent.horizontalCenter
       
   135             y: (screen.height - 32 - height)/2;
       
   136             width: numColumnsAvailable * gridSize + 2*margin
       
   137             height: numRowsAvailable * gridSize + 2*margin
       
   138 
       
   139 
       
   140             Content.Skull {
       
   141                 id: skull
       
   142             }
       
   143 
       
   144             MouseArea {
       
   145                 anchors.fill: parent
       
   146                 onPressed: {
       
   147                     if (!head || !heartbeat.running) {
       
   148                         Logic.startNewGame();
       
   149                         return;
       
   150                     }
       
   151                     if (direction == 0 || direction == 2)
       
   152                         Logic.scheduleDirection((mouseX > (head.x + head.width/2)) ? 1 : 3);
       
   153                     else
       
   154                         Logic.scheduleDirection((mouseY > (head.y + head.height/2)) ? 2 : 0);
       
   155                 }
       
   156             }
       
   157         }
       
   158 
       
   159     }
       
   160 
       
   161     Rectangle {
       
   162         id: progressBar
       
   163         opacity: 0
       
   164         Behavior on opacity { NumberAnimation { duration: 200 } }
       
   165         color: "transparent"
       
   166         border.width: 2
       
   167         border.color: "#221edd"
       
   168         x: 50
       
   169         y: 50
       
   170         width: 200
       
   171         height: 30
       
   172         anchors.horizontalCenter: parent.horizontalCenter
       
   173         anchors.verticalCenter: parent.verticalCenter
       
   174         anchors.verticalCenterOffset: 40
       
   175 
       
   176         Rectangle {
       
   177             id: progressIndicator
       
   178             color: "#221edd";
       
   179             width: 0;
       
   180             Behavior on width { NumberAnimation { duration: startHeartbeatTimer.running ? 1000 : 0}}
       
   181             height: 30;
       
   182         }
       
   183     }
       
   184 
       
   185     Rectangle {
       
   186         id: toolbar
       
   187         color: activePalette.window
       
   188         height: 32; width: parent.width
       
   189         anchors.bottom: screen.bottom
       
   190 
       
   191         Content.Button {
       
   192             id: btnA; text: "New Game"; onClicked: Logic.startNewGame();
       
   193             anchors.left: parent.left; anchors.leftMargin: 3
       
   194             anchors.verticalCenter: parent.verticalCenter
       
   195         }
       
   196 
       
   197         Text {
       
   198             color: activePalette.text
       
   199             text: "Score: " + score; font.bold: true
       
   200             anchors.right: parent.right; anchors.rightMargin: 3
       
   201             anchors.verticalCenter: parent.verticalCenter
       
   202         }
       
   203     }
       
   204 
       
   205     focus: true
       
   206     Keys.onSpacePressed: Logic.startNewGame();
       
   207     Keys.onLeftPressed: if (state == "starting" || direction != 1) Logic.scheduleDirection(3);
       
   208     Keys.onRightPressed: if (state == "starting" || direction != 3) Logic.scheduleDirection(1);
       
   209     Keys.onUpPressed: if (state == "starting" || direction != 2) Logic.scheduleDirection(0);
       
   210     Keys.onDownPressed: if (state == "starting" || direction != 0) Logic.scheduleDirection(2);
       
   211 
       
   212     states: [
       
   213         State {
       
   214             name: "starting"
       
   215             when: startHeartbeatTimer.running
       
   216             PropertyChanges {target: progressIndicator; width: 200}
       
   217             PropertyChanges {target: title; opacity: 0}
       
   218             PropertyChanges {target: progressBar; opacity: 1}
       
   219         },
       
   220         State {
       
   221             name: "running"
       
   222             when: (heartbeat.running && !startHeartbeatTimer.running)
       
   223             PropertyChanges {target: progressIndicator; width: 200}
       
   224             PropertyChanges {target: title; opacity: 0}
       
   225             PropertyChanges {target: skull; row: 0; column: 0; }
       
   226             PropertyChanges {target: skull; spawned: 1}
       
   227         }
       
   228     ]
       
   229 
       
   230 }