190Now that we have all the game components, we can add the game logic that
191dictates how a player interacts with the blocks and plays the game
192until it is won or lost.
193
194To do this, we have added the following functions to \c samegame.js:
195
196\list
197\li \c{handleClick(x,y)}
198\li \c{floodFill(xIdx,yIdx,type)}
199\li \c{shuffleDown()}
200\li \c{victoryCheck()}
201\li \c{floodMoveCheck(xIdx, yIdx, type)}
202\endlist
203
204As 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.
205
206\section3 Enabling Mouse Click Interaction
207
208To 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:
212The \c gameCanvas item is the exact size of the board, and has a \c score property and a \l MouseArea to handle mouse clicks.
213The blocks are now created as its children, and its dimensions are used to determine the board size so that
214the application scales to the available screen size.
215Since 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.
216Note that it can still be accessed from the script.
217
218When 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:
222Note 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.
223
224\section3 Updating the Score
225
226When 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:
230This updates the \c gameCanvas.score value and displays a "Game Over" dialog if the game is finished.
231
232The 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:
240We 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.
241
242
243\section3 A Dash of Color
244
245It'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:
332To fully understand this you should read \l {Using the Qt Quick Particle System}, but it's important to note that \c emitRate is set
333to zero so that particles are not emitted normally.
334Also, 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
339Now the game is beautifully animated, with subtle (or not-so-subtle) animations added for all of the
340player's actions. The end result is shown below, with a different set of images to demonstrate basic theming:
341
342\image declarative-adv-tutorial4.gif
343
344The 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.
345
346\section2 Keeping a High Scores Table
347
348Another feature we might want to add to the game is a method of storing and retrieving high scores.
349
350To 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.
351This requires a few changes to \c Dialog.qml. In addition to a \c Text type, it now has a
352\c TextInput child item for receiving keyboard text input:
373When 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.
374
375The \c nameInputDialog is activated in the \c victoryCheck() function in \c samegame.js:
389First we call \c sendHighScore() (explained in the section below) if it is possible to send the high scores to an online database.
390
391Then, 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.
392
393This 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.
394
395\section3 Storing High Scores Online
396
397You'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
398simple: the high score data is posted to a php script running on a server somewhere, and that server then stores it and
399displays it to visitors. You could also request an XML or QML file from that same server, which contains and displays the scores,
400but that's beyond the scope of this tutorial. The php script we use here is available in the \c examples directory.
401
402If the player entered their name we can send the data to the web service us
403
404If the player enters a name, we send the data to the service using this code in \c samegame.js:
408The \l 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
409or 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
410score 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
411way as you did with the blocks.
412
413An alternate way to access and submit web-based data would be to use QML types designed for this purpose. XmlListModel
414makes it very easy to fetch and display XML based data such as RSS in a QML application.
415
416
417\section2 That's It!
418
419By following this tutorial you've seen how you can write a fully functional application in QML:
420
421\list
422\li Build your application with \l {Qt Quick QML Types}{QML types}