|Home | Tutorial | Classes | Functions | Qt Scripter | Language | Library | Qt API | QSA Articles Qt Script for Applications

Game Example

The Game example demonstrates how to combine a simple customizable widget with non trivial script code to significantly increase the functionality of the widget.

The Game example consists of two main parts: the Playfield and a script file. The C++ class PlayField contains a few properties and functions that can be accessed by the script file and that offers some basic game related functionality to the developer. The game script file describes how the game works.

The PlayField Class

The PlayField class is a simple game engine for playing sprite based games. It contains functionality that makes it easy for game developers to create game objects and to receive input from the keyboard.

Programming a game for the game engine is done by implementing three stages. The first stage is initialization; this is where the script loads the images that will be used in the game and add them to the PlayField for later use. This is done by calling the script function init(). The second stage is the game loop where the PlayField runs a timer based loop that triggers every few milliseconds. Each time the loop triggers the script, function next() is called. This continues until the player either wins or looses and the game calls the function gameOver(). This will tell the game loop to stop.

The PlayField class has the following properties:

The PlayField class has the following script enabled functions:

The Shooter Game Script

The shooter game is the epic tale of the heroic space commander battling the alien armies in outer space. The player gets to be the hero!

The game is implemented using a Qt Script file that contains the game logics. This includes moving the participants of the game around or determining if a bullet shot hits or misses.

As described above, the game is divided into three stages. Initialization is implemented in the script function init(). This function is called once before the game starts running, and is used to set load the images and move them to their desired location on the screen. The second stage is the function next() which is called for every step in the game loop. In this function the script determines the actions of the player througth the PlayFields properties, then moves the player, enemies and bullets around and checks for collisions. If an enemy is hit, it is disabled. If all enemies are disabled the script calls the gameOver function with true, meaning that the user won. If the player is hit, or the enemies get past the player the script calls the function gameOver with the argument false, meaning that the player lost.

See also QSA Examples.


Copyright © 2001-2003 TrolltechTrademarks
QSA version 1.0.0-beta2