Tuesday, August 05, 2008

Qt - Gaming In Qt

The previous article covered a simple GUI application, let’s extend this concept to a game, written completely in Qt. Here is the screen-shot of the game. This game is popularly known as "Pocket Tanks" in the gaming market.

I won’t be explaining the entire code here, but let me give you a description about it. Like before, lets first split the entire setup into simpler components.

First let’s construct the complex widget - LCD Range where a LCD Number is connected to a slider. A LCD Number is created and a slider is also created with specifying a certain range. These two are connected by generating a signal from slider that the value has changed to the LCD Number slot display.

Two of these complex widgets are created - Angle and Force.

Now let’s design the gaming area itself. Here we will be using a very powerful tool known as the QPainter. It provides us with tool for drawing almost anything, lines, arcs, circles, etc.

The wall and the brick are rectangles each with their own dimensions and color. Now comes the cannon, we use a circle here with a rectangle covering a part of it corresponding to the angle. As the angle varies, the position of the rectangle will also vary. Also the brick is placed at a random to make the game more challenging.

Once the UI is done the next step is building the AI (Artificial Intelligence) for the game. Going back to physics, for a body projected at a certain angle, the following formulae exist

Vx - starting velocity in x- direction
Vy - starting velocity in y- direction
X - position in x direction of the cannon-ball after a time t from firing
Y - position in y direction of the cannon-ball after a time t from firing
g - acceleration due to gravity

Now these objects are placed properly and set as the main widget to the application. Compile it and run it. That’s it, happy gaming.

No comments:

About Me

I have done my Engineering from Osmania University and am currently working as a Senior Software Developer at McGraw-Hill Financials. I previously worked as a Technical Evangelist (Campus Ambassador) at Sun Microsystems while I was doing my Engineering.