PySide QML Advanced Tutorial 1: Creating the Game Canvas and Blocks

This tutorial walks step-by-step through the creation of a full application using QML. It assumes that you already know the basics of QML (for example, from reading the QML Basic Tutorial. In this tutorial we write a game, Same Game, based on the Same Game application included in the declarative demos directory, which looks like this:

We will cover concepts for producing a fully functioning application, including JavaScript integration, using QML States and Behaviors to manage components and enhance your interface, and storing persistent application data.

An understanding of JavaScript is helpful to understand parts of this tutorial, but if you don’t know JavaScript you can still get a feel for how you can integrate backend logic to create and control QML elements.

Creating the application screen

The first step is to create the basic QML items in your application.

To begin with, we create our Same Game application with a main screen like this:

This is defined by the main application file, samegame.qml, which looks like this:

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.     id: screen
  5.  
  6.     width: 490; height: 720
  7.  
  8.     SystemPalette { id: activePalette }
  9.  
  10.     Item {
  11.         width: parent.width
  12.         anchors { top: parent.top; bottom: toolBar.top }
  13.  
  14.         Image {
  15.             id: background
  16.             anchors.fill: parent
  17.             source: "../shared/pics/background.jpg"
  18.             fillMode: Image.PreserveAspectCrop
  19.         }
  20.     }
  21.  
  22.     Rectangle {
  23.         id: toolBar
  24.         width: parent.width; height: 30
  25.         color: activePalette.window
  26.         anchors.bottom: screen.bottom
  27.  
  28.         Button {
  29.             anchors { left: parent.left; verticalCenter: parent.verticalCenter }
  30.             text: "New Game"
  31.             onClicked: console.log("This doesn't do anything yet...")
  32.         }
  33.  
  34.         Text {
  35.             id: score
  36.             anchors { right: parent.right; verticalCenter: parent.verticalCenter }
  37.             text: "Score: Who knows?"
  38.         }
  39.     }
  40. }

This gives you a basic game window that includes the main canvas for the blocks, a “New Game” button and a score display.

One item you may not recognize here is the SystemPalette item. This provides access to the Qt system palette and is used to give the button a more native look-and-feel.

Notice the anchors for the Item, Button and Text elements are set using group notation for readability.

Adding Button and Block components

The Button item in the code above is defined in a separate component file named Button.qml. To create a functional button, we use the QML elements Text and MouseArea inside a Rectangle. Here is the Button.qml code:

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.     id: container
  5.  
  6.     property string text: "Button"
  7.  
  8.     signal clicked
  9.  
  10.     width: buttonLabel.width + 20; height: buttonLabel.height + 5
  11.     border { width: 1; color: Qt.darker(activePalette.button) }
  12.     smooth: true
  13.     radius: 8
  14.  
  15.     // color the button with a gradient
  16.     gradient: Gradient {
  17.         GradientStop {
  18.             position: 0.0
  19.             color: {
  20.                 if (mouseArea.pressed)
  21.                     return activePalette.dark
  22.                 else
  23.                     return activePalette.light
  24.             }
  25.         }
  26.         GradientStop { position: 1.0; color: activePalette.button }
  27.     }
  28.  
  29.     MouseArea {
  30.         id: mouseArea
  31.         anchors.fill: parent
  32.         onClicked: container.clicked();
  33.     }
  34.  
  35.     Text {
  36.         id: buttonLabel
  37.         anchors.centerIn: container
  38.         color: activePalette.buttonText
  39.         text: container.text
  40.     }
  41. }

This essentially defines a rectangle that contains text and can be clicked. The MouseArea has an onClicked() handler that is implemented to emit the clicked() signal of the container when the area is clicked.

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 Block.qml file:

  1. import QtQuick 1.0
  2.  
  3. Item {
  4.     id: block
  5.  
  6.     Image {
  7.         id: img
  8.         anchors.fill: parent
  9.         source: "../shared/pics/redStone.png"
  10.     }
  11. }

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.

We have set the image to be the size of its parent Item using anchors.fill: parent. 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.

Notice the relative path for the Image element’s source property. This path is relative to the location of the file that contains the Image element. Alternatively, you could set the Image source to an absolute file path or a URL that contains an image.

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.

Categories: