Towers lasers and spacecrafts example
Jump to navigation
Jump to search
Simple example of game scene with QGraphicsScene
This simple example mostly shows how to deal with
- QGraphicsScene::advance()
- QGraphicsItem::advance()
- QGraphicsItem::paint(…)
Overview
The example contains three classes with following responsabilities
- The scene is a QGraphicsScene
- Creates and deletes new items (towers and mobile units)
- Towers
- search for nearby units
- shoot them if any
- Mobile units (fighters)
- Move
- Explode and die
View example on Youtube [youtube.com]
Main.cpp
The main() method creates a scene and a QTimer.
The timer calls Scene::advance() every 0.01 sec.
The Scene
- The scene constructor creates several towers
- Scene::advance() :
- has a counter m_TicTacTimer
- every 20th ticTac, a new mobile unit is created with a random position, direction and speed.
- dead units are removed from the item list
scene.h
scene.cpp
Towers
- Towers search for nearby units
- Search is performed by squared distance comparison
- Search stops when a first item is found
- Warning: n against n = O(n2) algorithm… Poor performances on large amount of items… but sufficient here.
- Towers shoot the located item if any
- after shooting, a tower must wait until its weapon is reloaded : it requires a “reload time” of 100ms
- The laser beam is drawn with lines of different thicknesses and colors
simpletower.h
simpletower.cpp
Mobile units
Life Cycle of a mobile unit :
- A mobile unit is created with 10 life points
- Position changes according to predefined direction and speed
- It loses life points if touched by a laser beam
- The unit explodes when lifepoints are exhausted
- The explosion has a duration.
- The explosion is drawn by painter->drawEllipse(…) with a growing radius
mobileunit.h
mobileunit.cpp