Using Qt to Develop for Embedded Linux

Qt/Embedded extends the ease and speed of Qt to embedded application development.
Canvas and CanvasView

You now need a class to set up the program. Its constructor will create an instance of the program, as used in the main method that appears above. It will inherit from QMainWindow and will create menus or toolbars and possibly a status bar. It also needs to create a QCanvas and a QCanvasView. All of this is shown in Listing 1.

Listing 1. Creating a QCanvas and QCanvasView

This class will also contain some other methods to be used in your program. In Snake, this class contains methods to display a title screen, start a new game, update the score, change levels, clear the screen, end the game and handle keyboard interaction (key events). These methods will be specific to your program, so I have not included the code here.

Sprites

Now that you have a QCanvas, you can start placing QCanvasItems on it. There are many types of QCanvasItems, and to create a graphical item, we will use QCanvasSprite. See the Qt documentation for information on the other types of QCanvasItems. In the Snake game, the target mouse is a QCanvasSprite created from an image. The mouse is defined in its own class and inherits from QCanvasSprite. The following code shows the constructor for the mouse in a class called Target:

Target::Target(QCanvas* canvas)
   : QCanvasSprite(0, canvas)
       //inherits from QCanvasSprite
{
  mouse = new
QCanvasPixmapArray
  setSequence(mouse);
  newTarget();
}

The first thing this code does is load the image into a pixmap array called ``mouse''. If you had several images and wanted to animate your sprite, there is a readPixmaps() function to load all them into the array. You can then use the setSequence() function in QCanvasSprite to tell your sprite to use the images just loaded into the array. This constructor also calls a function newTarget(). This simply generates random integer x and y values at which the new target can be placed. It then calls the move (int x, int y) function in QCanvasSprite to set its position and show() to make it visible. The wall is also created in the same way.

Keyboard Events

Now you can make your program interactive. The Qt class keyEvent collects information about key events. The event handlers keyPressEvent and keyReleaseEvent receive this information. To use these, you will need to implement them as a function in your program. A typical implementation of keyPressEvent would be to use a switch statement to call functions when the user presses certain keys.

In the game, the snake changes direction when one of the arrow keys is pressed. This was implemented with a switch statement that calls a function move(), with the direction pressed as its argument. Here is the implementation of the keyPressEvent() function. It is quite simple because it calls a function in the Snake class that remembers the last key pressed and calls another function, moveSnake(), to determine which direction the snake should move in. To enact this, add these lines to the interface.cpp file:

void SnakeGame::keyPressEvent(QKeyEvent* event)
{
       int newkey = event->key();
       snake->go(newkey)
}

To the snake.cpp file, add:

void Snake::moveSnake()
{
   switch (last)
   {
      case Key_Up: move(up); break;
      case Key_Left: move(left);  break;
      case Key_Right: move(right); break;
      case Key_Down: move(down);  break;
    }
    detectCrash();
}
If the user presses the Up key, the program calls move(up). The move function executes, and the snake moves in the direction of the key pressed: in this case, up. The code in the move() function will be specific to the particular game created.

The actual movement of the snake is quite complex because the snake's body is a collection of little images. To move, the end image is put at the front, and the head and tail pieces are moved. In other situations, such as pressing a key to shoot a bullet, the setVelocity() function in Qt could be used to set a constant speed at which the bullet will move.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Good Article

Luis Abarca's picture

Hey nice article !!

Muy buen articulo, me despejaste varias dudas que tenia al respecto.

Saludos.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState