?? rpg.cpp
字號:
#include<iostream.h>
#include<time.h>
#include<stdlib.h>
enum propte {sw,ar,mg};
class container//人物的小包裹,用來裝東西的....目前只裝兩種物品~有興趣的可以自己加點東西進去`嘿嘿~
{
friend class player;//定義一個基類
friend class Swordsman;//定義一個劍士
friend class Archer;//定義一個弓劍手
friend class Mage;//定義一個魔法師,其實我們以前玩的都是這樣做的~只要把這些類附給一個人物身上就行了
friend void ShowWindows(player &p1,player &p2);//顯示框框~當前狀態(tài)的函數(shù)
protected:
int NumHeal;//生命回復(fù)劑
int NumMgWorter;//魔法恢復(fù)劑
public:
container ();//構(gòu)造函數(shù),....目前可有可無的樣子~~
bool IsNumHealEmpoty();//判斷還有沒有生命恢復(fù)劑
bool IsMWEmpoty();//判斷還有沒有魔法恢復(fù)劑
void display();//顯示當前還有多少魔法恢復(fù)劑和生命恢復(fù)劑
};
container::container()
{
NumHeal = 0;
NumMgWorter = 0;
}
bool container::IsNumHealEmpoty()
{
return NumHeal == 0?true:false;
}
bool container::IsMWEmpoty()
{
return NumMgWorter == 0?true:false;
}
void container::display()
{
cout << "還剩下回復(fù)劑(HP+100) " << NumHeal << "個" << endl;
cout << "還剩下魔法劑 (MP+80) " << NumMgWorter << "個" << endl;
}
class player//人物的基類
{
friend class Swordsman;
friend class Archer;
friend class Mage;//這些都是朋友關(guān)系~又是父與子的關(guān)系~- -! 好象有點復(fù)雜的樣子~
friend void ShowWindows(player &p1,player &p2);//上面已經(jīng)說過了~不再重復(fù)
protected:
int Hp,HpMax,Mp,MpMax,speed,Ap,Dp,EXP,LV;//最大血量和最大魔法值,當前血量,魔法值,等等~望文生意
char name[10];//人物名稱
propte role;//人物職業(yè)類型
bool death ;//是否死亡函數(shù)
container bag;//人物的小包包~~^^
public:
void IsDead();//判斷人物是否死亡
bool Dead();//確認人物當前死亡狀態(tài)~有返回值哦~
bool UseHeal();//好累...不過還是繼續(xù)打字~ 使用生命回復(fù)劑
bool UseMW();//....跟上面差不多~
void Getbag(player &p);//如果電腦死亡....就拿走他的小包包里面的東西~
void Shourole();//顯示當前人物的職業(yè)~....這個是額外的函數(shù)~亂寫的~
void HpMpfull();//每打完一關(guān)敵人~HP,MP全滿~
virtual bool attack(player &p) = 0;
virtual bool TSattack(player &p) = 0;
virtual void IsLvUp() = 0;//上面三個都是純虛函數(shù)~分別代表 人物的普通攻擊~特殊攻擊和是否升級
};
void player::HpMpfull()
{
Hp = HpMax;
Mp = MpMax;
}
void player::IsDead()
{
if (Hp <= 0 )
{
death = 1;
}
}
bool player::Dead()
{
return death == 1? true:false;
}
bool player::UseHeal()
{
if ( !bag.IsNumHealEmpoty() )
{
Hp +=100;
bag.NumHeal --;
cout << name << "使用了恢復(fù)劑,生命值恢復(fù)了100點" << endl;
if (Hp > HpMax)
{
Hp = HpMax;
}
return true;
}
else
{
cout << "沒有回復(fù)劑了 " << endl;
return false;
}
}
bool player::UseMW()
{
if ( !bag.IsMWEmpoty() )
{
Mp += 80;
bag.NumMgWorter --;
cout << name << "使用了魔法恢復(fù)劑,魔法值恢復(fù)了80點" << endl;
if (Mp > MpMax)
{
Mp = MpMax;
}
return true;
}
else
{
cout << "沒有魔法恢復(fù)劑了~" << endl;
return false;
}
}
void player::Getbag(player &p)
{
cout << name << "獲得了對方物品" << endl;
cout << "獲得了回復(fù)劑 " << p.bag.NumHeal << "個" << endl;
cout << "獲得了魔法回復(fù)劑 " << p.bag.NumMgWorter << "個" << endl;
bag.NumHeal += p.bag.NumHeal;
bag.NumMgWorter += p.bag.NumMgWorter;
}
void player::Shourole()
{
switch(role)
{
case sw:
cout << "職業(yè):劍士" << endl;
break;
case ar:
cout << "職業(yè):弓箭手" << endl;
break;
case mg:
cout << "職業(yè):魔法師" << endl;
break;
}
}
class Swordsman:public player//從基類派生出的一個類,是關(guān)于劍士的~
{
public:
Swordsman(int i, char *chname)//構(gòu)造函數(shù)~賦予當前人物的屬性值
{
role = sw;
int j;
for (j=0; j<10; j++)
{
name[j] = chname[j];
}
Hp = 150 + 8 * (i - 1);
HpMax = 150 + 8 * (i - 1);
Mp = 80 + 3 * (i - 1);
MpMax = 80 + 3 * (i - 1);
Ap = 25 + 4 * (i - 1);
Dp = 25 + 4 * (i - 1);
speed = 25 + 2 * (i - 1);
LV = i;
death = 0;
EXP = 0;
bag.NumHeal = i * 5;
bag.NumMgWorter = i * 5;
}
bool attack(player &p);//劍士的普通攻擊
bool TSattack(player &p);//特殊攻擊
void IsLvUp();//判斷下是否升級
void AI(player &p);//規(guī)定了電腦只能是劍士..當然~如果要電腦是其他角色就要寫多點代碼~可惜我比較懶..
};
bool Swordsman::attack(player &p)
{
int Hphit;
int EXPhit;
cout << name << "攻擊" << endl;
srand(time(NULL));
int j = rand()%100;
if (speed >= p.speed && j <= 25)
{
cout << "揮著大劍向敵人砍去" << endl;
cout << "會心一擊" << endl;
Hphit = p.Hp;
p.Hp = p.Hp - (Ap + j%10 + 10 - p.Dp) * 2 + 2 * (LV - 1);
cout << "直接命中敵人腦門,頓時鮮血四濺" << endl;
cout << "敵人損失Hp " << Hphit - p.Hp << endl;
EXPhit = EXP;
EXP += Hphit - p.Hp;
cout << name <<"獲得經(jīng)驗 " << EXP - EXPhit << endl;
p.IsDead();
return true;
}
else if(speed <= p.speed && (j >25 || j <=50) )
{
cout << "拿著劍搖搖晃晃的朝敵人砍去。。。" << endl;
cout << "敵人躲避" << endl;
return true;
}
else if(j < 100 && j > 50)
{
cout << "揮劍向敵人砍去" << endl;
Hphit = p.Hp;
p.Hp = p.Hp - (Ap + j%10 + 5 - p.Dp) + 2 * (LV - 1);
cout << "命中敵人" << endl;
cout << "敵人損失Hp " << Hphit - p.Hp << endl;
EXPhit = EXP;
EXP += Hphit - p.Hp;
cout << name << "獲得EXP " << EXP - EXPhit << endl;
p.IsDead();
return true;
}
else
return false;
}
bool Swordsman::TSattack(player &p)
{
srand( time(NULL) );
int j = rand()%100;
int Hphit;
int EXPhit;
if ( Mp >= 50 )
{
cout << "萬劍訣" << endl;
Mp -= 40;
if (j <= 49 || j >= 60)
{
cout << "完全命中目標 " << endl;
cout << "敵人完全吸收了傷害" << endl;
Hphit = p.Hp;
p.Hp = p.Hp - (Ap + j/10 + 10 - p.Dp) * 3 + 2 * ( LV -1);
cout << "敵人損失Hp " << Hphit - p.Hp << endl;
EXPhit = EXP;
EXP += Hphit - p.Hp;
cout << "獲得EXP " << EXP - EXPhit << endl;
p.IsDead();
return true;
}
else
{
cout << "敵人無意中的躲避, 躲開攻擊" << endl;
cout << "攻擊無效" << endl;
return true;
}
}
else
{
cout << "魔法不足! " << endl;
return false;
}
}
void Swordsman::IsLvUp()
{
if (EXP >= LV * LV * 75)
{
EXP -= LV * LV * 75;
++LV;
HpMax = 150 + 8 * (LV -1);
MpMax = 80 + 3 * (LV -1);
Ap += 3;
Dp += 3;
speed += 4;
cout << " 恭喜,"<< name <<"升級了~~ " << " 等級為" << LV << endl;
}
}
void Swordsman::AI(player &p)
{
int Hphit;
srand( time(NULL) );
int j = rand()%100;
if (Hp <= 70 && j <=90 && !bag.IsNumHealEmpoty() )
{
cout << "敵人使用了回復(fù)劑,體力恢復(fù)了100點 " << endl;
Hp +=100;
bag.NumHeal --;
if ( Hp > HpMax )
{
Hp = HpMax;
}
}
else if ( (Mp>=50 && j<=80) || (p.Hp<=75 && Mp>=50 && j<=50) )
{
cout << "敵人的特殊攻擊(還沒想好- -!) " << endl;
Mp -= 40;
if ( speed <= p.speed && (j >= 20 || j <= 30) )
{
cout << p.name << "無意中的走位 " << endl;
cout << p.name << "躲開了敵人攻擊無效 " << endl;
}
else
{
cout << p.name << "受到猛烈沖擊 " << endl;
Hphit = p.Hp;
p.Hp -= (Ap + j%10 + 10 - p.Dp) * 2 + 2 * (LV - 1);
cout << p.name << "損失Hp " << Hphit - p.Hp << endl;
p.IsDead();
}
}
else if (Mp < 50 && j <=40 && !bag.IsMWEmpoty() )
{
cout << "敵人使用魔法劑,魔法值恢復(fù)了80點~ " << endl;
Mp += 80;
bag.NumMgWorter --;
if (Mp > MpMax)
{
Mp = MpMax;
}
}
else
{
cout << "敵人的普通攻擊 " << endl;
if ( j <= 25 && speed <= p.speed )
{
cout << p.name << " 躲開攻擊 " << endl;
}
else
{
cout << p.name <<"受到傷害 " << endl;
Hphit = p.Hp;
p.Hp -= (Ap + j%10 + 10 - p.Dp) + (LV - 1);
cout << "損失Hp " << Hphit - p.Hp << endl;
p.IsDead();
}
}
}
class Archer:public player//差不多拉~只是數(shù)值和絕招要改改~其他的沒什么了~只是少了個AI電腦智能函數(shù)
{
public:
Archer(int i, char *chname)
{
role = ar;
int j;
for (j=0; j<10; j++)
{
name[j] = chname[j];
}
Hp = 150 + 8 * (i - 1);
HpMax = 150 + 8 * (i - 1);
Mp = 80 + 3 * (i - 1);
MpMax = 80 + 3 * (i - 1);
Ap = 25 + 4 * (i - 1);
Dp = 25 + 4 * (i - 1);
speed = 25 + 2 * (i - 1);
LV = i;
death = 0;
EXP = LV * LV * 75;
bag.NumHeal = i * 5;
bag.NumMgWorter = i * 5;
}
bool attack(player &p);
bool TSattack(player &p);
void IsLvUp();
};
bool Archer::attack(player &p)
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -