博文背景:

  还记大二上学期的时候看的这个C++&RPG游戏(博主大一下学期自学的php,涵盖oop内容),一个外校的同学他们大一学的C++,大二初期C++实训要求做一个程序填空,就是这个 RPG 对战游戏,有几处空需要填上,问我可以不,我就信誓旦旦的说OK,最后“万般无奈多方考究”才给将程序补充完整。最近在做毕业设计,用到C++&QT oop部分,重新温习了下C++ oop,就想起来大二的时候还“捣鼓”过这么个游戏,现重新看了下,进行整理总结,写下该博文。

--------------------华丽的分割线,开始正题-----------------

本RPG游戏介绍:

  用户创建一个玩家,与AI进行PK,PK中,可以普通攻击(随机暴击、躲闪)、技能攻击、加血、加蓝。本游戏中指定了5个对手,任务失败或者打败所有对手游戏结束。

代码架构:

mian.cpp:控制游戏流程.

container.h container.cpp:物品类.

player.h player.cpp:角色类.

swordsman.h swordsman.cpp:剑士角色.

archer.h archer.cpp:弓箭手角色.

mage.h mage.cpp:魔法师角色.

每个角色都需要物品,每个角色都继承与角色类。

全部代码如下:

 //=======================
// main.cpp
//======================= // main function for the RPG style game #include <iostream>
#include <string>
using namespace std; #include "swordsman.h"
#include "archer.h"
#include "mage.h" int main()
{
string tempName;
bool success=; //flag for storing whether operation is successful
cout <<"请输入玩家姓名:";
cin >>tempName; // get player's name from keyboard input
player *human; // use pointer of base class, convenience for polymorphism
int tempJob; // temp choice for job selection
do
{
cout <<"请选择职业: 1 战士, 2 弓箭手, 3 法师"<<endl;
cin>>tempJob;
system("cls"); // clear the screen
switch(tempJob)
{
case :
human=new swordsman(,tempName); // create the character with user inputted name and job
success=; // operation succeed
break;
case :
human=new archer(,tempName); // create the character with user inputted name and job
success=; // operation succeed
break;
case :
human=new mage(,tempName); // create the character with user inputted name and job
success=; // operation succeed
break;
default:
break; // In this case, success=0, character creation failed
}
}
while(success!=); // so the loop will ask user to re-create a character int tempCom; // temp command inputted by user
int nOpp=; // the Nth opponent
for(int i=; nOpp<; i+=) // i is opponent's level
{
nOpp++;
system("cls");
cout<<"STAGE" <<nOpp<<endl;
cout<<"你的对手, 一个 Lv. "<<i<<" 战士"<<endl;
system("pause");
swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior"
human->reFill(); // get HP/MP refill before start fight while(!human->death() && !enemy.death()) // no died
{
success=;
while (success!=)
{
showinfo(*human,enemy); // show fighter's information
cout<<"请选择: "<<endl;
cout<<"1 攻击; 2 技能; 3 使用体力药水; 4 使用魔法药水; 0 离开游戏"<<endl;
cin>>tempCom;
switch(tempCom)
{
case :
cout<<"确定退出吗? Y/N"<<endl;
char temp;
cin>>temp;
if(temp=='Y'||temp=='y')
return ;
else
break;
case :
success=human->attack(enemy);
human->isLevelUp();
enemy.isDead();
break;
case :
success=human->specialatt(enemy);
human->isLevelUp();
enemy.isDead();
break;
case :
success=human->useHeal();
break;
case :
success=human->useMW();
break;
default:
break;
}
}
if(!enemy.death()) // If AI still alive
enemy.AI(*human);
else // AI died
{
cout<<"YOU WIN"<<endl;
human->transfer(enemy); // player got all AI's items
}
if (human->death())
{
system("cls");
cout<<endl<<setw()<<"GAME OVER"<<endl;
delete human;//6_??????????? // player is dead, program is getting to its end, what should we do here?
system("pause");
return ;
}
}
}
delete human;//7_????????? // You win, program is getting to its end, what should we do here?
system("cls");
cout<<"恭喜你! 你击败了所有对手!!"<<endl;
system("pause");
return ;
}
 //=======================
// container.h
//======================= // The so-called inventory of a player in RPG games
// contains two items, heal and magic water #ifndef _CONTAINER//1_????????????? // Conditional compilation
#define _CONTAINER class container // Inventory
{
protected:
int numOfHeal; // number of heal
int numOfMW; // number of magic water
public:
container(); // constuctor
void set(int heal_n, int mw_n); // set the items numbers
int nOfHeal(); // get the number of heal
int nOfMW(); // get the number of magic water
void display(); // display the items;
bool useHeal(); // use heal
bool useMW(); // use magic water
}; #endif
 //=======================
// container.cpp
//=======================
#include "container.h"
#include <iostream>
using namespace std;
// default constructor initialise the inventory as empty
container::container()
{
set(,);
} // set the item numbers
void container::set(int heal_n, int mw_n)
{
numOfHeal=heal_n;
numOfMW=mw_n;
} // get the number of heal
int container::nOfHeal()
{
return numOfHeal;
} // get the number of magic water
int container::nOfMW()
{
return numOfMW;
} // display the items;
void container::display()
{
cout<<"你的物品: "<<endl;
cout<<"恢复(HP+100): "<<numOfHeal<<endl;
cout<<"魔法药水 (MP+80): "<<numOfMW<<endl;
} //use heal
bool container::useHeal()
{
numOfHeal--;//2_????????
return ; // use heal successfully
} //use magic water
bool container::useMW()
{
numOfMW--;
return ; // use magic water successfully
}
 //=======================
// player.h
//======================= // The base class of player
// including the general properties and methods related to a character #ifndef _PLAYER
#define _PLAYER #include <iomanip> // use for setting field width
#include <time.h> // use for generating random factor
#include "container.h"
#include <string>
#include <windows.h>
using namespace std; enum job {sw, ar, mg}; /* define 3 jobs by enumerate type
sword man, archer, mage */
class player
{
friend void showinfo(player &p1, player &p2);
friend class swordsman;
friend class archer;
friend class mage; protected:
int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
// General properties of all characters
string name; // character name
job role; /* character's job, one of swordman, archer and mage,
as defined by the enumerate type */
container bag; // character's inventory public:
virtual bool attack(player &p)=; // normal attack
virtual bool specialatt(player &p)=; //special attack
virtual void isLevelUp()=; // level up judgement
/* Attention!
These three methods are called "Pure virtual functions".
They have only declaration, but no definition.
The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects.
The detailed definition of these pure virtual functions will be given in subclasses. */ void reFill(); // character's HP and MP resume
bool death(); // report whether character is dead
void isDead(); // check whether character is dead
bool useHeal(); // consume heal, irrelevant to job
bool useMW(); // consume magic water, irrelevant to job
void transfer(player &p); // possess opponent's items after victory
void showRole(); // display character's job private:
bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited
}; #endif
 //=======================
// player.cpp
//======================= #include "player.h"
#include <iostream>
//using namespace std;
// character's HP and MP resume
void player::reFill()
{
HP=HPmax; // HP and MP fully recovered
MP=MPmax;
} // report whether character is dead
bool player::death()
{
return playerdeath;
} // check whether character is dead
void player::isDead()
{
if(HP<=) // HP less than 0, character is dead
{
cout<<name<<" 死亡." <<endl;
system("pause");
playerdeath=; // give the label of death value 1
}
} // consume heal, irrelevant to job
bool player::useHeal()
{
if(bag.nOfHeal()>)
{
HP=HP+;
if(HP>HPmax) // HP cannot be larger than maximum value
HP=HPmax; // so assign it to HPmax, if necessary
cout<<name<<" 使用恢复, HP 增加 100."<<endl;
bag.useHeal(); // use heal
system("pause");
return ; // usage of heal succeed
}
else // If no more heal in bag, cannot use
{
cout<<"对不起, 你没有足够的恢复."<<endl;
system("pause");
return ; // usage of heal failed
}
} // consume magic water, irrelevant to job
bool player::useMW()
{
if(bag.nOfMW()>)
{
MP=MP+;
if(MP>MPmax)
MP=MPmax;
cout<<name<<" 使用魔法药水, MP 增加 100."<<endl;
bag.useMW();
system("pause");
return ; // usage of magic water succeed
}
else
{
cout<<"对不起,你没有足够的魔法药水."<<endl;
system("pause");
return ; // usage of magic water failed
}
} // possess opponent's items after victory
void player::transfer(player &p)
{
cout<<name<<" 获得"<<p.bag.nOfHeal()<<" 恢复, 和 "<<p.bag.nOfMW()<<" 魔法药水."<<endl;
system("pause");
bag.set(bag.nOfHeal() + p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW());//3_???????????
// set the character's bag, get opponent's items
} // display character's job
void player::showRole()
{
switch(role)
{
case sw:
cout<<"战士";
break;
case ar:
cout<<"弓箭手";
break;
case mg:
cout<<"法师";
break;
default:
break;
}
} // display character's job
void showinfo(player &p1, player &p2)//4_??????????????
{
system("cls");
cout<<"##############################################################"<<endl;
cout<<"# 玩家"<<setw()<<p1.name<<" LV. "<<setw() <<p1.LV
<<" # 对手"<<setw()<<p2.name<<" LV. "<<setw() <<p2.LV<<" #"<<endl;
cout<<"# HP "<<setw()<<(p1.HP<=?p1.HP:)<<'/'<<setw()<<(p1.HPmax<=?p1.HPmax:)
<<" | MP "<<setw()<<(p1.MP<=?p1.MP:)<<'/'<<setw()<<(p1.MPmax<=?p1.MPmax:)
<<" # HP "<<setw()<<(p2.HP<=?p2.HP:)<<'/'<<setw()<<(p2.HPmax<=?p2.HPmax:)
<<" | MP "<<setw()<<(p2.MP<=?p2.MP:)<<'/'<<setw()<<(p2.MPmax<=?p2.MPmax:)<<" #"<<endl;
cout<<"# AP "<<setw()<<(p1.AP<=?p1.AP:)
<<" | DP "<<setw()<<(p1.DP<=?p1.DP:)
<<" | speed "<<setw()<<(p1.speed<=?p1.speed:)
<<" # AP "<<setw()<<(p2.AP<=?p2.AP:)
<<" | DP "<<setw()<<(p2.DP<=?p2.DP:)
<<" | speed "<<setw()<<(p2.speed<=?p2.speed:)<<" #"<<endl;
cout<<"# EXP"<<setw()<<p1.EXP<<" 职业: "<<setw();
p1.showRole();
cout<<" # EXP"<<setw()<<p2.EXP<<" 职业: "<<setw();
p2.showRole();
cout<<" #"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
p1.bag.display();
cout<<"##############################################################"<<endl;
}
 //=======================
// swordsman.h
//======================= // Derived from base class player
// For the job Swordsman #include "player.h"
class swordsman :public player// 5_????????? // subclass swordsman publicly inherited from base player
{
public:
swordsman(int lv_in=, string name_in="Not Given");
// constructor with default level of 1 and name of "Not given"
void isLevelUp();
bool attack (player &p);
bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent
};
 //=======================
// swordsman.cpp
//=======================
#include "swordsman.h"
#include <iostream> // constructor. default values don't need to be repeated here
swordsman::swordsman(int lv_in, string name_in)
{
role=sw; // enumerate type of job
LV=lv_in;
name=name_in; // Initialising the character's properties, based on his level
HPmax=+*(LV-); // HP increases 8 point2 per level
HP=HPmax;
MPmax=+*(LV-); // MP increases 2 points per level
MP=MPmax;
AP=+*(LV-); // AP increases 4 points per level
DP=+*(LV-); // DP increases 4 points per level
speed=+*(LV-); // speed increases 2 points per level playerdeath=;
EXP=(LV-)*(LV-)*;
bag.set(lv_in, lv_in);
} void swordsman::isLevelUp()
{
if(EXP>=LV*LV*)
{
LV++;
AP+=;
DP+=;
HPmax+=;
MPmax+=;
speed+=;
cout<<name<<" Level UP!"<<endl;
cout<<"HP 增加 8 变成 "<<HPmax<<endl;
cout<<"MP 增加 2 变成 "<<MPmax<<endl;
cout<<"Speed 增加 2 变成 "<<speed<<endl;
cout<<"AP 增加 4 变成 "<<AP<<endl;
cout<<"DP 增加 5 变成 "<<DP<<endl;
system("pause");
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
}
} bool swordsman::attack(player &p)
{
double HPtemp=; // opponent's HP decrement
double EXPtemp=; // player obtained exp
double hit=; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack
if ((speed>p.speed) && (rand()%<(speed-p.speed))) // rand()%100 means generates a number no greater than 100
{
HPtemp=(int)((1.0*AP/p.DP)*AP*/(rand()%+)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
cout<<name<<"的攻击伤害了 "<<p.name<<", "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
p.HP=int(p.HP-HPtemp);
EXPtemp=(int)(HPtemp*1.2);
} // If speed smaller than opponent, the opponent has possibility to evade
if ((speed<p.speed) && (rand()%<))
{
cout<<name<<"的攻击被 "<<p.name<<"躲避"<<endl;
system("pause");
return ;
} // 10% chance give critical attack
if (rand()%<=)
{
hit=1.5;
cout<<"关键的攻击: ";
} // Normal attack
HPtemp=(int)((1.0*AP/p.DP)*AP*/(rand()%+));
cout<<name<<"使用猛击, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
p.HP=(int)(p.HP-HPtemp);
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
EXP=(int)(EXP+EXPtemp);
system("pause");
return ; // Attack success
} bool swordsman::specialatt(player &p)
{
if(MP<)
{
cout<<"你没有足够的魔法值!"<<endl;
system("pause");
return ; // Attack failed
}
else
{
MP-=; // consume 40 MP to do special attack //10% chance opponent evades
if(rand()%<=)
{
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl;
system("pause");
return ;
} double HPtemp=;
double EXPtemp=;
//double hit=1;
//srand(time(NULL));
HPtemp=(int)(AP*1.2+); // not related to opponent's DP
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
cout<<name<<" 使用技能, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
p.HP=(int)(p.HP-HPtemp);
EXP=(int)(EXP+EXPtemp);
system("pause");
}
return ; // special attack succeed
} // Computer opponent
void swordsman::AI(player &p)
{
if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+<=1.1*HPmax)&&(bag.nOfHeal()>)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5)))
// AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{
useHeal();
}
else
{
if(MP>= && HP>0.5*HPmax && rand()%<=)
// AI has enough MP, it has 30% to make special attack
{
specialatt(p);
p.isDead(); // check whether player is dead
}
else
{
if (MP< && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water
{
useMW();
}
else
{
attack(p); // normal attack
p.isDead();
}
}
}
}
 //=======================
// archer.h
//======================= // Derived from base class player
// For the job archer #include "player.h"
class archer :public player// 5_????????? // subclass swordsman publicly inherited from base player
{
public:
archer(int lv_in=, string name_in="Not Given");
// constructor with default level of 1 and name of "Not given"
void isLevelUp();
bool attack (player &p);
bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent
};
 //=======================
// archer.cpp
//=======================
#include "archer.h"
#include <iostream>
#include <cstdio>
#include <windows.h>
// constructor. default values don't need to be repeated here
archer::archer(int lv_in, string name_in)
{
role=sw; // enumerate type of job
LV=lv_in;
name=name_in; // Initialising the character's properties, based on his level
HPmax=+*(LV-); // HP increases 8 point2 per level
HP=HPmax;
MPmax=+*(LV-); // MP increases 2 points per level
MP=MPmax;
AP=+*(LV-); // AP increases 4 points per level
DP=+*(LV-); // DP increases 4 points per level
speed=+*(LV-); // speed increases 2 points per level playerdeath=;
EXP=(LV-)*(LV-)*;
bag.set(lv_in, lv_in);
} void archer::isLevelUp()
{
if(EXP>=LV*LV*)
{
LV++;
AP+=;
DP+=;
HPmax+=;
MPmax+=;
speed+=;
cout<<name<<" Level UP!"<<endl;
cout<<"HP 增加 8 变成 "<<HPmax<<endl;
cout<<"MP 增加 2 变成 "<<MPmax<<endl;
cout<<"Speed 增加 2 变成 "<<speed<<endl;
cout<<"AP 增加 2 变成 "<<AP<<endl;
cout<<"DP 增加 6 变成 "<<DP<<endl;
system("pause");
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
}
} bool archer::attack(player &p)
{
double HPtemp=; // opponent's HP decrement
double EXPtemp=; // player obtained exp
double hit=; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack
if ((speed>p.speed) && (rand()%<(speed-p.speed))) // rand()%100 means generates a number no greater than 100
{
HPtemp=(int)(DP*/(rand()%+)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
cout<<name<<"的攻击伤害了 "<<p.name<<", "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
p.HP=int(p.HP-HPtemp);
EXPtemp=(int)(HPtemp*1.2);
} // If speed smaller than opponent, the opponent has possibility to evade
if ((speed<p.speed) && (rand()%<))
{
cout<<name<<"的攻击被 "<<p.name<<"躲避"<<endl;
system("pause");
return ;
} // 10% chance give critical attack
if (rand()%<=)
{
hit=1.5;
cout<<"关键的攻击: ";
} // Normal attack
HPtemp=(int)(DP*/(rand()%+));
cout<<name<<"使用猛击, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
p.HP=(int)(p.HP-HPtemp);
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
EXP=(int)(EXP+EXPtemp);
system("pause");
return ; // Attack success
} bool archer::specialatt(player &p)
{
if(MP<)
{
cout<<"你没有足够的魔法值!"<<endl;
system("pause");
return ; // Attack failed
}
else
{
MP-=; // consume 40 MP to do special attack //10% chance opponent evades
if(rand()%<=)
{
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl;
system("pause");
return ;
} double HPtemp=;
double EXPtemp=;
//double hit=1;
//srand(time(NULL));
HPtemp=(int)(AP*1.2+); // not related to opponent's DP
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
cout<<name<<" 使用技能, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
p.HP=(int)(p.HP-HPtemp);
EXP=(int)(EXP+EXPtemp);
system("pause");
}
return ; // special attack succeed
} // Computer opponent
void archer::AI(player &p)
{
if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+<=1.1*HPmax)&&(bag.nOfHeal()>)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5)))
// AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{
useHeal();
}
else
{
if(MP>= && HP>0.5*HPmax && rand()%<=)
// AI has enough MP, it has 30% to make special attack
{
specialatt(p);
p.isDead(); // check whether player is dead
}
else
{
if (MP< && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water
{
useMW();
}
else
{
attack(p); // normal attack
p.isDead();
}
}
}
}
 //=======================
// mage.h
//======================= // Derived from base class player
// For the job mage #include "player.h"
class mage :public player// 5_????????? // subclass swordsman publicly inherited from base player
{
public:
mage(int lv_in=, string name_in="Not Given");
// constructor with default level of 1 and name of "Not given"
void isLevelUp();
bool attack (player &p);
bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent
};
 //=======================
// mage.cpp
//=======================
#include "mage.h"
#include <iostream> // constructor. default values don't need to be repeated here
mage::mage(int lv_in, string name_in)
{
role=sw; // enumerate type of job
LV=lv_in;
name=name_in; // Initialising the character's properties, based on his level
HPmax=+*(LV-); // HP increases 8 point2 per level
HP=HPmax;
MPmax=+*(LV-); // MP increases 2 points per level
MP=MPmax;
AP=+*(LV-); // AP increases 4 points per level
DP=+*(LV-); // DP increases 4 points per level
speed=+*(LV-); // speed increases 2 points per level playerdeath=;
EXP=(LV-)*(LV-)*;
bag.set(lv_in, lv_in);
} void mage::isLevelUp()
{
if(EXP>=LV*LV*)
{
LV++;
AP+=;
DP+=;
HPmax+=;
MPmax+=;
speed+=;
cout<<name<<" Level UP!"<<endl;
cout<<"HP 增加 8 变成 "<<HPmax<<endl;
cout<<"MP 增加 2 变成 "<<MPmax<<endl;
cout<<"Speed 增加 2 变成 "<<speed<<endl;
cout<<"AP 增加 4 变成 "<<AP<<endl;
cout<<"DP 增加 5 变成 "<<DP<<endl;
system("pause");
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
}
} bool mage::attack(player &p)
{
double HPtemp=; // opponent's HP decrement
double EXPtemp=; // player obtained exp
double hit=; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack
if ((speed>p.speed) && (rand()%<(speed-p.speed))) // rand()%100 means generates a number no greater than 100
{
HPtemp=(int)(AP*/(rand()%+)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
cout<<name<<"的攻击伤害了 "<<p.name<<", "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
p.HP=int(p.HP-HPtemp);
EXPtemp=(int)(HPtemp*1.2);
} // If speed smaller than opponent, the opponent has possibility to evade
if ((speed<p.speed) && (rand()%<))
{
cout<<name<<"的攻击被 "<<p.name<<"躲避"<<endl;
system("pause");
return ;
} // 10% chance give critical attack
if (rand()%<=)
{
hit=1.5;
cout<<"关键的攻击: ";
} // Normal attack
HPtemp=(int)(AP*/(rand()%+));
cout<<name<<"使用猛击, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
p.HP=(int)(p.HP-HPtemp);
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
EXP=(int)(EXP+EXPtemp);
system("pause");
return ; // Attack success
} bool mage::specialatt(player &p)
{
if(MP<)
{
cout<<"你没有足够的魔法值!"<<endl;
system("pause");
return ; // Attack failed
}
else
{
MP-=; // consume 40 MP to do special attack //10% chance opponent evades
if(rand()%<=)
{
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl;
system("pause");
return ;
} double HPtemp=;
double EXPtemp=;
//double hit=1;
//srand(time(NULL));
HPtemp=(int)(AP*1.2+); // not related to opponent's DP
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
cout<<name<<" 使用技能, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
p.HP=(int)(p.HP-HPtemp);
EXP=(int)(EXP+EXPtemp);
system("pause");
}
return ; // special attack succeed
} // Computer opponent
void mage::AI(player &p)
{
if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+<=1.1*HPmax)&&(bag.nOfHeal()>)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5)))
// AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{
useHeal();
}
else
{
if(MP>= && HP>0.5*HPmax && rand()%<=)
// AI has enough MP, it has 30% to make special attack
{
specialatt(p);
p.isDead(); // check whether player is dead
}
else
{
if (MP< && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water
{
useMW();
}
else
{
attack(p); // normal attack
p.isDead();
}
}
}
}

【经典】C++&RPG对战游戏的更多相关文章

  1. ADO.Net(五)——实战:对战游戏

    对战游戏 要求: 自建数据表(例如:数据表包含:代号,姓名,性别,血量,攻击力,防御力,命中,闪避,等级等字段) 需要通过程序向数据表添加人员 添加的时候,根据用户输入的名字,自动计算生成相应的血量. ...

  2. C#小游戏(文字对战游戏)

    第一代,不是很完善,会在后续增加更多的功能 主: using System; using System.Collections.Generic; using System.Linq; using Sy ...

  3. c#部分---网吧充值系统;简易的闹钟;出租车计费;简单计算器;对战游戏;等额本金法计算贷款还款利息等;随机生成10个不重复的50以内的整数;推箱子;

    网吧充值系统namespace ConsoleApplication1 { class Program { struct huiyuan { public string name; public st ...

  4. 分享一组Rpg Marker人物行走,游戏素材图片,共20张图片

    分享一组Rpg Marker人物行走,游戏素材图片,共20张图片 上面的下载地址链接是图片,无法直接复制哦!下载请直接点击: 游戏素材下载  或者复制以下链接:http://***/view/13.h ...

  5. ADO.NET 扩展属性、配置文件 和 对战游戏

    扩展属性 有外键关系时将信息处理成用户可看懂的 利用扩展属性 如:Info表中的民族列显示的是民族代号处理成Nation表中的民族名称 需要在Info类里面扩展一个显示nation名称的属性 例:先前 ...

  6. C# 推箱子游戏&对战游戏

    推箱子游戏提纲,只有向右向上的操作,向左向下同理,后期需完善. namespace 推箱子 { class Program { static void Main(string[] args) { // ...

  7. 基于Udp的五子棋对战游戏

    引言 本文主要讲述在局域网内,使用c#基于Udp协议编写一个对战的五子棋游戏.主要从Udp的使用.游戏的绘制.对战的逻辑这三个部分来讲解. 开发环境:vs2013,.Net4.0,在文章的末尾提供源代 ...

  8. VS 游戏:推箱子&对战游戏

    推箱子 //只做了两关 class Program { static void Main(string[] args) { ; ; ; ; ; ; ; #region 地图绘制 , , ] { { { ...

  9. Unity《ATD》塔防RPG类3D游戏架构设计(二)

    目录 <ATD> 游戏模型 <ATD> 游戏逻辑 <ATD> UI/HUD/特效/音乐 结语 前篇:Unity<ATD>塔防RPG类3D游戏架构设计(一 ...

随机推荐

  1. tttttabs

    <div id="fil-page" class="fil-page"> <div class="fil-container&quo ...

  2. [转] EJB到底是什么,真的那么神秘吗??

    原文地址:http://blog.csdn.net/jojo52013145/article/details/5783677 1. 我们不禁要问,什么是"服务集群"?什么是&quo ...

  3. eclipse汉化

    一.准备工作: 1.eclipse点击help——about eclipse查看软件版本,如图: 2.登录官网语言包下载地址:http://www.eclipse.org/babel/download ...

  4. IntelliJ_编译一直报错“找不到符号”

    执行maven compile时一直报错"找不到符号",类 XXX 各种clean.compile都不行 最后执行Rebuild Project一次后解决   执行rebuild ...

  5. hdu4549 矩阵快速幂 + 欧拉降幂

    R - M斐波那契数列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit  ...

  6. Oauth2.0 用Spring-security-oauth2 来实现

    前言: 要准备再次研究下 统一认证的功能了,我还是觉得实现统一认证 用Oauth2 最好了,所以,现在再次收集资料和记笔记. 正文: 一.概念理解 OAuth2, 是个授权协议, RFC文档见:htt ...

  7. [bzoj1787][Ahoi2008]紧急集合

    Description 给定一棵大小为的树,有组询问,每组询问给三个点,求到这三个点距离和最小的点及最小距离和. Input 第一行两个数. 接下来行,每行两个数表示到有一条边. 最后行,每行个数,为 ...

  8. 控制div位于最上层

    前提: 设置了position属性,且为relative,absolute或是fixed: 设置z-index,数值越大,越上,-1为最底层,99999为最顶层

  9. tomcat部署web应用的4种方法

    在Tomcat中有四种部署Web应用的方式,简要的概括分别是: (1)利用Tomcat自动部署 (2)利用控制台进行部署 (3)增加自定义的Web部署文件(%Tomcat_Home%\conf\Cat ...

  10. C# winform窗体设计-查询单个值

    查询单个值主要用于对成绩最低分,最高分,学生总数,学生性别等信息的统计 在查询单个值的时候用到了ExecuteScalar方法,连接以及语句方面,以及思路和对数据的增删改差不多 下面请看一段代码: s ...