Menu
DOWN TO THE WIRE: These geeks bear gifts

DOWN TO THE WIRE: These geeks bear gifts

By the time you read this, the first LinuxWorld Expo Geek Bowl will be history. As I write this, however, it is still two days away. In case you missed it (shame on you) or are just tuning in, the Geek Bowl was a quiz show where two teams of six high-profile geeks matched wits answering trivia questions about math, computing, science fiction, television shows, cartoons, and the like.

I just put the finishing touches on the Geek Bowl quiz program, and I must say that it has been a rewarding experience writing this program in Troll Tech's Qt. In the past week I ripped out a couple of custom classes I'd written and replaced them with stock objects that Qt provides. I wrote my custom classes because I didn't realise how much Qt does for you out of the box. Of the two special classes, one displayed pictures and scaled them on demand and another displayed animations. It turns out that it was entirely unnecessary work because you can create a label (the Qt object QLabel) that automatically displays pictures or animations. All you have to do is flip a switch and the label scales the graphics for you. The method for that is QLabel->setScaled(TRUE). In the end, I decided not to use animations in the Geek Bowl, but I've filed away the knowledge of how to do it for future use.

I also changed the behaviour of the program for a very embarrassing reason: I simply couldn't think of enough questions to fill all four categories (easy, medium, hard, bizarre) to ensure we wouldn't run out of any single category. So I changed the game rules to prevent this. Instead of allowing the contestants to pick from the four categories (which would give them the opportunity to deplete a single category quickly), the program automatically cycles through the categories one by one. I like the way this worked out because it gave me the opportunity to create a few question themes. For example, the program poses four questions about The Hitchhiker's Guide to the Galaxy by Douglas Adams, going from easy to bizarre.

The last features I needed to add dealt with timing issues. The program needed to display the question category (easy, medium, etc.) for a few seconds before automatically displaying the question. And it needed to count down 15 seconds while we waited for the contestants to buzz in with an answer. When the 15 seconds are up, the program rings a bell to let everyone know it's too late to answer the question.

Timers can be tricky things to implement in a graphical application, but Qt again came to my rescue. Every Qt object has a timer that is automatically associated with it. So all I had to do was choose an object and set the timer to count by seconds. Every time a second passes, a timer event method automatically gets invoked. The first thing this method does is display the question category. After three blips of the timer, it removes the category and displays the question. Now each time the timer event gets invoked, it displays the value of the counter in the corner of the screen to let the contestants know how much time they have left.

If anyone buzzes in, I hit the space bar, which sets a flag to let the program know someone answered the question in time. The next time the timer event method is invoked, it sees the flag and recognises that someone buzzed in. So it kills the timer. If nobody buzzes in, the timer counts down to zero and flips another flag to let the program know that nobody can score on this question. The whole thing takes about 20 lines of code. I'm sure I could shrink it even more if I gave it some thought.

Finally, I have to give some credit to the development environment, KDevelop 1.2, for making this such an enjoyable program to write. KDevelop (www.kdevelop.org) is an IDE (integrated development environment) with some nice features for developing KDE (K Desktop Environment), Qt, or even Gnome applications. It has an application wizard to get you started. You can create the skeleton for a complete application in just a few clicks, including all the files you'll need to build the application at the command line if you wish to drop out of KDevelop at any point. KDE, Qt, and C documentation is just a click away, too - a priceless resource. The bottom line is that I'm more sold on Qt than ever. If you haven't looked at it yet, what are you waiting for?


Follow Us

Join the newsletter!

Error: Please check your email address.
Show Comments