This example recreates the classic game of missile defense. This VI demonstrates the use of the LabVIEW Project, advanced picture control concepts, Event structures, and the use of shared variables to transfer data between VIs. Try resizing the window for different game experiences.
File list
TOP LEVEL
Missile Defense.lvproj Project file containing all files
Missile Defense.vi Top-Level VI where user interacts with game
SUPPORT FILES
Update Array.vi Updates the explosion array
find slope.vi Creates missile information
Update line array.vi Updates the missile array
Check Collisions.vi Checks if missiles hit a city
point 2 point distance.vi Calculates the distance between two points
Draw front panel.vi Draws explosions and missiles
Draw Background.vi Draws cities and blue background
Decide next state.vi Checks for game over and level finished
Display Menus.vi Displays in-game text
Initalize Variables.vi Initialize shared variables
Center Text.vi Finds length of a string, and centers in the game window
CONTROL FILES
Gamestate.ctl type def for the gamestate enum
LevelControl.ctl type def for the level control enum
line control.ctl type def for the missile array
Explosion Array.ctl type def for the explosion array
SHARED VARIABLES
Stop True if user clicks quit button
Explosion data Stores centers, sizes, colors of all explosions
Line Data Stores start/end points, speeds of missiles
Level data Stores score, shots/missiles remaining, and level
City data stores which cities are still active
Game State stores the current state of the state machine for display
OTHER
MD.ico Icon for missile defense
variables.lvlib Project library for shared variables
Citypic.bmp bmp of a city
Open View>>VI Hierarchy from within Missile Defense.vi to see dependencies.
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
This version of the Missile Defense game uses shared variables to communicate between producer and consumer loops. This can lead to race conditions if not carefully designed. (I know, because I wrote this code and I remember how difficult it was to track down all the race conditions.)
A better example using LabVIEW Statechart can be found here Missile Defense Game using LabVIEW Statechart.