PySide QML Advanced Tutorial 2: Populating the Game Canvas

Generating the blocks in JavaScript

Now that we’ve written some basic elements, let’s start writing the game.

The first task is to generate the game blocks. Each time the New Game button is clicked, the game canvas is populated with a new, random set of blocks. Since we need to dynamically generate new blocks for each new game, we cannot use Repeater to define the blocks. Instead, we will create the blocks in JavaScript.

Here is the JavaScript code for generating the blocks, contained in a new file, samegame.js. The code is explained below.

  1. var blockSize = 40;
  2. var maxColumn = 10;
  3. var maxRow = 15;
  4. var maxIndex = maxColumn * maxRow;
  5. var board = new Array(maxIndex);
  6. var component;
  7.  
  8. //Index function used instead of a 2D array
  9. function index(column, row) {
  10.     return column + (row * maxColumn);
  11. }
  12.  
  13. function startNewGame() {
  14.     //Delete blocks from previous game
  15.     for (var i = 0; i < maxIndex; i++) {
  16.         if (board[i] != null)
  17.             board[i].destroy();
  18.     }
  19.  
  20.     //Calculate board size
  21.     maxColumn = Math.floor(background.width / blockSize);
  22.     maxRow = Math.floor(background.height / blockSize);
  23.     maxIndex = maxRow * maxColumn;
  24.  
  25.     //Initialize Board
  26.     board = new Array(maxIndex);
  27.     for (var column = 0; column < maxColumn; column++) {
  28.         for (var row = 0; row < maxRow; row++) {
  29.             board[index(column, row)] = null;
  30.             createBlock(column, row);
  31.         }
  32.     }
  33. }
  34.  
  35. function createBlock(column, row) {
  36.     if (component == null)
  37.         component = Qt.createComponent("Block.qml");
  38.  
  39.     // Note that if Block.qml was not a local file, component.status would be
  40.     // Loading and we should wait for the component's statusChanged() signal to
  41.     // know when the file is downloaded and ready before calling createObject().
  42.     if (component.status == Component.Ready) {
  43.         var dynamicObject = component.createObject(background);
  44.         if (dynamicObject == null) {
  45.             console.log("error creating block");
  46.             console.log(component.errorString());
  47.             return false;
  48.         }
  49.         dynamicObject.x = column * blockSize;
  50.         dynamicObject.y = row * blockSize;
  51.         dynamicObject.width = blockSize;
  52.         dynamicObject.height = blockSize;
  53.         board[index(column, row)] = dynamicObject;
  54.     } else {
  55.         console.log("error loading block component");
  56.         console.log(component.errorString());
  57.         return false;
  58.     }
  59.     return true;
  60. }

The startNewGame() function deletes the blocks created in the previous game and calculates the number of rows and columns of blocks required to fill the game window for the new game. Then, it creates an array to store all the game blocks, and calls createBlock() to create enough blocks to fill the game window.

The createBlock() function creates a block from the Block.qml file and moves the new block to its position on the game canvas. This involves several steps:

  • Qt.createComponent() is called to generate an element from Block.qml. If the component is ready, we can call createObject() to create an instance of the Block item.
  • If createObject() returned null (i.e. if there was an error while loading the object), print the error information.
  • Place the block in its position on the board and set its width and height. Also, store it in the blocks array for future reference.
  • Finally, print error information to the console if the component could not be loaded for some reason (for example, if the file is missing).
    Connecting JavaScript components to QML¶

Now we need to call the JavaScript code in samegame.js from our QML files. To do this, we add this line to samegame.qml which imports the JavaScript file as a module:

  1. import "samegame.js" as SameGame

This allows us to refer to any functions within samegame.js using “SameGame” as a prefix: for example, SameGame.startNewGame() or SameGame.createBlock(). This means we can now connect the New Game button’s onClicked handler to the startNewGame() function, like this:

  1.         Button {
  2.             anchors { left: parent.left; verticalCenter: parent.verticalCenter }
  3.             text: "New Game"
  4.             onClicked: SameGame.startNewGame()
  5.         }

So, when you click the New Game button, startNewGame() is called and generates a field of blocks, like this:

Now, we have a screen of blocks, and we can begin to add the game mechanics.

Categories: