Chicken Wranglers

From Qt Wiki
Jump to navigation Jump to search

Chicken Wranglers – Showcase app

Listen to the INdT team in Brazil about how they used Qt to make a multiplayer and cross-device game using mobiles devices as the controllers.

YuoTube video

The original premise of this project was the development of an application to showcase how Qt and QML can support interoperability between different platforms and devices.

The idea started to shape as a cross-device game, where several players could use their mobiles as remote controls and a big screen as a main game board.

After several proposals being considered, the designers presented a quite charming and funny game concept: farmers trying to capture chickens on their hencoops, using laser walls to trap them.

The challenge ahead: how to implement this concept with a small team (with zero previous experience in QML and no gaming development experience as well) in a really tight schedule?

If successful, this could be the perfect scenario to prove how was the QML learning curve and how productive it could be.

Game Development

It was clear that some components would be required for assembling a multiplayer networked game, so the team decided to start implementing modules that could be used independently of how the idea would evolve. The demands at this stage were interoperability and client devices acting as remote controllers.

The Mobile as a Remote control

The first module to be developed was the game controller. For input the team decided to use the accelerometer sensor for moving the characters. Qt Mobility features a sensors module that provides access to the device accelerometers.

Character movements were quite simple (up, down, right, left, stop), being just a matter of capturing accelerometer data and converting to discrete directions. The main challenge here was to fine tune those values such that it wouldn’t be hard for the user to guide his character through the battlefield.

It required some degree of experimentation to achieve good thresholds for handling common commands. Some parameters were used to aid the calculations:

  • The minimum angle the device needs to be rotated to trigger a direction change.
  • A threshold between two directions in the same axis (e.g. up and stopped) so the controller does not keep oscillating between them.
  • Direction changing timeout, to check if the time spent in a given direction is not too short. This avoids undesired direction changes.

Connectivity

Before working on the connectivity itself, it was defined a communication protocol (messages and states to be exchanged between the devices), targeting to support both WLAN and bluetooth. Qt has great networking support, so it was relatively easy to implement connectivity via local network (therefore WLAN).

Qt Mobility 1.2 offers bluetooth classes for device management (discovering, pairing, etc) and message interchanging. It maps closely to the networking classes, enabling the initial WLAN code to be later translated to be used for bluetooth connections.

To support both WLAN and Bluetooth connectivity it a common abstraction was created for representing a network connection (QBluetoothSocket and QTcpSocket don’t share the same immediate parent class), thus signals diverge in type signature besides being quite similar. This allowed the application to switch the type of connectivity used at run time.

UI and Animation

One major concern was on the visual aspects of the game: how to make rich user interface screens, fancy transitions and animations to look beautiful and run smoothly.

That is where QML (Qt Modeling Language) came to the rescue. The team was formed by proficient programmers in C++/Qt/Linux but, as explained earlier, no previous experience with QML.

It turned out to be a pleasant surprise how easy and intuitive was to implement animations and effects while building the user interface in a declarative way. The possibilities of integration between C++ and the declarative language were seamless, enabling logic coded in native code to easily plug into Javascript based QML.

This way, we could keep the performance sensitive code paths in the native side (e.g. the chickens’ artificial intelligence, collision detection and connectivity, to name a few) and let only the visual implementation on the declarative side.

Making Smart Chickens

To make the chickens look like they are smart, it a module was created to add some intelligence to them. It basically changes the position of the chickens based on the proximity of the farmers or of hencoops. This logic enables the chickens to avoid wranglers and coops whenever possible and features a pseudo random criteria of avoidance, enriching the gaming experience.

The game elements (wranglers, hencoop, chickens, lasers) are logically represented on a matrix, in order to help the module to detect collisions and calculate the runaway logic.

Porting and Optimizations

The next step to fulfill the original vision of being truly cross-platform was a port to Symbian. The possibility of a port was only made possible starting with Qt Mobility 1.2 beta being made available (this is required to support bluetooth connectivity).

The first step was to change the buildsystem to couple with Symbian idiosyncrasies. While the initial buildsystem allowed to build different components as shared libraries (thus helping in debugging), to adapt to Symbian the application buildsystem was changed to build its submodules as a single project.

The app successfully launched in Symbian, but its memory footprint was above the maximum heapsize, as also the default stack size. These memory problems required some optimizations like using smaller image assets, installable character models (instead of using Qt resources for everything) and lazy loading of some screens.

Next, the screens got to be made more scalable (the game targeted the resolution of Nokia N900, which is bigger than Symbian devices like Nokia N8). The next step was to dynamically load assets depending on the environment and fine tune parameters animations (i.e. the feathers animation will use different number of feathers depending on the environment). This together with some reordering and refactoring of screen elements brought some benefits for CW in both environments, like faster app startup and better framerates. The end result was a reduction of memory usage from above 80MB to just 15MB of RAM.

Finally, much improved frame rates were achieved by simply using a QGLWidget thus unleashing OpenGL hardware acceleration for free. At end, we even got host mode to run successfully in the mobile (hosting up to 4 players at same time) with pretty good CPU usage (load around 65%) and framerates.

Conclusion

Qt technologies made possible to write from scratch a game for mobile devices in a short schedule with a small team, enabling quick implementation of designer’s mockups into real code.

It features support for everything needed to turn ideas into a running application: networking, bluetooth, accelerometer and graphics. The fact that it is possible to use C++ for more complex code paths, brings a huge advantage while coding games.

Add a declarative way for UI development in the mix and you achieve a winning combination for rapid application development.
How to Play

Chicken Wranglers is a 2-4 players game, where each player is a farmer whose goal is to capture as much chickens as he can while a timer is running. The chickens walk freely across a bi-dimensional grid, always trying to escape from the farmers and their chicken coops.

In order to push chickens to his coop, the farmer can activate lasers in the grid to build mazes that should restrain the chickens’ path and guide them to the coop. The lasers can be placed at any edge of a cell (up, down, left or right) where the farmer is currently located. Although active lasers are supposed to help a farmer, the other farmers can deactivate any laser at any time. This enables a farmer to have his maze broken by his opponents.

Note that neither chickens nor farmers are allowed to cross a laser wall before it is deactivated.
Score

Each time a chicken cross a farmer’s coop, the corresponding player scores 1 point. After the timer expires, the player with the highest score wins.

CW.png

Ashley Walker holds the Chicken Wrangler title with wrangling all 40 chickens into the hen house before the clock ran out. She was undefeated at the MeeGo Conference this spring. Do you have what it takes to steal the CW title?

Controls

Movement – A player controls his character direction via device accelerometer. The characters moves in four directions: up, down, left and right according to device tilting on any of those directions. If the device is tilted diagonally (e.g. up and left), the predominant direction will be the most tilted one.

Lasers – Tapping on screen toggles a laser on the corresponding direction of the player character’s position. For instance, if the player taps on the top of the screen, the laser is activated/deactivated above the character’s position.

Binaries

Harmattan (N9 / N950): http://get.qt.nokia.com/demos/chicken-wranglers/chicken-wranglers_0.1-0_harmattan_armel.deb

Fremantle (N900): http://get.qt.nokia.com/demos/chicken-wranglers/chicken-wranglers_0.1-0_fremantle_armel.deb

Ubuntu 11.04: http://get.qt.nokia.com/demos/chicken-wranglers/chicken-wranglers_0.1-0_i386.deb

Source

http://qt.gitorious.org/qt-labs/mobile-demos/trees/master/chicken-wranglers