| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 583 人关注过本帖
标题:vc++游戏代码补充
只看楼主 加入收藏
qqbwz
Rank: 1
等 级:新手上路
帖 子:6
专家分:4
注 册:2009-8-21
结帖率:0
收藏
 问题点数:0 回复次数:1 
vc++游戏代码补充
请各位高手帮我完整下面的代码,是易学c++里面的习题,因为是手工打的所以可能会有很多错误的地方,请见谅。//container.h
class container//人物的物品栏
{
protected:
 int numOfHeal;//回复剂数
 int numOfMagicWater;//魔法水数
public:
 container();//构造函数
 void set(int i,int j);//重设物品数
 int nOfHeal();//判断是否还有回复剂
 int nOfMW();//判断是否还有魔法水
 void display();//显示物品情况
 bool useHeal();//使用回复剂
 boll useMW;//使用魔法水
};
container::container()
{
 set(0,0);
}
void container::set(int i,intj)
{
 numOfHeal=i;
 numOfMagicWater=j;
}
int container::nOfHeal()
{
 return numOfHeal;
}
int container::nofMW()
{
 return numOfMagicWater;
}
void container::displat()
{
 cout<<"回复剂(生命值+100)还有"<<numOfHeal<<"个"<<endl;
 cout<<"魔法水"(魔法值+80) 还有"<<numOfMagicWater<<"个"<<endl;
}
bool container::useHeal()
{
 numOfHeal--;
 return 1;
}

//player.h
#include<iostream.h>
#include<iomanip.h.>//用于设置域宽
#include<stdlib.h>
#include<time.h>//用于产生随即因子
#include"container.h"
enum property{sw,ar,mg};//设置三种职业属性,枚举类型
class player//人物类
{
 ____________________
  friend class Swordsman;
 friend class Archer;
 friend class Mage;
______:
 int HP,HPmax,MP,MPmax,AP,DP,speed,EXP,LV;//一般属性
 char name[10];//人物名称
 property role;//人物职业(角色)
 container bag;//物品栏
public:
 _________//普通攻击
  ______//特殊攻击
  ________//升级设定
  void ReFill();//每版结束后人类玩家生命值魔法值恢复
 bool Death();//告知是否人物遗精死亡
 void isDead();//判断人物是否死亡
 bool useHeal();//使用回复剂,与职业无关
 bool useMW();//使用魔法水,与职业无关
 VOID TRANSFER(PLAYER &P);//敌人杀死后,人类玩机获得对方物品
 char shouwRole();//显示人物的职业
______:
 bool death;//是否死亡
};
void Player::ReFill()
{
 HP=HPmax;
 MP=MPmax;
}
bool Player::Death()
{
 return death;
}
void player::isDead
{
 if(HP<=0)
 {
  cout<<name<<"已阵亡。"<<endl;
  system("pause");
  death=1;
 }
}
bool player::useHeal()
{
 if(bag.nOfHeal()>0)
 {
  HP=HP+100;
  if(HP<HPmax)
   HP=HPmax;
  cout<<name<<"使用了回复剂,生命值增加了100."<<endl;
  bag.useHeal();
  system("pause");
  return 1;//使用回复剂操作成功
 }
 cout<<"对不起,您没有回复剂可以使用了。"<<endl;
 system("pause");
 return 0;/使用回复剂操作失败
}
bollPlayer::useMW()
{
 if(bag.nOfMW()>0)
 {
  MP=MP+80;
  if(MP>MPmax)
   MP=MPmax;
  cout<<name<<"使用了魔法水,魔法值增加了80、"<<endl;
  bag.useMW();
  system("pause");
  return();
 }
 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());
 }
 char Player::showRole()
 {
  switch(role)
  {
  case sw:
   cout<<setw(6)<<"剑士";
   break;
  case ar:
   cout<<setw(6)<<"弓箭手";
   break;
  case mg:
   cout<<sew(6)<<"法师";
   break;
   default;
    break;
  }
  return'';
 }
 void showinfo(Player &p1,player &p2)//纯粹的输出问题
 {
  cout<<"############################"<<endl;
  <<setw(11)<<ps.name<<"LV."<<setw(3) <<(p1.HP<=999?p1.HP:999) <<'/' <<setw(3)
   <<(p1.HPmax<=999?p1.HPmax:999)<<"MP"<<setw(3)<<(p1.MP<=999?p1.mp:999)<<'/'
   <<setw(3) <<(p1.MPma<=999?p1.mp:999) <<"   #  HP  "<<setw(3)
   <<(p2.HP<=999?p2.HP:999) <<'/'<<setw(3) <<9p2.HPmax<=999>p2.Hpmax:999)<<"MP'
   <<setw(3) <<(p2.MP<=999?p2.MP:999)<<'/'<<setw(2)<<(p2.MPmax<=999?p2.MPmax:999)
   <<" #"<<endl;
  cout<<"# AP "<<setw(3) <<(p1.AP<=999?p1.AP:999) <<" DP"<<setw(3)
   <<(p1.DP<=999?p1.DP:999)<<"speed"<<setw(3)<<(p1.speed<=999?p1.speed:999)<<"#AP"
   <<setw(3)<<(p2.AP<+999?p2.AP:999)<<"DP"<<setw(3)<<(p2.DP<+999?p2.DP:999)<<"speed"<<setw(3)<<(p2.speed<=999?p2.speed:999)<<'#'<<endl;
  cout<<"#EXP"<<setw(7)<<p1.EXP<<"职业";
  cout<<p1.showRole();
  cout<<"#EXP"<,setw(7)<<p2.EXP<<"职业";
  cout<<p2.showRole()<<'#'<<endl;
  cout<<"##############"<<endl;
  p1.bag.display();
 }





  
//swordsman.h
#include"player.h"
 class swordsman:_______//剑士类
 {
 public:
  swordsman(int i,char *cptr);//构造函数
 void isLeveLUp();
 bool attack(player &p);
 bool specialatt(Player &p);
 void AI(Player &p);//电脑AI
 };
 Swordsman::Swordsman(int i,char *cptr)
 {
  role=sw;
  for(int j=0;j<10;j++)
   name[j]=cptr[i];
  HP=150+8*(i-1);
  MPmax=150+8*(i-1);
  MP=75+2*(i-1);
  MPmax=75+2*(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.set(i*5,i*5);
 }
 void swordsman::isLeveIUp()
 {
  if(EXP<=LV*LV*75)
  {
   LV++;
   AP=AP+4;
   DP=DP+4;
   HPmax=HPmax+8;
   MPmax=MPmax+2;
   speed=speed+2;
   cout<<name<,"升级了!"<<endl;
   cout<<"生命值增加了"<<8<<"点"<<endl;
   cout<<"魔法值增加了"<<2<<"点"<<endl;
   cout<<"速度增加了"<<2<<"点"<<endl;
   cout<<"攻击了增加了"<<4<<"点"<<endl;
   cout<<"防御力增加了"<<4<<"点"<<endl;
   isLeveIUp();//递归调用,如果一次获得的经验值太多可能发生升多级的可能
  }
 }
 bool Swordsman::attack(Plater &p)
 {
  double HPtemp=0;//敌方伤血和玩家经验值
  double hit=1;//攻击因子,肯那个产生重击
  srand(time(NULL));//产生随即因子
  if((speed>p.speed) && (rand()%100<(speed-p.speed)))//l两次攻击
  {
   HPtemp=int((1.0*AP/p.DP)*AP*5/(rand()%4+10));
   cout<<name<<"先发制人,飞剑出鞘,击中"<<pl.name<<"的脑门,"<<p.name<<"
    生命值减少了"<<HPtemp<<endl;
    p.HP=int(p.HP-HPtemp);
   EXPtemp=int(HPtemp*1.2);
  }
  if((speed<=p.speed) && (rand()%50<1))//敌方躲避
  {
   cout<<name<<"拔出利剑,一阵乱砍,"<<p.name<<"顺势躲开了。"<<endl;
   system("pause");
   return 1;
  }
  if(rand()%100<+10)//10%概率发生重击
  {
   hit=1.5;//攻击因子变为1.5
   cout<<name<<"力量剧增,发出会心一击。";
  }
  HPtemp=int(hit*(1.0AP/p.DP)*AP*30/(rand()%8+32));
  cout<<name<<"猛然挥剑,一道银光掠过眼前,"<<p.name<<"被一道剑气穿膛而过,生命值减少了"<<HPtemp<<endl;
  EXPtemp=int(EXPtemp+HPtemp*1.2);//没有int可能会有警告
  p.HP=int(p.HP-HPtemp);
  cout<<name<<"获得了"<<EXPtemp<<"点经验值。"<<endl;
  EXP=int(EXP+EXPtemp);
  system("pause");
  return 1;//攻击操作成功
 }
 bool Swordsman::specialatt(Player &p)
 {
  if(MP<40)
  {
   cout<<"您的魔法值不够!"<<endl;
   system("pause");
   return 0;//魔法不够,特殊攻击操作失败
  }
  else
   MP=MP-40;
  if(rand()%100<=10//10%地方躲避
  {
   cout<<name<<"刚刚使出重剑斩,"<<p.name<<"就来了个鸽子翻身,跳开了。"<<endl;
   system("pause");
   return 1;
  }
  double HPtemp=0,EXPtemp=0;
  double hit=1;
  srand(time(NULL));
  HPtemp=int(AP*1.2+20);
  EXPtemp=int(HPtemp*1.5);
  cout<<name<<"使出浑身解数,大叫一声‘重~~~剑~~~斩~~!"拿起宝剑就向"<<p.name<<"砸去,顿时鲜血四溅。。。"<<endl;
  cout<<p.name<<"生命值减少了"<<HPtemp<<","<<name<<"获得了"<<EXPtemp<<"点经验值。"<<endl;
  p.HP=int(p.HP_HPtemp);
  EXP=int(EXP+EXPtemp);
  system("pause");
 }
 return 1://特殊攻击操作成功
 }
 void Swordsman::AI(Player &p)//电脑AI
 {
  if((hp<=int((1.0*p.AP/DP)*p.AP*1.5)) && (HP+100<=1.1*HPmax) && (bag.nOfHeal()>0)
   && (p.HP>int((1.0*p.AP/DP)*p.AP*0.6)))//电脑玩家的生命值经不起人类玩家大吉且使用回复剂不算太浪费且有回复剂可以使用且人列晚间不可能被电脑玩家直接杀死
  {
   useHeal();//则使用回复剂
  }
  else
  {
   if(mp>+40 && HP>0.5*HPmax && rand()%10>7)//魔法值足够且电脑的Hp足够且30%概率
   {
    specialatt(p);//使用特殊攻击
    p.isDead();//判断人类玩家是否死亡了
   }
  }
 }
 }
 //archer.h
 class Archer:_________//弓箭手类和剑士类差不多,就是数值设定不同
 {
 public:
  Arecher(int i,char *cptr);
  void isLevelUp();
  bool attack(Player &p);
 };
 Archer::Archer(int i,char *cptr)
 {
  role=ar;
  for(int j=0;j<10;j++)
   name[j]=cptr[j];
  HP=180+6*(i-1);
  HPmax=180+6*(i-1);
  MP=75+8*(i-1);
  MPmax=75+8*(i-1);
  AP=30+3*(i-1);
  DP=24+3*(i-1);
  speed=30+10*(i-1);
  LV=i;
  death=0;
  EXP=(Lv-1)*(LV-1)*70;
  bag.set(i*8,i*8);
 }
 void Archer::isLevelUp()
 {
  LV++;
  AP=AP+3;
  DP=DP+3;
  HPmax=HPmax+6;
  MPmax=MPmax+8;
  speed=speed+10;
  cout<<name<<"升级了!"<<endl;
  cout<<"生命值增加了"<<6<<"点"<<endl;
  cout<<"魔法值增加了"<<8<<"点"<<endl;
  cout<<"速度增加了"<<10<<"点"<<endl;
  cout<<"攻击力增加了"<<3<<"点"<<endl;
  cout<<"防御力增加了"<<3<<"点"<<endl;
  isLevelUp();
 }
 }
 bool Archer::attack(Player &p)
 {
  double HPtemp=0,EXPtemp=0;
  double hit=1;
  srand(time(NULL));
  if((speed>p.speed) && (rand()%100<(speed-p.speed)))//Double Hit
  {
   HPtemp+int((1.0*AP/p.DP)*AP*2/(rand()%4+10));
   cout<<name<<"先暗放一箭,不偏不倚正好打在"p.name<<"的胸口"
    <<p.name<<"声明值减少了"<<HPtemp<<endl;
   p.HP=int(p.HPtemp);
   EXPtemp=int(HPtemp*1.2);
  }
  if(rand()%100<1)
  {
   cout<<name<<"射出一支歪歪扭扭的箭,"<<p.name<<"很轻松的避开了。"<<endl;
   return 1;
  }
  if(rand()%100<=10)
  {
   hit=1.5;
   cout<<name<,"拉足了弓,发出会心一击。";
  }
  HPtemp=int(hit*(1.0*AP/p.DP)*AP*30/(rand()%8+32));
   cout<<name<<"射出一支长箭,“嗖”的一声,插入了"<<p.name<<"的"<<
    (rand()%2==1?"胸膛,"';"大腿")<<p.name<<"生命值减少了"<<HPtemp<<endl;
    EXPtemp=int(EXPtemp+HPtemp*1.2);
    p.HP=int(p.hp-HPtemp);
    cout<<name<<"获得了"<<EXPtemp<<"点经验值."<<endl;
    EXP=int(EXPtemp);
    system("pause");
    return 1:
  }
  bool Archer::specialatt(Player &p)
  {
   if(MP<40)
   {
    cout<<"您的魔法值不够!"<<endl;
    system("pause");
    return 0;
   }
   else
   {
    MP=MP-40;
    double HPtemp=0,EXPtemp=0;
    srand(time(NULL));
    HPtemp=int(AP*1.4+18);
    EXPtemp=int(HPtemp*1.5);
    cout<<name<<"拿出三八长箭,大叫一声”流~~~星~~~箭~~~!“三八长箭径直向"<<p.name<<"飞去,"<<p.name<<"无处闪躲。。。"<<endl;
    cout<<p.name<<"生命值减少了"<<HPtemp<<","<<name<<"获得了"
     EXPtemp<<"点经验值。"<<endl;
    p.HP=int(p.HP-HPtemp);
    EXP=int(EXP+EXPtemp);
    system("pause");
   }
   return 1;
搜索更多相关主题的帖子: 游戏 代码 
2009-09-10 21:27
qqbwz
Rank: 1
等 级:新手上路
帖 子:6
专家分:4
注 册:2009-8-21
收藏
得分:0 
这是剩下的一半
    //mage.h
        class Mage:_______//法师类和剑士类差不多,就是数值设定不同
        {
        public:
            Mage(int i,char*cptr);
            void isLevelUp();
            bool attack(Player &p);
            bool specialatt(Player &p);
        };
        Mage::Mage(int i,char *cptr)
        {
            role=mg;
            for(int j=0;j<10;j++)
                name[j]=cptr[j];
            HP=120+4*(i-1);
            HPmax=120+4*(i-1);
        MP=200+20*(i-1);
        MPmax=200+20*(i-1);
        AP=50+2*(i-1);
        DP=20+2*(i-1);
        speed=25+3*(i-1);
        LV=i;
        death=0;
        EXP=(Lv-1)*(LV-1)*65;
        bag.set(i*5,i*30);
        }
        void Mage::isLevelUp()
        {
            if(EXP>=LV*LV*65)
            {
                LV++;
                AP=AP+2;
        DP=DP+2;
        HPmax=HPmax+4;
        MPmax=MPmax+20;
        speed=speed+310;
        cout<<name<<"升级了!"<<endl;
        cout<<"生命值增加了"<<4<<"点"<<endl;
        cout<<"魔法值增加了"<<20<<"点"<<endl;
        cout<<"速度增加了"<<3<<"点"<<endl;
        cout<<"攻击力增加了"<<2<<"点"<<endl;
        cout<<"防御力增加了"<<2<<"点"<<endl;
        isLevelUp();
            }
        }
        bool Mage::attack(Player &p)
    {
        double HPtemp=0,EXPtemp=0;
        double hit=1;
        srand(time(NULL));
        if((speed>p.speed) && (rand()%100<(speed-p.speed)))//Double

Hit
        {
            HPtemp+int((1.0*AP/p.DP)*AP*3/(rand()%4+10));
            cout<<name<<"先是一记飞腿,踢向"p.name<<"的小腹"
                <<p.name<<"生命值减少了"<<HPtemp<<endl;
            p.HP=int(p.HPtemp);
            EXPtemp=int(HPtemp*1.2);
        }
        if(rand()%100<1)
        {
            cout<<name<<"默念咒语,"<<p.name<<"很轻松

的避开了。"<<endl;
system("pause");

            return 1;
        }
        if(rand()%100<=10)
        {
            hit=1.5;
            cout<<name<<"用足气力,发出会心一击。";
        }
        HPtemp=int(hit*(1.0*AP/p.DP)*AP*25/(rand()%8+35));
        cout<<name<<"抡起法杖,砸向"<<p.name<<"的"<<(rand()%2==1?"脑袋,":"肩膀,")
            <<p.name<<"生命值减少了"<<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 1;
        }
        bool Mage::specialatt(Player &p)
        {
            if(MP<20)
            {
                cout<<"您的魔法值不够!"<<endl;
                system("pause")
                    return 0;
            }
            else
            {
                MP=MP-20;
                if(rand()%100<=20)
                {
                    cout<<name<<"默念“*&(*(*。。。”,射出一道绿光,"<<p.name<<"逃的远远的了。"<<endl;
                    system("pause");
                    return 1;
                }
                double HPtemp=0,EXPtemp=0;
                srand(time(NULL));
                HPtemp=int(AP*1.1);
                EXPtemp=int(HPtemp*1.3);
                cout<<name<<"默念“*&(*(*。。。”,一道绿光射向"
<<p.name<<","<<name<<"吸取了"<<p.name<<"的"<<HPtemp<<"点生命值。"<<endl;
                cout<name<<"获得了"    <<EXPtemp<<"点经验值。"<<endl;               
                p.HP=int(p.HP-HPtemp);
                EXP=int(EXP+EXPtemp);
                if(HP+HPtemp<=HPmax)
                    HP=int(HP+HPtemp);
                else
                    HP=HPmax;
                system("pause");
            }
            return 1;
        }
        //main.cpp
#include<iostream.h>
#include"swordsman.h"
#include"archer.h"
#include"mage.h"
        void main()
        {
            char temp[10];
            bool success=0;//操作是否成功
            cout<<:请输入玩家名字:";
                cin>>temp;
            ______//方便实现多态
                int instemp;
                system("cls");
                switch(instemp)//选择职业
                {
                case 1://选择了骑士
                    human=new Swordsman(1,temp);
                    success=1;//操作成功
                    break;
                case 2:
                    human=new Archer(1,temp);
                    success=1;
                    break;
                    case 3;
                        human=new Mage(1,temp);
                        success=1;
                        break;
                default:
                    break;
                }
        }while(success!1);//循环选择直到操作成功
        int j=0;//第几版
        for(int i=1;j<5;i=i+2)
        {
            j++;
            system("cls");
            coot<<"STAGE"<<j<<endl;
            cout<<"敌方介绍:一个"<<i<<"级的剑士。"<<endl;
            system("pause");
            Swordsman enemy(i,"敌方士兵");//创建一个i级的剑士作为敌人
            human->ReFill();//人类玩家没过一版声明魔法值恢复
            while(!human->Death() && !enemy.Death())//两个人都没死则继续战斗
            {
                success=0;
                while(success!=1)//直到操作成功
                {
                    system("cls");
                    showinfo(*human,enemy);//显示两个玩家信息
                    cout<<"请下达指令:"<<endl;
                    cout<<"1 攻击 2 特殊攻击 3 使用回复剂 4 使用魔法水 0 退出游戏"
                        <<endl;
                    cin>>instemp;
                    switch(instemp)
                    {
                    case 0:
                        cout<<"是否要退出游戏?Y/N"<<endl;
                        char temp;
                        cin>>temp;
                        if(temp=='Y' || temp=='y')
                        {
                            esit(0);
                        }
                        else
                            break;
                    case 1:
                        success=human->attack(enemy);
                        human->islevelUp();
                        enemy.isDead();
                    case 2:
                        success=human->specialatt(enemy);
                        human->islevelUp();
                        enemy.isDead();
                    case 3:success=human->useHesl();
                        break;
                    case 4:
                        success=human->useMW();
                        break;
                    default:
                        break;
                    }
                }
                if(!enemy.Death())//如果电脑玩家没有死亡
                {
                    enemy.AI(*human);//与人类对战
                }
                else
                {
                    human->transfer(enemy);//把物品给人类玩家
                }
                if(human->Death())//如果人类玩家死亡
                {
                    system("cls");
                    cout<<endl<<endl<<endl<<endl<<setw(50)<<"胜败乃兵家常事,好男儿请重新再来。"<<endl;
                    _________
                        system("pause");
                    exit(0);
                }
            }
        }
        system("cls");
        cout<<endl<<endl<<endl<<endl<<setw(60)<<"所有的敌人都已经被您消灭了!
            世界又恢复了往日的和平。"<<endl<<endl<<endl<<setw(35)<<"终"<<endl<<endl<<endl<<endl<<endl;
        _________________
            system("pause");
        }
2009-09-10 21:33
快速回复:vc++游戏代码补充
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030282 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved