收藏 分享(赏)

C++对战游戏代码.doc

上传人:精品资料 文档编号:8219250 上传时间:2019-06-15 格式:DOC 页数:28 大小:107.50KB
下载 相关 举报
C++对战游戏代码.doc_第1页
第1页 / 共28页
C++对战游戏代码.doc_第2页
第2页 / 共28页
C++对战游戏代码.doc_第3页
第3页 / 共28页
C++对战游戏代码.doc_第4页
第4页 / 共28页
C++对战游戏代码.doc_第5页
第5页 / 共28页
点击查看更多>>
资源描述

1、#ifndef Color_h#define Color_h#includeclass Colorpublic:void setColor(unsigned short ForeColor,unsigned short BackGroundColor)HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hCon, ForeColor|BackGroundColor);#endif/*/注意命名规则/srand()函数/*给文字作色*/log 类/在最后做一个统计/setw();#define _CRT_SE

2、CURE_NO_DEPRECATE#include“Game.h“#include“Log.h“#include#includeusing namespace std;Log diary;/*/int main()Game G;G.game();coutusing namespace std;class Logprivate:ofstream fout;public:Log();void write(string str);void write(int NO);void write(double no);Log();#endif#include“Log.h“Log:Log()fout.open

3、(“CombatRecord.txt“);Log:Log()fout.close();void Log:write(string str)fout#include“Soldier.h“using namespace std;class Wizard:public Soldierprotected:int maxMP;/最大魔法值double MP;/魔法值public:void rest();void wizard_attack(Soldier public:/ bool test();void alter();/修改巫师的标识符为 2Wizard(string name);Wizard();

4、Wizard();bool makeDecision(Soldier /返回是否使用了魔法;#endif#define _CRT_SECURE_NO_DEPRECATE#include“Wizard.h“#include#include#includeusing namespace std;#include“Log.h“#includeextern Log diary;Wizard:Wizard(string name):Soldier(name)maxMP=100;MP=100;alter();/在构造函数中自动修改标识符void Wizard:alter()i=2;Wizard:Wizar

5、d()Wizard:Wizard()void Wizard:rest()Soldier:rest();MP+=maxMP*0.3;if(MPmaxMP)MP=maxMP;void Wizard:wizard_attack(Soldier coutname;diary.write(name);setColor(10,0);coutlevelHP;diary.write(HP);setColor(15,0);/ coutmaxHP;coutSP;diary.write(SP);setColor(15,0);/ cout “;/ diary.write(“/100“);coutmaxSP;cout“

6、;diary.write(“/“);diary.write(maxSP);diary.write(“);setColor(11,0);coutmaxHP;coutmaxSP;coutn“;diary.write(“/“);diary.write(maxSP);diary.write(“n“);this-SP-=10;this-MP-=25;this-exp+=5;enemy.setHP(enemy.getHP()-this-damage);if(this-exp=this-level*20)/判断巫师在攻击后是否进行升级this-level+;this-maxHP+=20;this-maxSP

7、+=20;this-damage+=20;this-MP+=20;if(enemy.getHP()namedamage+=enemy.getDrugNum();enemy.setHP(0);enemy.setSP(0);enemy.setLevel(0);enemy.setmaxHP(0);enemy.setmaxSP(0);if(enemy.getI()=1)/在相应的计数器上加一s_num+;else if(enemy.getI()=2)w_num+;else if(enemy.getI()=3)m_num+;bool Wizard:makeDecision(Soldier if(HPta

8、kingDrug();else if(SPrest();elseif(MP25return true;else if(MP25if(temp=0)this-attack(enemy);return false;elsethis-wizard_attack(enemy);return false;elsethis-attack(enemy);return false;return false;/*/Soldier.h/注意命名规则#ifndef Soldier_h#define Soldier_h#include#include“Log.h“#include“Color.h“using name

9、space std;class Soldier:public Colorprotected:int maxHP,maxSP;/生命最大值和体力最大值double HP;/生命double SP;/体力double damage;/攻击力int exp;/经验int level;/等级string name;/姓名int drugNum;/药瓶数量int i;/标明对方的身份 1 表示 Soldier,2 表示 Wizard,3 表示 Master int s_num,w_num,m_num;/记录战士的杀敌情况public:/作测试用的void attack(Soldier void rest

10、();void takingDrug();public:string getName()return name;double getSP()return SP;void setSP(double sp)SP=sp;int getI()return i;int getDrugNum()return this-drugNum;void setHP(double ihp)HP=ihp;double getHP()return HP;int getLevel()return level;void setLevel(int ilevel)level=ilevel;void setmaxHP(int ih

11、p)maxHP=ihp;void setmaxSP(int isp)maxSP=isp;Soldier();Soldier(string iname);Soldier();virtual bool makeDecision(Soldier virtual void total();#endif/*#define _CRT_SECURE_NO_DEPRECATE#include“Soldier.h“#include#include#include“Log.h“using namespace std;#includeSoldier:Soldier()Soldier:Soldier(string i

12、name)maxHP=200;maxSP=200;name=iname;HP=100;SP=100;damage=5;exp=0;level=1;drugNum=5;i=1;s_num=0;w_num=0;m_num=0;extern Log diary;void Soldier:attack(Soldier coutname;diary.write(name);setColor(10,0);coutlevelHP;diary.write(HP);setColor(15,0);coutmaxHP;coutSP;diary.write(SP);setColor(15,0);coutmaxSP;c

13、out“;diary.write(“/“);diary.write(maxSP);diary.write(“);setColor(11,0);coutmaxHP;coutmaxSP;coutn“;diary.write(“/“);diary.write(maxSP);diary.write(“n“);if(SP=5)this-SP-=5;this-exp+=5;enemy.HP-=this-damage;if(this-exp=(this-level*20+20)/看攻击者是否升级setColor(12,0);coutnamelevel+;this-maxHP+=20;this-maxSP+=

14、20;this-damage+=20;if(enemy.HPnamedrugNum+=enemy.drugNum;coutname;diary.write(name);setColor(10,0);coutlevelHP;diary.write(HP);setColor(15,0);coutmaxHP;coutSP;diary.write(SP);setColor(15,0);coutmaxSP;cout“;diary.write(“/“);diary.write(maxSP);diary.write(“);setColor(15,0);coutmaxHP)HP=maxHP;SP+=maxSP

15、*0.5;if(SPmaxSP)SP=maxHP;setColor(10,0);coutlevelHP;diary.write(HP);setColor(15,0);coutmaxHP;coutSP;diary.write(SP);setColor(15,0);coutmaxSP;coutn“;diary.write(“/“);diary.write(maxSP);diary.write(“n“);setColor(15,0);void Soldier:takingDrug()if(this-drugNum=0)setColor(12,0);coutnamename;coutlevelHP;d

16、iary.write(HP);setColor(15,0);/ coutmaxHP;coutSP;diary.write(SP);setColor(15,0);/ cout “;coutmaxSP;cout“;diary.write(“/“);diary.write(maxSP);diary.write(“);setColor(15,0);coutdrugNum-;HP+=maxHP*0.5;if(this-drugNumdrugNum=0;setColor(10,0);coutlevelHP;diary.write(HP);setColor(15,0);coutmaxHP;coutSP;di

17、ary.write(SP);setColor(15,0);coutmaxSP;coutn“;diary.write(“/“);diary.write(maxSP);diary.write(“n“);setColor(15,0);Soldier:Soldier()void Soldier:total()setColor(14,0);coutnamelevelHP;diary.write(HP);setColor(15,0);coutmaxHP;coutSP;diary.write(SP);setColor(15,0);coutmaxSP;cout“;diary.write(“/“);diary.

18、write(maxSP);diary.write(“);setColor(15,0);couts_num;diary.write(s_num);setColor(15,0);coutw_num;diary.write(w_num);setColor(15,0);couttakingDrug();else if(SPrest();elsethis-attack(enemy);return false;/士兵不可能进行魔法攻击所以直接返回 false/Master.h 大师类/注意命名规则#ifndef Master_h#define Master_h#include“Wizard.h“class

19、 Master:public Wizardprotected:int maxNP;/最大内力double NP;/内力public:void rest();void master_attack(Soldier void alter();public:Master(string name);Master();Master();bool makeDecision(Soldier ;#endif#define _CRT_SECURE_NO_DEPRECATE#include“Master.h“#includeusing namespace std;#include“Log.h“Master:Mast

20、er(string name):Wizard(name)NP=maxNP;alter();void Master:alter()i=3;Master:Master()void Master:rest()Wizard:rest();NP+=maxNP;if(NPmaxNP)NP=maxNP;extern Log diary;void Master:master_attack(Soldier coutname;diary.write(name);setColor(10,0);coutlevelHP;diary.write(HP);setColor(15,0);/ coutmaxHP;coutSP;

21、diary.write(SP);setColor(15,0);/ cout “;/ diary.write(“/100 “);coutmaxSP;cout“;diary.write(“/“);diary.write(maxSP);diary.write(“);setColor(11,0);coutmaxHP;coutSP;diary.write(SP);setColor(12,0);cout“n“);coutmaxSP;coutn“;diary.write(“/“);diary.write(maxSP);diary.write(“n“);this-NP-=50;this-SP-=20;this

22、-exp+=5;enemy.setHP(enemy.getHP()-enemy.getHP()*0.5);if(enemy.getHP()getName()damage+=enemy.getDrugNum();enemy.setHP(0);enemy.setSP(0);enemy.setLevel(0);enemy.setmaxHP(0);enemy.setmaxSP(0);if(enemy.getI()=1)s_num+;else if(enemy.getI()=2)w_num+;else if(enemy.getI()=3)m_num+;/ this-total();Master:Mast

23、er()bool Master:makeDecision(Soldier if(HPtakingDrug();else if(SPrest();elseif(enemy.getI()=3)this-master_attack(enemy);return false;else if(NP50)if(MP25if(temp=0)this-attack(enemy);return false;else if(temp=1)this-wizard_attack(enemy);return true;elsethis-master_attack(enemy);return false;elsetemp=

24、rand()%2;if(temp=0)this-attack(enemy);return false;elsethis-master_attack(enemy);return false;elseif(MP25if(temp=0)this-attack(enemy);return false;elsethis-wizard_attack(enemy);return true;elsethis-attack(enemy);return false;return false;/WarSystem.h#ifndef WarSystem_h#define WarSystem_h#include“Sol

25、dier.h“#include“Wizard.h“#include“Master.h“#include“Color.h“class WarSystem:public Colorprivate:Soldier *legion2;public:WarSystem(Soldier *ilegionA,Soldier *ilegionB);int startWar(int time1,int time2);int startRound(int,int,int);WarSystem();#endif#define _CRT_SECURE_NO_DEPRECATE#include“WarSystem.h“

26、#include#include#includeusing namespace std;#include#include“Log.h“WarSystem:WarSystem(Soldier *ilegionA,Soldier *ilegionB)legion0=ilegionA;legion1=ilegionB;extern Log diary;int WarSystem:startWar(int time1,int time2)int i=0,temp1,temp2;int flags=0;/标志战斗是否结束,并标志是谁赢了 1 表示 A 军团胜利,2 表示 B 军团胜利temp1=rand

27、()%2;/决定是那一个军团开始攻击temp2=temp1;temp2+=1;if(temp2=2)temp2=0;while(1)i+;setColor(12,0);cout1)temp1=0;if(temp21)temp2=0;Sleep(time1);int WarSystem:startRound(int temp1,int temp2,int time2)/返回战斗是否结束,如果结束还要标明是谁胜利/每个回合随机选取一个队员去攻击对方的一名战士,循环让每一个战士攻击一次int start1,start2;int flags;/标志是战斗情况bool temp;Soldier *Tem

28、p;/用于看战斗是否结束start1=rand()%10;/决定第一个去攻击的任务start2=rand()%10;/决定另外一个团队接受攻击开始的任务Temp=legiontemp1start1;/记录每一轮中第一个攻击的人,用于确定此轮战斗是否结束int ii,f;/*/用于第一次看没选中的战士是否是活的for(ii=0;iigetHP()=0)coutgetName()getHP()!=0)break;start2+;if(start2=10)start2=0;if(ii=10)/有可能在使用第二次魔法时所有的战士已经死了break;temp=legiontemp1start1-make

29、Decision(*legiontemp2start2);/.运算符的优选级高于* 运算符f+; /通过调用 makedecision 确定是否执行了魔法攻击if(temp=true)start2+;if(start2=10)start2=0;while(temp=truereturn flags;WarSystem:WarSystem()#ifndef Game_h#define Game_h#include“Color.h“#include“Soldier.h“#include#include#include“Wizard.h“#include“Master.h“#include“WarS

30、ystem.h“#includeclass Game:Colorprivate:/LAM 表示 legionA Master,LBM 类似Master LAM,LBM;/分别是 A,B 军团的大师Wizard LAW1,LBW1,/分别是 B,B 军团的 3 个巫师 LAW 表示 legionA Wizard LBW 类似LAW2,LBW2,LAW3,LBW3;Soldier LAS1,LBS1,/分别是 A,B 军团的 6 个兵 LAS 表示 legionA Soldier LBS 类似LAS2,LBS2,LAS3,LBS3,LAS4,LBS4,LAS5,LBS5,LAS6,LBS6;voi

31、d welcome();void intoserver(char *p1,char *p2,int temp);void into();void total(Soldier *temp);void introduce();public:Game();void game();#endif#include“Game.h“#include“Log.h“using namespace std;Game:Game()LAM=Master(“刘备“),LBM=Master(“孙权“);/分别是 A,B 军团的大师LAW1=Wizard(“关羽“),LBW1=Wizard(“ 周瑜“);/分别是 B,B 军

32、团的 3 个巫师 LAW 表示 legionA Wizard LBW 类似LAW2=Wizard(“张飞“),LBW2=Wizard(“ 鲁肃“);LAW3=Wizard(“马超“),LBW3=Wizard(“ 吕蒙“);LAS1=Soldier(“法正“),LBS1=Soldier(“张昭“);/分别是 A,B 军团的 6 个兵 LAS 表示 legionA Soldier LBS 类似LAS2=Soldier(“周仓“),LBS2=Soldier(“黄盖“);LAS3=Soldier(“马岱“),LBS3=Soldier(“甘宁“);LAS4=Soldier(“杨仪“),LBS4=Sold

33、ier(“丁奉“);LAS5=Soldier(“王平“),LBS5=Soldier(“程普“);LAS6=Soldier(“赵广“),LBS6=Soldier(“周善“);void Game:welcome()setColor(14,0);couttotal();extern Log diary;/全局变量void Game:game()welcome();into();introduce();int temp=1,time1=0,time2=0;couttime1;couttime2;Soldier *legionA10=Soldier *legionB10=WarSystem war(le

34、gionA,legionB);srand(int)time(NULL);temp=war.startWar(time1,time2);/由返回的值决定是哪个取得胜利,1 表示 A 军团胜利,2 表示 B军团胜利setColor(12,0);cout“nn-战斗结束-n“;setColor(15,0);if(temp=1)setColor(12,0);cout“nn 蜀国取得胜利!nn“;diary.write(“nn 蜀国取得胜利!nn“);setColor(15,0);total(legionA);elsesetColor(12,0);cout“nn 吴国取得胜利!nn“;diary.write(“nn 吴国取得胜利!nn“);setColor(15,0);total(legionB);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报