193Now that we have all the game components, we can add the game logic that
194dictates how a player interacts with the blocks and plays the game
195until it is won or lost.
196
197To do this, we have added the following functions to \c samegame.js:
198
199\list
200\li \c{handleClick(x,y)}
201\li \c{floodFill(xIdx,yIdx,type)}
202\li \c{shuffleDown()}
203\li \c{victoryCheck()}
204\li \c{floodMoveCheck(xIdx, yIdx, type)}
205\endlist
206
207As this is a tutorial about QML, not game design, we will only discuss \c handleClick() and \c victoryCheck() below since they interface directly with the QML types. Note that although the game logic here is written in JavaScript, it could have been written in C++ and then exposed to QML.
208
209\section3 Enabling Mouse Click Interaction
210
211To make it easier for the JavaScript code to interface with the QML types, we have added an Item called \c gameCanvas to \c samegame.qml. It replaces the background as the item which contains the blocks. It also accepts mouse input from the user. Here is the item code:
215The \c gameCanvas item is the exact size of the board, and has a \c score property and a \l MouseArea to handle mouse clicks.
216The blocks are now created as its children, and its dimensions are used to determine the board size so that
217the application scales to the available screen size.
218Since its size is bound to a multiple of \c blockSize, \c blockSize was moved out of \c samegame.js and into \c samegame.qml as a QML property.
219Note that it can still be accessed from the script.
220
221When clicked, the \l MouseArea calls \c{handleClick()} in \c samegame.js, which determines whether the player's click should cause any blocks to be removed, and updates \c gameCanvas.score with the current score if necessary. Here is the \c handleClick() function:
225Note that if \c score was a global variable in the \c{samegame.js} file you would not be able to bind to it. You can only bind to QML properties.
226
227\section3 Updating the Score
228
229When the player clicks a block and triggers \c handleClick(), \c handleClick() also calls \c victoryCheck() to update the score and to check whether the player has completed the game. Here is the \c victoryCheck() code:
233This updates the \c gameCanvas.score value and displays a "Game Over" dialog if the game is finished.
234
235The Game Over dialog is created using a \c Dialog type that is defined in \c Dialog.qml. Here is the \c Dialog.qml code. Notice how it is designed to be usable imperatively from the script file, via the functions and signals:
243We give the dialog a \l {Item::z}{z} value of 100 to ensure it is displayed on top of our other components. The default \c z value for an item is 0.
244
245
246\section3 A Dash of Color
247
248It's not much fun to play Same Game if all the blocks are the same color, so we've modified the \c createBlock() function in \c samegame.js to randomly create a different type of block (for either red, green or blue) each time it is called. \c Block.qml has also changed so that each block contains a different image depending on its type:
336To fully understand this you should read \l {Using the Qt Quick Particle System}, but it's important to note that \c emitRate is set
337to zero so that particles are not emitted normally.
338Also, we extend the \c dying State, which creates a burst of particles by calling the \c burst() method on the particles type. The code for the states now look
343Now the game is beautifully animated, with subtle (or not-so-subtle) animations added for all of the
344player's actions. The end result is shown below, with a different set of images to demonstrate basic theming:
345
346\image declarative-adv-tutorial4.gif
347 {Game demonstrating smooth block animations}
348
349The theme change here is produced simply by replacing the block images. This can be done at runtime by changing the \l Image \c source property, so for a further challenge, you could add a button that toggles between themes with different images.
350
351\section2 Keeping a High Scores Table
352
353Another feature we might want to add to the game is a method of storing and retrieving high scores.
354
355To do this, we will show a dialog when the game is over to request the player's name and add it to a High Scores table.
356This requires a few changes to \c Dialog.qml. In addition to a \c Text type, it now has a
357\c TextInput child item for receiving keyboard text input:
378When the dialog emits the \c closed signal, we call the new \c saveHighScore() function in \c samegame.js, which stores the high score locally in an SQL database and also send the score to an online database if possible.
379
380The \c nameInputDialog is activated in the \c victoryCheck() function in \c samegame.js:
394First we call \c sendHighScore() (explained in the section below) if it is possible to send the high scores to an online database.
395
396Then, we use the \l{QtQuick.LocalStorage}{Local Storage API} to maintain a persistent SQL database unique to this application. We create an offline storage database for the high scores using \c openDatabaseSync() and prepare the data and SQL query that we want to use to save it. The offline storage API uses SQL queries for data manipulation and retrieval, and in the \c db.transaction() call we use three SQL queries to initialize the database (if necessary), and then add to and retrieve high scores. To use the returned data, we turn it into a string with one line per row returned, and show a dialog containing that string.
397
398This is one way of storing and displaying high scores locally, but certainly not the only way. A more complex alternative would be to create a high score dialog component, and pass it the results for processing and display (instead of reusing the \c Dialog). This would allow a more themeable dialog that could better present the high scores. If your QML is the UI for a C++ application, you could also have passed the score to a C++ function to store it locally in a variety of ways, including a simple format without SQL or in another SQL database.
399
400\section3 Storing High Scores Online
401
402You've seen how you can store high scores locally, but it is also easy to integrate a web-enabled high score storage into your QML application. The implementation we've done her is very
403simple: the high score data is posted to a php script running on a server somewhere, and that server then stores it and
404displays it to visitors. You could also request an XML or QML file from that same server, which contains and displays the scores,
405but that's beyond the scope of this tutorial. The php script we use here is available in the \c examples directory.
406
407If the player entered their name we can send the data to the web service us
408
409If the player enters a name, we send the data to the service using this code in \c samegame.js:
413The \l{The XMLHttpRequest JavaScript Object}{XMLHttpRequest} in this code is the same as the \c XMLHttpRequest() as you'll find in standard browser JavaScript, and can be used in the same way to dynamically get XML
414or QML from the web service to display the high scores. We don't worry about the response in this case - we just post the high
415score data to the web server. If it had returned a QML file (or a URL to a QML file) you could instantiate it in much the same
416way as you did with the blocks.
417
418An alternate way to access and submit web-based data would be to use QML types designed for this purpose. XmlListModel
419makes it very easy to fetch and display XML based data such as RSS in a QML application.
420
421
422\section2 That's It!
423
424By following this tutorial you've seen how you can write a fully functional application in QML:
425
426\list
427\li Build your application with \l {Qt Quick QML Types}{QML types}