< Previous | Contents | Next >
The Game Stats program displays information that you might want to keep track of in a space shooter game, such as a player’s score, the number of enemies the player has destroyed, and whether the player has his shields up. The program uses a group of variables to accomplish all of this. Figure 1.5 illustrates the program.
Figure 1.5
Each game stat is stored in a variable.
You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 1 folder; the filename is game_stats.cpp.
// Game Stats
// Demonstrates declaring and initializing variables
Declaring and Initializing Variables 17
#include <iostream> using namespace std;
int main()
{
int score; double distance; char playAgain; bool shieldsUp;
short lives, aliensKilled; score = 0;
distance = 1200.76;
playAgain = ’y’; shieldsUp = true; lives = 3;
aliensKilled = 10;
double engineTemp = 6572.89;
cout << "\nscore: " << score << endl; cout << "distance: " << distance << endl; cout << "playAgain: " << playAgain << endl;
//skipping shieldsUp since you don’t generally print Boolean values cout << "lives: " << lives << endl;
cout << "aliensKilled: "<< aliensKilled << endl; cout << "engineTemp: " << engineTemp << endl;
int fuel;
cout << "\nHow much fuel? "; cin >> fuel;
cout << "fuel: " << fuel << endl;
typedef unsigned short int ushort; ushort bonus = 10;
cout << "\nbonus: " << bonus << endl;
return 0;
}
18 Chapter 1 n Types, Variables, and Standard I/O: Lost Fortune