资源简介
学了一个学期,自己写的一个电子宠物小程序,初次发表,代码简单,适合初学c++的同志参考。
代码片段和文件信息
#include
#include
#include
using namespace std;
class Pet
{
public:
void change_with_time(int w); //随时间变化函数
void display();
void eat();
void drink();
void doctor();
void touch(int w);
void play(int w);
void named();
int emg();
virtual int get_life(); //提取寿命值(虚函数)
virtual void action(int act int w) = 0; //主人行为函数(纯虚)
virtual void show()=0; // 显示宠物图像
Pet(int a = 0 int hy = 0 int th = 0 int m = 0 int he = 0 int sp = 20)
{
age = a;
hungry_index = hy;
thirsty_index = th;
health_index = he;
mood_index = m;
step_points = sp;
}
void get_named()
{
cout << name;
}
int get_step_points()
{
return step_points;
}
int get_health_index()
{
return health_index;
}
int get_mood_index()
{
return mood_index;
}
int get_thirsty_index()
{
return thirsty_index;
}
int get_hungry_index()
{
return hungry_index;
}
void reduce()
{
hungry_index++;
thirsty_index++;
}
void add_step_points()
{
step_points += 5;
}
private:
int age; //年龄
string name; //名字
int hungry_index; //饥饿度
int thirsty_index; //口渴度
int mood_index; //心情
int health_index; //健康度
int step_points; //行动点数
};
int Pet::get_life()
{
return age;
}
void Pet::play(int w)
{
switch (w)
{
case 0:
mood_index += 2;
break;
case 1:
mood_index += 1;
break;
case 2:
hungry_index += 1;
thirsty_index += 1;
health_index += 1;
break;
case 3:
hungry_index += 2;
thirsty_index += 1;
health_index += 1;
break;
case 4:
hungry_index += 1;
thirsty_index += 1;
health_index += 1;
break;
default:
break;
}
step_points -= 8;
}
//抚摸函数
void Pet::touch(int w)
{
switch (w)
{
case 0:
mood_index += 2;
break;
case 1:
case 2:
mood_index += 1;
break;
case 3:
thirsty_index += 1;
break;
case 4:
mood_index -= 1;
hungry_index += 1;
break;
default:
break;
}
step_points -= 3;
}
//展示宠物状态
void Pet::display()
{
cout << “---------------------------------------------------------------------------------“ << endl;
cout << “您的宠物现在状态:“;
cout << “ 饥饿度:“ << hungry_index << “ 口渴度:“ << thirsty_index << “ 心情:“ << mood_index << “ 健康度:“ << health_index << endl;
cout << “---------------------------------------------------------------------------------“ << endl;
cout << “ 您的行动点数:“ << step_points << endl;
system(“pause“);
}
//特殊状态
int Pet::emg()
{
int x;
srand((int)time(0));
x = rand() % 21;
if (x == 21)
{
cout << “您的宠物生病了!“ << endl;
health_index -=2;
}
if (get_hungry_index() == 3)
{
mood_index -= 1;
cout << “饿了“ << endl;
}
else if (get_hungry_index() == -2)
{
mood_index -= 1;
cout << “很饱了“ << endl;
}
else if (get_hungry_index() > 3)
{
mood_index -= 1;
cout << “要饿死了“ << endl;
}
else if (get_hungry_ind
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-03-23 10:57 pet-li\
目录 0 2017-03-23 10:57 pet-li\Debug\
文件 97792 2017-03-23 10:28 pet-li\Debug\pet-li.exe
文件 1087092 2017-03-23 10:28 pet-li\Debug\pet-li.ilk
文件 1575936 2017-03-23 10:28 pet-li\Debug\pet-li.pdb
目录 0 2017-03-23 10:57 pet-li\pet-li\
文件 7274496 2017-03-23 10:51 pet-li\pet-li.sdf
文件 964 2017-03-20 08:22 pet-li\pet-li.sln
文件 22528 2017-03-23 10:51 pet-li\pet-li.v12.suo
目录 0 2017-03-23 10:57 pet-li\pet-li\Debug\
文件 1997 2017-03-23 10:28 pet-li\pet-li\Debug\pet-li.log
文件 274954 2017-03-23 10:28 pet-li\pet-li\Debug\pet-li.obj
目录 0 2017-03-23 10:57 pet-li\pet-li\Debug\pet-li.tlog\
文件 582 2017-03-23 10:28 pet-li\pet-li\Debug\pet-li.tlog\cl.command.1.tlog
文件 5712 2017-03-23 10:28 pet-li\pet-li\Debug\pet-li.tlog\CL.read.1.tlog
文件 356 2017-03-23 10:28 pet-li\pet-li\Debug\pet-li.tlog\CL.write.1.tlog
文件 1030 2017-03-23 10:28 pet-li\pet-li\Debug\pet-li.tlog\li
文件 2262 2017-03-23 10:28 pet-li\pet-li\Debug\pet-li.tlog\li
文件 330 2017-03-23 10:28 pet-li\pet-li\Debug\pet-li.tlog\li
文件 154 2017-03-23 10:28 pet-li\pet-li\Debug\pet-li.tlog\pet-li.lastbuildstate
文件 265216 2017-03-23 10:28 pet-li\pet-li\Debug\vc120.idb
文件 389120 2017-03-23 10:28 pet-li\pet-li\Debug\vc120.pdb
文件 16688 2017-03-23 10:28 pet-li\pet-li\pet-li.cpp
文件 4018 2017-03-20 09:48 pet-li\pet-li\pet-li.vcxproj
文件 947 2017-03-20 09:48 pet-li\pet-li\pet-li.vcxproj.filters
- 上一篇:谭浩强C程序设计第三版课件
- 下一篇:编译原理语义分析程序 c++实现
评论
共有 条评论