C++对战游戏代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#ifndefColor_h#defineColor_h#includewindows.hclassColor{public:voidsetColor(unsignedshortForeColor,unsignedshortBackGroundColor){HANDLEhCon=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor);}};#endif//***************************//注意命名规则//srand()函数/*给文字作色*///log类//在最后做一个统计//setw();#define_CRT_SECURE_NO_DEPRECATE#includeGame.h#includeLog.h#includeconio.h#includeiostreamusingnamespacestd;Logdiary;/**/intmain(){GameG;G.game();coutPreeanykeytoend!;_getch();return0;}//Log.h日记类主要是记录整个战斗过程#ifndefLog_h#defineLog_h#includefstreamusingnamespacestd;classLog{private:ofstreamfout;public:Log();voidwrite(stringstr);voidwrite(intNO);voidwrite(doubleno);~Log();};#endif#includeLog.hLog::Log(){fout.open(CombatRecord.txt);}Log::~Log(){fout.close();}voidLog::write(stringstr){foutstr.c_str();}voidLog::write(intNO){foutNO;}voidLog::write(doubleno){foutno;}//Wizard.h巫师类//注意命名规则#ifndefWizard_h#defineWizard_h#includestring#includeSoldier.husingnamespacestd;classWizard:publicSoldier{protected:intmaxMP;//最大魔法值doubleMP;//魔法值public:voidrest();voidwizard_attack(Soldier&enemy);public://booltest();voidalter();//修改巫师的标识符为2Wizard(stringname);Wizard();~Wizard();boolmakeDecision(Soldier&enemy);//返回是否使用了魔法};#endif#define_CRT_SECURE_NO_DEPRECATE#includeWizard.h#includestdlib.h#includetime.h#includeiostreamusingnamespacestd;#includeLog.h#includetime.hexternLogdiary;Wizard::Wizard(stringname):Soldier(name){maxMP=100;MP=100;alter();//在构造函数中自动修改标识符}voidWizard::alter(){i=2;}Wizard::Wizard(){}Wizard::~Wizard(){}voidWizard::rest(){Soldier::rest();MP+=maxMP*0.3;if(MPmaxMP)MP=maxMP;}voidWizard::wizard_attack(Soldier&enemy){setColor(14,0);coutthis-name;diary.write(name);setColor(10,0);cout[this-level级];diary.write([);diary.write(level);diary.write(级]);setColor(15,0);coutHP:;diary.write(HP:);setColor(12,0);coutthis-HP;diary.write(HP);setColor(15,0);//cout/100SP:;//diary.write(/100SP:);cout/;coutthis-maxHP;coutSP:;diary.write(/);diary.write(maxHP);diary.write(SP:);setColor(12,0);coutthis-SP;diary.write(SP);setColor(15,0);//cout/100;//diary.write(/100);cout/;coutthis-maxSP;cout;diary.write(/);diary.write(maxSP);diary.write();setColor(11,0);cout魔法攻击;diary.write(魔法攻击);setColor(14,0);coutenemy.getName();diary.write(enemy.getName());setColor(10,0);cout[enemy.getLevel()级];diary.write([);diary.write(enemy.getName());setColor(15,0);coutHP:;diary.write(HP:);setColor(12,0);coutenemy.getHP();diary.write(enemy.getHP());setColor(15,0);//cout/100SP:;//diary.write(/100SP:);cout/;coutthis-maxHP;coutSP:;diary.write(/);diary.write(maxHP);diary.write(SP:);setColor(12,0);coutenemy.getSP();diary.write(enemy.getHP());setColor(15,0);cout/;coutthis-maxSP;cout\n;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+=20;this-damage+=20;this-MP+=20;}if(enemy.getHP()=0){coutenemy.getName()被this-name打死\n;this-damage+=enemy.getDrugNum();enemy.setHP(0);enemy.setSP(0);enemy.setLevel(0);enemy.setmaxHP(0);enemy.setmaxSP(0);if(enemy.getI()==1)//在相应的计数器上加一{s_num++;}elseif(enemy.getI()==2){w_num++;}elseif(enemy.getI()==3){m_num++;}}}boolWizard::makeDecision(Soldier&enemy){inttemp;if(HPmaxHP*0.3)this-takingDrug();elseif(SPmaxSP*0.5)this-rest();else{if(MP25&&this-SP10&&enemy.getI()=2){this-wizard_attack(enemy);returntrue;}elseif(MP25&&SP10){temp=rand()%2;if(temp==0){this-attack(enemy);returnfalse;}else{this-wizard_attack(enemy);returnfalse;}}else{this-attack(enemy);returnfalse;}}returnfalse;}//***************************//Soldier.h//注意命名规则#ifndefSoldier_h#defineSoldier_h#includestring#includeLog.h#includeColor.husingnamespacestd;classSoldier:publicColor{protected:intmaxHP,maxSP;//生命最大值和体力最大值doubleHP;//生命doubleSP;//体力doubledamage;//攻击力intexp;//经验intlevel;//等级stringname;//姓名intdrugNum;//药瓶数量inti;//标明对方的身份1表示Soldier,2表示Wizard,3表示Masterints_num,w_num,m_num;//记录战士的杀敌情况public://作测试用的voidattack(Soldier&enemy);voidrest();voidtakingDrug();public:stringgetName(){returnname;}doublegetSP(){returnSP;}voidsetSP(doublesp){SP=sp;}intgetI(){returni;}intgetDrugNum(){returnthis-drugNum;}voidsetHP(doubleihp){HP=ihp;}doublegetHP(){returnHP;}intgetLevel(){returnlevel;}voidsetLevel(intilevel){level=ilevel;}voidsetmaxHP(intihp){maxHP=ihp;}voidsetmaxSP(intisp){maxSP=isp;}Soldier();Soldier(stringiname);~Soldier();virtualboolmakeDecision(Soldier&enemy);virtualvoidtotal();};#endif//******************************#define_CRT_SECURE_NO_DEPRECATE#includeSoldier.h#includeiostream#includewindows.h#includeLog.husingnamespacestd;#includeiomanipSoldier::Soldier(){}Soldier::Soldier(stringiname){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;}externLogdiary;voidSoldier::attack(Soldier&enemy){setColor(14,0);coutthis-name;diary.write(name);

1 / 28
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功