RPG游戏(C++)

作者:无*
时间:2019-4-18

几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。本次上机要求编写一个简化的创建游戏角色的程序。
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
 名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
 本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职       业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
 生命值=体力*20。
 魔法值=(智力+智慧)*10。

一.题目分析:

1.本次上机要求编写一个简化的创建游戏角色的程序,主要是实现面向对象的编程。主要的算法就是类及其相关应用。最后还要实现文件的写入。此程序准备利用c++来实现面向对象的编程。
2.从整体的需求分析来看,每个类里要有相关的成员和成员函数;避免只建一个类来实现全部功能(效率低)。
3.还要注意种族的选择会影响职业的选择;其余属性会用到随机函数。再利用式子计算出生命值和魔法值。最后再实现所有属性的打印输出功能。

二.算法构造

1.先建立一个基类class game{};,记录姓名和性别;并写成员函数show()实现姓名输入和性别的选择。
2.再建立一个派生类class game2:public game{},记录种族和职业;并写成员函数Rrace()实现种族和职业的选择。
3.定义种族类的派生类,生成随机属性class Attribute :public game2{};类中包括取决于职业的属性确定函数output_attribute()和随机生成数函数rad(int rand1, int rand2, int rand3, int rand4, int rand5)。
4.主函数建立一个Attribute类的对象,通过 a1.show();a1.Rrace(); 选择姓名性别种族职业。a1.output_attribute()来输出。最后有一个选择判断用户是否对所创建的游戏满意,若满意则存入文件,若不满意,则重新创建

源代码:

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<string>
#include<fstream>
using namespace std;
//先建立基类,记录姓名性别
class game
{   public:
    char name[100];              //定义姓名
    string sex;                  //定义性别
	int sex_choice;              //整型数据对性别进行选择
	void show();           
};
void game::show()
{ 
    cout << "请输入您所要创建的角色姓名:";
    cin >> name;
int tag=1;
while (tag)
	{
		cout << "请选择角色性别:(0.男 1.女):"<< endl;;
        cin >> sex_choice;
		switch (sex_choice)
		{
		case 0:sex = "男"; tag = 0; break;
        case 1:sex = "女"; tag= 0; break;
        default:cout << "输入错误,请重新输入" << endl; break;
		}
	}
}
//game2类记录种族和职业

class game2:public game            
{public:
string race;                                   //定义种族
string occupation;                             //定义职业
int race_choice;                              //种族选择
int occupation_choice;                         //职业选择
void Rrace();
};
void game2::Rrace()            //成员函数选择种族和职业      
{   
int tag = 1;
while (tag)
{
cout << "请选择种族:0.人类 1.精灵 2.兽人 3.矮人 4.元素" << endl;
cin >> race_choice;
switch (race_choice)
{
case 0:race = "人类"; tag= 0; break;
case 1:race = "精灵"; tag = 0; break;
case 2:race = "兽人"; tag= 0; break;
case 3:race = "矮人"; tag = 0; break;
case 4:race = "元素"; tag = 0; break;
default:cout << "输入错误,请重新输入!" << endl; break;
}
}
while (1)
{
switch (race_choice)
{
case 1: cout << "选择您的职业:0.狂战士 1.圣骑士 2.刺客 3.猎手 4.祭司 5.巫师" << endl; break;
case 2: cout << "选择您的职业:2.刺客 3.猎手 4.祭司 5.巫师" << endl; break;
case 3: cout << "选择您的职业:0.狂战士 3.猎手 4.祭司 " << endl; break;
case 4: cout << "选择您的职业:0.狂战士 1.圣骑士 4.祭司 " << endl; break;
case 5: cout << "选择您的职业:4.祭司 5.巫师" << endl; break;
}
cin >> occupation_choice;
if (race_choice == 0 && (occupation_choice >= 0&& occupation_choice <= 5)) break;
else if (race_choice == 1&& (occupation_choice > 1 && occupation_choice < 6)) break;
else if (race_choice == 2 && (occupation_choice == 0 || occupation_choice == 3 || occupation_choice == 4)) break;
else if (race_choice == 3 && (occupation_choice == 0 || occupation_choice == 1 || occupation_choice == 4)) break;
else if (race_choice == 4 && (occupation_choice > 3 && occupation_choice < 6)) break;
else cout << "输入错误,请重新输入" << endl;
}
if (occupation_choice == 0) occupation = "狂战士";
if (occupation_choice == 1) occupation = "圣骑士";
if (occupation_choice == 2) occupation = "刺客";
if (occupation_choice == 3) occupation = "猎手";
if (occupation_choice == 4) occupation = "祭司";
if (occupation_choice == 5) occupation = "巫师";
}


class Attribute :public  game2//定义种族类的派生类,生成随机属性
{
public:
	int strength;//力量
	int agility;//敏捷
	int physical;//体力
	int intelligence;//智力
	int wisdom;//智慧
	int HP;//生命值
	int MP;//魔法值
	void output_attribute();//取决于职业的属性确定函数
	void rad(int rand1, int rand2, int rand3, int rand4, int rand5);//随机生成数函数
};

void Attribute::rad(int rand1, int rand2, int rand3, int rand4, int rand5)
{
	int sum;
	srand((unsigned)time(NULL));               //随机属性值
	do
	{
		strength= rand() % 10 + rand1;
		agility = rand() % 10 + rand2;
		physical = rand() % 10 + rand3;
		intelligence = rand() % 10 + rand4;
		wisdom = rand() % 10 + rand5;
		sum = strength + agility + physical + intelligence + wisdom;
	} while (sum != 100);//5项属性总值为100
	HP = physical * 20;//生命值为体力的20倍
	MP = (wisdom + intelligence) * 10;//魔法值为智力与智慧的10倍
 
}
void Attribute::output_attribute()           //初始属性
{
	if (occupation == "狂战士") rad(35, 15, 25, 5, 5);
	if (occupation == "圣骑士") rad(20, 10, 25, 15, 15);
	if (occupation == "刺客") rad(15, 30, 15, 10, 15);
	if (occupation == "猎手") rad(10, 35, 10, 5, 25);
	if (occupation == "祭司") rad(10, 25, 10, 30, 20);
	if (occupation == "巫师") rad(5, 15, 5, 15, 45);
}

  	
 int main()     
 {   int sign=1;
	Attribute a1;                      //建立对象
	while(sign)
	{	
	srand((unsigned)time(NULL));  //随机数
    
	a1.show();                  //调用选择姓名性别函数
	a1.Rrace();                 //调用选择种族职业函数
	a1.output_attribute();      //输出函数
     cout<<endl;
	cout<<"名字\t"<<a1.name<<endl;
     cout<<endl;
	cout<<"性别\t"<<a1.sex<<endl;
	 cout<<endl;
	cout<<"种族\t"<<a1.race<<endl;
	 cout<<endl;
	cout<<"职业\t"<<a1.occupation<<endl;
	 cout<<endl;
    cout<<"力量\t"<<a1.strength<<endl;
	cout<<endl;
	cout<<"敏捷\t"<<a1.agility<<endl;
	cout<<endl;
	cout<<"体力\t"<<a1.physical<<endl;
	cout<<endl;
	cout<<"智力\t"<<a1.intelligence<<endl;
	cout<<endl;
	cout<<"智慧\t"<<a1.wisdom<<endl;
	cout<<endl;
	cout<<"生命值\t"<<a1.HP<<endl;
	cout<<endl;
	cout<<"法力值\t"<<a1.MP<<endl;
	cout<<endl;

	 int choose;
	 cout<<"是否满意所创建的角色,0满意,1不满意:";
	 cin>>choose;
	 if(choose==0)                      //写入文本
	 {  sign=0;
		 ofstream outfile;   
  	outfile.open("save.txt");         //打开文件
  	outfile<< "姓名:" << a1.name << endl;
  	outfile<< "性别:" << a1.sex << endl;
  	outfile<< "种族:" << a1.race << endl;
  	outfile<< "职业:" << a1.occupation << endl;
  	outfile<< "力量:" << a1.strength << endl;
  	outfile<< "敏捷:" << a1.agility << endl;
  	outfile<< "体力:" << a1.physical << endl;
  	outfile<< "智力:" << a1.intelligence << endl;
  	outfile<< "智慧:" << a1.wisdom << endl;
  	outfile<< "生命值:" << a1.HP << endl;
  	outfile<< "法力值:" << a1.MP << endl;
    outfile.close();                            //关闭文件
	 }
	 else sign=1;                                    //循环标志
	}
	return 0;
}

三.类图设计

在这里插入图片描述

四.调试测试,及运行结果

1.调试截图:
在这里插入图片描述
2.测试截图
(1)(选择名字和性别)测试代码和结果:

class game
{   public:
    char name[100];              //定义姓名
    string sex;                  //定义性别
	int sex_choice;              //整型数据对性别进行选择
	void show();           
};
void game::show()
{ 
    cout << "请输入您所要创建的角色姓名:";
    cin >> name;
int tag=1;
while (tag)
	{
		cout << "请选择角色性别:(0.男 1.女):"<< endl;;
        cin >> sex_choice;
		switch (sex_choice)
		{
		case 0:sex = "男"; tag = 0; break;
        case 1:sex = "女"; tag= 0; break;
        default:cout << "输入错误,请重新输入" << endl; break;
		}
	}
}
int main()     
 {   int sign=1;
	game a1;                      //建立对象
	a1.show();                  //调用选择姓名性别函数
    cout<<endl;
	cout<<"名字\t"<<a1.name<<endl;
     cout<<endl;
	cout<<"性别\t"<<a1.sex<<endl;
}

在这里插入图片描述
(2)(选择职业和种族)测试代码和结果:
在这里插入图片描述
(3)输出属性截图
在这里插入图片描述
(4)写入文本文件截图

 int choose;
 cout<<"是否满意所创建的角色,0满意,1不满意:";
 cin>>choose;
 if(choose==0)                      //写入文本
 {  sign=0;
	 ofstream outfile;   
    outfile.open("save.txt");         //打开文件
    outfile<< "姓名:" << a1.name << endl;
    outfile<< "性别:" << a1.sex << endl;
    outfile<< "种族:" << a1.race << endl;
    outfile<< "职业:" << a1.occupation << endl;
    outfile<< "力量:" << a1.strength << endl;
    outfile<< "敏捷:" << a1.agility << endl;
    outfile<< "体力:" << a1.physical << endl;
    outfile<< "智力:" << a1.intelligence << endl;
    outfile<< "智慧:" << a1.wisdom << endl;
    outfile<< "生命值:" << a1.HP << endl;
    outfile<< "法力值:" << a1.MP << endl;
   outfile.close();                            //关闭文件
  }
  else sign=1;   

在这里插入图片描述
3.运行结果截图:
(1)如果用户对创建角色不满意可重新进行角色创建
在这里插入图片描述
(2)如果用户满意的话则程序结束
在这里插入图片描述

五.总结和归纳:

**主要知识:
1.类的创建和派生类的应用,类和对象的使用,相关函数的调用 class Game//类名为Game,一般类名的第一个字母大写,用来和变量明区别 ; { public:
/表示函数的访问权限为公有,既可以被类中成员函数调用,也可以被类作用域中的其他函数调用 ,在类外可以被访问,还可以在类的定义中被赋值;/
int num;//声明公有数据成员num; char name[20];//声明公有数据成员name; void
show()//声明公有成员函数 { //可以直接写在类中,也可在在类外定义; } } Game g1;
----建立对象
2.随机生成数函数的应用 srand((unsigned)time(NULL)); //随机数 rand()函数
3.将结果保存到文本文件 ofstream outfile; outfile.open(“save.txt”); //打开文件 … outfile.close(); //关闭文件

心得:此次程序要求实现面向对象的编程,通过RPG游戏人物的创建掌握面向对象设计的七大原则,掌握常见的设计模式以及类图的描述。开始对于c++有些知识存在遗忘现象,比如结果保存到文本文件中的操作。通过查阅资料,完成了基本程序的编写。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值