rpg游戏角色设计

第四次上机

  • 题目分析:
  1. 原题:为(一种源自《龙与地下城》的游戏类型)在进入游戏时做一个让用户自己来创建自己喜欢的角色RPG游戏。本次上机要求编写一个简化的创建游戏角色的程序。
  2. 分析:
  1. 创建一个角色类,包含角色的各个属性
  2. 在类中定义各个属性的输入函数,且所有职业属性单独用一个函数输入。
  3. 对职业的各个属性,进行判断是否符合种族,若不符合,重新输入。
  4. 定义一个输出函数
  5. 定义一个保存文件函数
  6. 在类外定义一个界面函数,在函数中生成角色对象,并判断,是否满意,若满意则退出,否则就重新输入。
  • 类图设计

  • 源代码:

#include<iostream>

using namespace std;

#include<string.h>

#include<string>

#include<math.h>

#include<stdlib.h>

#include<time.h>

#include<fstream>

//角色类

class role

{

public:

       string name;//姓名

       string sex;//性别

       string race;//种族

       string profession; //职业

       int power;//力量

       int agility; //敏捷

       int tili;//体力

       int  brains;//智力

       int wit;//智慧

       int smingz; //生命值

       int mana;//法力值

       void setname();

       void setsex();

       void setrace();

       void setprofession();

       void setattribute();

       void check();

       void display();

       void save();

 } ;

 //输入名字

 void role::setname()

 {

      cout<<"输入你游戏角色的姓名:";

      cin>>name;

 }

 //输入性别

 void role::setsex()

 {

      int p;

      cout<<"选择你游戏角色的性别(0:男性 ,1:女性)";

      cin>>p;

      if(p==0)

      sex="男";

      else

      sex="女";

 }

 //输入种族

 void role::setrace()

 {

      int p;

      cout<<"选择你游戏角色的种族(0:人类,1:精灵,2:兽人,3:矮人,4:元素)";

      cin>>p;

      if(p==0)

      race="人类";

      else if(p==1)

      race="精灵";

      else if(p==2)

      race="兽人";

      else if(p==3)

      race="矮人";

      else

      race="元素";

 }

 //输入职业

 void role::setprofession()

 {

      int p=0;

      cout<<"选择你游戏角色的职业(0:狂战士,1:圣骑士,2:刺客,3:猎手,4:祭司,5:巫师)";

      cin>>p;

      if(p==0)

      profession="狂战士";

      else if(p==1)

      profession="圣骑士";

      else if(p==2)

      profession="刺客";

else if(p==3)

profession="猎手";

else if(p==4)

profession="祭司";

else

profession="巫师";

       

 }

 //输入属性

 void role::setattribute()

 {

       srand( (unsigned)time( NULL ) );

      if(profession=="狂战士")

      {

            power=rand()%2+39;

            agility=rand()%2+18;

       tili=rand()%2+29;

       brains=rand()%2+3;

       wit=100-power-agility-tili-brains;

       smingz=tili*20;

       mana=(brains+wit)*10;

}

if(profession=="圣骑士")

{

       power=rand()%2+24;

            agility=rand()%2+13;

       tili=rand()%2+29;

       brains=rand()%2+18;

       wit=100-power-agility-tili-brains;

       smingz=tili*20;

       mana=(brains+wit)*10;

}

if(profession=="刺客")

{

     power=rand()%2+19;

            agility=rand()%2+33;

       tili=rand()%2+18;

       brains=rand()%2+13;

       wit=100-power-agility-tili-brains;

       smingz=tili*20;

       mana=(brains+wit)*10;

      }

      if(profession=="猎手")

 {

     power=rand()%2+14;

            agility=rand()%2+38;

       tili=rand()%2+14;

       brains=rand()%2+8;

       wit=100-power-agility-tili-brains;

       smingz=tili*20;

       mana=(brains+wit)*10;

  }

  if(profession=="祭司")

  {

    power=rand()%2+14;

            agility=rand()%2+18;

       tili=rand()%2+14;

       brains=rand()%2+33;

       wit=100-power-agility-tili-brains;

       smingz=tili*20;

       mana=(brains+wit)*10;

   }

   if(profession=="巫师")

   {

   power=rand()%2+9;

            agility=rand()%2+18;

       tili=rand()%2+9;

       brains=rand()%2+18;

       wit=100-power-agility-tili-brains;

       smingz=tili*20;

       mana=(brains+wit)*10;

   }

}

//判断属性是否正常

 void role::check()

 {

      setprofession();

      int f=0;

      while(f==0){

      if(race=="精灵"&&(profession=="狂战士"||profession=="圣骑士"))

      setprofession();

      else

 break;

}    

while(f==0){

      if(race=="兽人"&&(profession=="圣骑士"||profession=="刺客"||profession=="巫师"))

      setprofession();

      else

      break;

      }

      while(f==0){

 if(race=="矮人"&&(profession=="刺客"||profession=="猎手"||profession=="巫师"))

      setprofession();

      else

      break;

 }

      while(f==0){

 if(race=="元素"&&profession!="祭司")

setprofession();

else

break;

 }

 

 }

 //输出

 void role::display()

 {

      cout<<"----------------------------------------------"<<endl;

      cout<<"姓名为:"<<name<<endl;

      cout<<"----------------------------------------------"<<endl;

      cout<<"性别为:"<<sex<<endl;

      cout<<"----------------------------------------------"<<endl;

      cout<<"种族为:"<<race<<endl;

      cout<<"----------------------------------------------"<<endl;

      cout<<"职业为:"<<profession<<endl;

      cout<<"----------------------------------------------"<<endl;

      cout<<"力量:"<<power<<endl;

      cout<<"----------------------------------------------"<<endl;

      cout<<"敏捷:"<<agility<<endl;

      cout<<"----------------------------------------------"<<endl;

      cout<<"体力:"<<tili<<endl;

      cout<<"----------------------------------------------"<<endl;

      cout<<"智力:"<<brains<<endl;

cout<<"----------------------------------------------"<<endl;

cout<<"智慧:"<<wit<<endl;

cout<<"----------------------------------------------"<<endl;

cout<<"生命值:"<<smingz<<endl;

cout<<"----------------------------------------------"<<endl;

cout<<"法力值:"<<mana<<endl; 

 }

 //将角色存入文件

 void role::save()

 {

      ofstream out;

      out.open("f1.txt",ios::trunc);

      out<<name;

out<<sex;//性别

out<< race;//种族

out<< profession; //职业

out<< power;//力量

out<< agility; //敏捷

out<< tili;//体力

out<<  brains;//智力

out<< wit;//智慧

out<< smingz; //生命值

out<< mana;//法力值

      out.close();

 }

 //界面

void  menu()

{

role r;

r.setname();

r.setsex();

r.setrace();

r.check();

r.setattribute();

r.display();  

r.save();

}

int main()

{

int x=0;

while(1)

{

       menu();

       cout<<"是否满意(1/满意,2/不满意)"<<endl;

       cin>>x;

       if(x==1)

       break;

}

 

return 0;

}

  • 运行,测试截图
  1. 随机生成属性值测试:

发现,属性值相加为100,且各个属性值和标准值都吻合的很好,职业并且匹配了种族。

  1. 文件存储的测试:

发现文件存储和输入一致:

  1. 运行结果:

 

  • 经验总结:
  1. 加深了我对文件的理解,将数据存入文件中
  2. 对uml类图有了基本的了解,类图分三层,第一层显示类的名称,如果是抽象类,则就用斜体显示。第二层是类的特性,通常就是字段和属性。第三层是类的操作,通常是方法或行为。前面的符号,+ 表示public,- 表示private,# 表示protected。  
  3. 对面向对象设计的7项基本原则有了初步的了解,

特别是单一对象原则和开闭原则。

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值