设为首页 加入收藏

TOP

1.5.1 Game Stats程序简介
2013-10-07 14:41:24 来源: 作者: 【 】 浏览:458
Tags:1.5.1 Game Stats 程序 简介

1.5  声明和初始化变量

变量代表了计算机内存的某一部分,该部分被保留下来用于存储、检索和操作数据。如果需要记录玩家的得分,则可以为它专门创建一个变量。这样一来,就可以读取并显示玩家得分。如果玩家将空中的外星敌人击毙,还可以将得分更新。

1.5.1  Game Stats程序简介

Game Stats程序显示在太空射击游戏中需要记录的诸如玩家得分、击毁敌人数目以及玩家防护盾是否开启等信息。该程序使用了一组变量来完成这些任务。程序如图1-5所示。

 
(点击查看大图)图1-5  游戏中每条数据都存储在一个变量中
从Course Technology网站(www.courseptr.com/downloads)或本书合作网站(http://www. tupwk.com.cn/downpage)上可以下载到该程序的代码。程序位于Chapter 1文件夹中,文件名为game_stats.cpp。
  1. // Game Stats  
  2. // Demonstrates declaring and initializing variables  
  3.  
  4. #include <iostream> 
  5. using namespace std;  
  6.  
  7. int main()  
  8. {  
  9.          int score;  
  10.          double distance;  
  11.          char playAgain;  
  12.          bool shieldsUp;  
  13.          short lives, aliensKilled;  
  14.          score = 0;  
  15.          distance = 1200.76;  
  16.          playAgain = 'y';  
  17.          shieldsUp = true;  
  18.          lives = 3;  
  19.          aliensKilled = 10;  
  20.          double engineTemp = 6572.89;  
  21.          cout << "\nscore: " << score << endl;  
  22.          cout << "distance: " << distance << endl;  
  23.          cout << "playAgain: " << playAgain << endl;  
  24.          //skipping shieldsUp since you don’t generally print Boolean values  
  25.          cout << "lives: " << lives << endl;  
  26.          cout << "aliensKilled: "<< aliensKilled << endl;  
  27.          cout << "engineTemp: " << engineTemp << endl;  
  28.          int fuel;  
  29.          cout << "\nHow much fuel  ";  
  30.          cin >> fuel;  
  31.          cout << "fuel: " << fuel << endl;  
  32.          typedef unsigned short int ushort;  
  33.          ushort bonus = 10;  
  34.          cout << "\nbonus: " << bonus << endl;  
  35.          return 0;  
  36. }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.3.2 使用using指令 下一篇1.5.3 类型修饰符

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: