“你对未来的规划是什么?”
“单纯的说,追求科学严谨的东西。”
“看来你还是很迷啊。”
不积跬步无以至千里,没有兴趣怎能下苦工
玩的一款网页游戏,简单的模拟一下战斗,并加深一下语言,纯粹的探索。
/*模拟《武林英雄》的切磋与c++类的基础*/
#include <iostream>
#include <time.h> //需要使用time();
#include <stdlib.h> //需要使用srand()与rand()
#include <unistd.h> //需要使用sleep()
using namespace std;
int speedN[8]={4,8,10,12,14,16,18,20}; //角色的攻速
class sman{
private: string name;
int life;
int speed;
bool isAlive; //保留
public: sman(string s) //构造函数
{
static int count=1;
srand((int)time(0));
cout<<"Time:"<<(int)time(0)<<endl;
name=s;
life=100;
isAlive=true;
speed = speedN[rand()%8];
cout<<"rand:"<<rand()<<endl;
cout<<"第"<<count<<"次创建对象!"<<endl;
count++;
}
//拷贝构造
sman(const sman& source)
{
srand((int)time(0));
this->name = source.name;
this->name = this->name.insert(0,"~"); //角色名前加“~”,区别原对象
this->life = source.life;
this->speed = source.speed;
this->isAlive = 1;
}
//析构
~sman()
{
cout<<this->name<<"进行了自我销毁!"<<endl;
}
//打印角色属性
void show(void)
{
cout<<"该角色的属性如下:"<<endl;
cout<<"角色名:"<<this->name<<endl;
cout<<"攻击速度:"<<this->speed<<endl;
cout<<"生命值:"<<this->life<<endl;
cout<<"状态:"<<this->isAlive<<endl;
}
//与另一对象的副本切磋
void justPk(sman role)
{
cout<<"双方已各就各位,裁判一声令下!"<<endl;
int da;
int db;
srand((int)time(0));
int GameState=1;
int Seconds=0;
while(GameState)
{
if(!Seconds)
cout<<"战斗开始了!"<<endl;
if(!(Seconds%this->speed) && (Seconds > 0))
{
cout<<"第"<<Seconds<<"秒,";
da = rand()% this->speed+this->speed; //伤害计算
role.life -= da;
cout<<this->name<<"抓住了"<<role.name<<"的破绽,使出了他的还我漂漂拳,对"
<<role.name<<"造成了"<<da<<"点伤害!"<<endl;
if(role.life <= 0)
{
role.life=100;
cout<<role.name<<"在"<<this->name<<"的攻击下狼狈不堪,已经有气无力!"<<endl;
cout<<"恭喜"<<this->name<<"获得了战斗的胜利!"<<endl;
break;
}
}
if(!(Seconds%role.speed) && (Seconds > 0))
{
cout<<"第"<<Seconds<<"秒,";
db = rand()% role.speed+role.speed;
this->life -= db;
cout<<role.name<<"一个无意识的出脚突然踢在了"
<< this->name <<"的身上,对"<<this->name<<"造成了"<<db<<
"点伤害,打得"<<this->name<<"一个踉跄!"<<endl;
if(this->life <=0)
{
this->life = 100;
cout<<this->name<<"在"<<role.name<<"的攻击下已经无法走动!"<<endl;
cout<<"恭喜"<<role.name<<"获得了战斗的胜利!"<<endl;
break;
}
}
sleep(1);
Seconds++;
}
}
};
int main(void)
{
sman frog("frog");
sleep(2); //延时两秒,避免生成同样攻速的角色
sman king("king");
frog.show();
king.show();
frog.justPk(king);
frog.justPk(frog);
king.justPk(frog);
return 0;
}