File: declarative-tutorials-samegame-samegame1.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (190 lines) | stat: -rw-r--r-- 13,261 bytes parent folder | download
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- advtutorial.qdoc -->
  <title>Qt 4.8: QML Advanced Tutorial 1 - Creating the Game Canvas and Blocks</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>QML Examples &amp; Demos</li>
<li>QML Advanced Tutorial 1 - Creating the Game Canvas and Blocks</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
  <link rel="prev" href="qml-advtutorial.html" />
  <link rel="next" href="declarative-tutorials-samegame-samegame2.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qml-advtutorial.html">QML Advanced Tutorial</a>
<a class="nextPage" href="declarative-tutorials-samegame-samegame2.html">QML Advanced Tutorial 2 - Populating the Game Canvas</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level2"><a href="#creating-the-application-screen">Creating the application screen</a></li>
<li class="level2"><a href="#adding-and-components">Adding <tt>Button</tt> and <tt>Block</tt> components</a></li>
</ul>
</div>
<h1 class="title">QML Advanced Tutorial 1 - Creating the Game Canvas and Blocks</h1>
<span class="subtitle"></span>
<!-- $$$declarative/tutorials/samegame/samegame1-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="declarative-tutorials-samegame-samegame1-block-qml.html">declarative/tutorials/samegame/samegame1/Block.qml</a></li>
<li><a href="declarative-tutorials-samegame-samegame1-button-qml.html">declarative/tutorials/samegame/samegame1/Button.qml</a></li>
<li><a href="declarative-tutorials-samegame-samegame1-samegame-qml.html">declarative/tutorials/samegame/samegame1/samegame.qml</a></li>
<li><a href="declarative-tutorials-samegame-samegame1-samegame1-qmlproject.html">declarative/tutorials/samegame/samegame1/samegame1.qmlproject</a></li>
</ul>
<a name="creating-the-application-screen"></a>
<h3>Creating the application screen</h3>
<p>The first step is to create the basic QML items in your application.</p>
<p>To begin with, we create our Same Game application with a main screen like this:</p>
<p class="centerAlign"><img src="images/declarative-adv-tutorial1.png" alt="" /></p><p>This is defined by the main application file, <tt>samegame.qml</tt>, which looks like this:</p>
<pre class="qml"> import QtQuick 1.0

 <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
     <span class="name">id</span>: <span class="name">screen</span>

     <span class="name">width</span>: <span class="number">490</span>; <span class="name">height</span>: <span class="number">720</span>

     <span class="type"><a href="qml-systempalette.html">SystemPalette</a></span> { <span class="name">id</span>: <span class="name">activePalette</span> }

     <span class="type"><a href="qml-item.html">Item</a></span> {
         <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>
         <span class="type">anchors</span> { <span class="name">top</span>: <span class="name">parent</span>.<span class="name">top</span>; <span class="name">bottom</span>: <span class="name">toolBar</span>.<span class="name">top</span> }

         <span class="type"><a href="qml-image.html">Image</a></span> {
             <span class="name">id</span>: <span class="name">background</span>
             <span class="name">anchors</span>.fill: <span class="name">parent</span>
             <span class="name">source</span>: <span class="string">&quot;../shared/pics/background.jpg&quot;</span>
             <span class="name">fillMode</span>: <span class="name">Image</span>.<span class="name">PreserveAspectCrop</span>
         }
     }

     <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
         <span class="name">id</span>: <span class="name">toolBar</span>
         <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="number">30</span>
         <span class="name">color</span>: <span class="name">activePalette</span>.<span class="name">window</span>
         <span class="name">anchors</span>.bottom: <span class="name">screen</span>.<span class="name">bottom</span>

         <span class="type">Button</span> {
             <span class="type">anchors</span> { <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">verticalCenter</span>: <span class="name">parent</span>.<span class="name">verticalCenter</span> }
             <span class="name">text</span>: <span class="string">&quot;New Game&quot;</span>
             <span class="name">onClicked</span>: <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;This doesn't do anything yet...&quot;</span>)
         }

         <span class="type"><a href="qml-text.html">Text</a></span> {
             <span class="name">id</span>: <span class="name">score</span>
             <span class="type">anchors</span> { <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span>; <span class="name">verticalCenter</span>: <span class="name">parent</span>.<span class="name">verticalCenter</span> }
             <span class="name">text</span>: <span class="string">&quot;Score: Who knows?&quot;</span>
         }
     }
 }</pre>
<p>This gives you a basic game window that includes the main canvas for the blocks, a &quot;New Game&quot; button and a score display.</p>
<p>One item you may not recognize here is the <a href="qml-systempalette.html">SystemPalette</a> item. This provides access to the Qt system palette and is used to give the button a more native look-and-feel.</p>
<p>Notice the anchors for the <tt>Item</tt>, <tt>Button</tt> and <tt>Text</tt> elements are set using <a href="qdeclarativeintroduction.html#dot-properties">group notation</a> for readability.</p>
<a name="adding-and-components"></a>
<h3>Adding <tt>Button</tt> and <tt>Block</tt> components</h3>
<p>The <tt>Button</tt> item in the code above is defined in a separate component file named <tt>Button.qml</tt>. To create a functional button, we use the QML elements <a href="qml-text.html">Text</a> and <a href="qml-mousearea.html">MouseArea</a> inside a <a href="qml-rectangle.html">Rectangle</a>. Here is the <tt>Button.qml</tt> code:</p>
<pre class="qml"> import QtQuick 1.0

 <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
     <span class="name">id</span>: <span class="name">container</span>

     property <span class="type">string</span> <span class="name">text</span>: <span class="string">&quot;Button&quot;</span>

     signal <span class="type">clicked</span>

     <span class="name">width</span>: <span class="name">buttonLabel</span>.<span class="name">width</span> <span class="operator">+</span> <span class="number">20</span>; <span class="name">height</span>: <span class="name">buttonLabel</span>.<span class="name">height</span> <span class="operator">+</span> <span class="number">5</span>
     <span class="type">border</span> { <span class="name">width</span>: <span class="number">1</span>; <span class="name">color</span>: <span class="name">Qt</span>.<span class="name">darker</span>(<span class="name">activePalette</span>.<span class="name">button</span>) }
     <span class="name">smooth</span>: <span class="number">true</span>
     <span class="name">radius</span>: <span class="number">8</span>

     <span class="comment">// color the button with a gradient</span>
     <span class="name">gradient</span>: <span class="name">Gradient</span> {
         <span class="type"><a href="qml-gradientstop.html">GradientStop</a></span> {
             <span class="name">position</span>: <span class="number">0.0</span>
             <span class="name">color</span>: {
                 <span class="keyword">if</span> (<span class="name">mouseArea</span>.<span class="name">pressed</span>)
                     <span class="keyword">return</span> <span class="name">activePalette</span>.<span class="name">dark</span>
                 else
                     <span class="keyword">return</span> <span class="name">activePalette</span>.<span class="name">light</span>
             }
         }
         <span class="type"><a href="qml-gradientstop.html">GradientStop</a></span> { <span class="name">position</span>: <span class="number">1.0</span>; <span class="name">color</span>: <span class="name">activePalette</span>.<span class="name">button</span> }
     }

     <span class="type"><a href="qml-mousearea.html">MouseArea</a></span> {
         <span class="name">id</span>: <span class="name">mouseArea</span>
         <span class="name">anchors</span>.fill: <span class="name">parent</span>
         <span class="name">onClicked</span>: <span class="name">container</span>.<span class="name">clicked</span>();
     }

     <span class="type"><a href="qml-text.html">Text</a></span> {
         <span class="name">id</span>: <span class="name">buttonLabel</span>
         <span class="name">anchors</span>.centerIn: <span class="name">container</span>
         <span class="name">color</span>: <span class="name">activePalette</span>.<span class="name">buttonText</span>
         <span class="name">text</span>: <span class="name">container</span>.<span class="name">text</span>
     }
 }</pre>
<p>This essentially defines a rectangle that contains text and can be clicked. The <a href="qml-mousearea.html">MouseArea</a> has an <tt>onClicked()</tt> handler that is implemented to emit the <tt>clicked()</tt> signal of the <tt>container</tt> when the area is clicked.</p>
<p>In Same Game, the screen is filled with small blocks when the game begins. Each block is just an item that contains an image. The block code is defined in a separate <tt>Block.qml</tt> file:</p>
<pre class="qml"> import QtQuick 1.0

 <span class="type"><a href="qml-item.html">Item</a></span> {
     <span class="name">id</span>: <span class="name">block</span>

     <span class="type"><a href="qml-image.html">Image</a></span> {
         <span class="name">id</span>: <span class="name">img</span>
         <span class="name">anchors</span>.fill: <span class="name">parent</span>
         <span class="name">source</span>: <span class="string">&quot;../shared/pics/redStone.png&quot;</span>
     }
 }</pre>
<p>At the moment, the block doesn't do anything; it is just an image. As the tutorial progresses we will animate and give behaviors to the blocks. We have not added any code yet to create the blocks; we will do this in the next chapter.</p>
<p>We have set the image to be the size of its parent Item using <tt>anchors.fill: parent</tt>. This means that when we dynamically create and resize the block items later on in the tutorial, the image will be scaled automatically to the correct size.</p>
<p>Notice the relative path for the Image element's <tt>source</tt> property. This path is relative to the location of the file that contains the <a href="qml-image.html">Image</a> element. Alternatively, you could set the Image source to an absolute file path or a URL that contains an image.</p>
<p>You should be familiar with the code so far. We have just created some basic elements to get started. Next, we will populate the game canvas with some blocks.</p>
</div>
<!-- @@@declarative/tutorials/samegame/samegame1 -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qml-advtutorial.html">QML Advanced Tutorial</a>
<a class="nextPage" href="declarative-tutorials-samegame-samegame2.html">QML Advanced Tutorial 2 - Populating the Game Canvas</a>
</p>
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>