利用c++多态实现的角色互相战斗场景 体现出c++多态的应用,和加深对面向对象的理解。
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
using namespace std;
//角色基类
//获取技能随机数种子
int get_random_flag(void)
{
//获取随机数
int flag = 1 ;
if((rand())%2==0)
return flag ;
return 0 ;
}
//获取暴击伤害随机种子 20%
int get_critical_strike_flag(void)
{
//获取随机数
int flag = 1 ;
if((rand())%5==0)
return flag ;
return 0 ;
}
class Role
{
public:
Role(){}
Role(int Power,int Life):Power(Power),Life(Life)
{
cout<<"============"<<endl;
cout << "角色属性" << endl;
cout << "力量:" << Power << endl;
cout << "生命:" << Life << endl;
}
//攻击
int attck()
{
return Power;
}
//受到的伤害
int Hurt(int l)
{
return this->Life-=l;
}
//获取生命
int get_Life()
{
return Life;
}
private:
int Power;
int Life;
};
//怪物基类
class Monster
{
public:
Monster(){}
Monster(int Power,int Life):Power(Power),Life(Life)
{
cout<<"============"<<endl;
cout << "怪物属性" << endl;
cout << "力量:" << Power << endl;
cout << "生命:" << Life << endl;
}
//攻击
int attck()
{
return Power;
}
//受到的伤害
int Hurt(int l)
{
return this->Life-=l;
}
//获取生命
int get_Life()
{
return Life;
}
private:
int Power;
int Life;
};
//角色打怪物函数
void Role_pk_Monster(Role &r ,Monster &m)
{
while (1)
{
cout << "角色发起攻击 造成伤害" << r.attck() << endl;
m.Hurt(r.attck());
if(m.get_Life() <= 0 )
{
cout << "角色战斗胜利!火锅整起来!" << endl;
return ;
}
cout << "怪物起攻击 造成伤害" << m.attck() << endl;
r.Hurt(m.attck());
if(r.get_Life() <= 0 )
{
cout << "战斗失败 回家吃糖吧!" << endl;
return ;
}
}
}
class lufei : public Role //角色 1 路飞 有自己的技能
{
public:
lufei(){}
lufei(int skill,int Power,int Life,const char*name,const char *skillname):skill(skill),Role(Power,Life)
{
strcpy(this->name,name);
strcpy(this->skillname,skillname);
cout<<this->name<<"技能:"<<skillname<<" 伤害:"<<skill<<endl;
}
int xiangjiao_huojianpao()
{
return skill;
}
int critical_strike()//产生暴击伤害 1000以上
{
return 1000+rand()%9000;
}
char *get_name()
{
return name;
}
char *get_skillname()
{
return skillname;
}
private:
//技能伤害
int skill;
char name[512];
char skillname[512];
};
class kaiduo : public Monster //怪物 凯多 有自己的技能
{
public:
kaiduo(){}
kaiduo(int skill,int Power,int Life,const char*name,const char *skillname):skill(skill),Monster(Power,Life)
{
strcpy(this->name,name);
strcpy(this->skillname,skillname);
cout<<this->name<<"技能:"<<skillname<<" 伤害:"<<skill<<endl;
}
int leiming_bagua()
{
return skill;
}
int critical_strike()//产生暴击伤害 500以上
{
return 500+rand()%9000;
}
char *get_name()
{
return name;
}
char *get_skillname()
{
return skillname;
}
private:
//技能伤害
int skill;
char name[512];
char skillname[512];
};
void lufei_vs_kaiduo(lufei &l,kaiduo &k)
{
int lufei_attk,kuaiduo_attk;
while (1)
{
if(get_critical_strike_flag() == 1 )//暴击伤害
{
lufei_attk = l.critical_strike();
cout<< l.get_name() <<"发起暴击伤害 造成伤害 " <<lufei_attk<<endl;
}else{
if(get_random_flag() == 0)
{
lufei_attk = l.attck();
cout << l.get_name() <<"发起普通攻击 造成伤害 " << lufei_attk << endl;
}else{
lufei_attk = l.xiangjiao_huojianpao();
cout << l.get_name() <<"发起技能攻击:"<<l.get_skillname()<<" 造成伤害" << lufei_attk << endl;
}
}
sleep(1);
k.Hurt(lufei_attk);
if(k.get_Life() <= 0 )
{
cout << "角色战斗胜利!火锅整起来!" << endl;
return ;
}
if(get_critical_strike_flag() == 1 )//暴击伤害
{
kuaiduo_attk = k.critical_strike();
cout <<k.get_name()<< "发起暴击伤害 造成伤害" << kuaiduo_attk<< endl;
}else {
if(get_random_flag() == 0)
{
kuaiduo_attk = k.attck();
cout << k.get_name()<<"起攻击 造成伤害" << kuaiduo_attk<< endl;
}else{
kuaiduo_attk = k.leiming_bagua();
cout << k.get_name()<<"发起技能攻击:"<<k.get_skillname()<< "造成伤害" << kuaiduo_attk<< endl;
}
}
l.Hurt(kuaiduo_attk);
if(l.get_Life() <= 0 )
{
cout << "战斗失败 回家吃糖吧!" << endl;
return ;
}
}
}
void random_combat(lufei &a,kaiduo &b,lufei &c,kaiduo &d,lufei &e,kaiduo &f)
{
//乱斗 a (随机)==>d e f
while (1)
{
if((rand())%2==0)
lufei_vs_kaiduo(a,b);
}
}
lufei l(600,100,9999,"路飞","橡胶火箭炮");
kaiduo k(500,80,6666,"凯多","雷鸣八卦");
lufei q(466,100,9999,"乔巴","乔巴甜甜圈");// 技能伤害 普通攻击 血量 名字 技能名字
kaiduo b(266,80,6666,"巴基","运气转转转");
lufei s(466,100,9999,"索隆","三刀流奥义");// 技能伤害 普通攻击 血量 名字 技能名字
kaiduo m(266,80,6666,"明哥","八爪攻击");
void *task1(void *arg)
{
lufei_vs_kaiduo(l,k);
}
void *task2(void *arg)
{
lufei_vs_kaiduo(q,b);
}
void *task3(void *arg)
{
lufei_vs_kaiduo(s,m);
}
int main(int argc, char const *argv[])
{
char str[256] = {0};
//开启线程战斗
pthread_t tid;
pthread_create(&tid, NULL,task1, NULL);
pthread_t pid1;
pthread_create(&pid1, NULL,task2, NULL);
pthread_t pid2;
pthread_create(&pid2, NULL,task3, NULL);
cout<<"按exit退出程序"<<endl;
cin>>str;
if(strcmp(str,"exit") == 0)
{
exit(0);
}
return 0;
}
输出结果:
版权声明:本文为aaa2540567665原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。