C程序设计电子宠物.docx
- 文档编号:5581072
- 上传时间:2022-12-28
- 格式:DOCX
- 页数:18
- 大小:18.90KB
C程序设计电子宠物.docx
《C程序设计电子宠物.docx》由会员分享,可在线阅读,更多相关《C程序设计电子宠物.docx(18页珍藏版)》请在冰豆网上搜索。
C程序设计电子宠物
#include
#include
usingnamespacestd;
classpet//建立一个宠物类
{
public:
pet(inta=0,inth=0,intt=0,intm=0,inthe=0,intst=15)//构造函数
{age=a;hunger=h;thirst=t;mood=m;health=he;step=st;}
intgeta(){returnage;}//提取各个私有成员
intgeth(){returnhunger;}
intgett(){returnthirst;}
intgetm(){returnmood;}
intgethe(){returnhealth;}
intgetst(){returnstep;}
voidp2();//饥饿、口渴以及心情随时间的变化函数
voidshow();//各个数值的输出函数
voidemergency(intw);//各种特殊状态函数
voidfeed()//喂食函数
{
hunger--;
step=step-5;
}
voiddrink(){thirst--;step=step-3;}//喂水函数
voiddoctor(){step=step-10;}//看医生函数
voidtouch(intw);//抚摸函数
voidfun(intw);//玩耍函数
virtualintgetl(){returnage;}//提取寿命值
virtualvoidaction(intact,intw,intemg){}//主人采取各种动作函数,定义为纯虚函数
private:
intage;//年龄
inthunger;//饥饿值
intthirst;//口渴值
intmood;//心情指数
inthealth;//健康指数
intstep;//行动点数
};
inlinevoidpet:
:
p2()//饥饿、口渴以及心情随时间的变化函数
{
intm;
m=rand()%15;
if(m<5)
mood=mood-2;
elseif(m<12)
mood--;
else
mood++;
age++;
hunger=hunger++;
thirst=thirst++;
}
inlinevoidpet:
:
show()//各个数值的输出函数
{
cout<<"现在的状态:
"< cout<<"年龄: "< cout<<"饥饿度: "< cout<<"口渴度: "< cout<<"心情指数: "< cout<<"健康指数: "< } inlinevoidpet: : emergency(intemg)//各种特殊状态函数 { step=step+5; if(emg==20) health--; if(hunger==3) { mood--; cout<<"主人,我已经很饿了。 "< } if(hunger==-2) { mood--; cout<<"主人,我已经很饱了。 "< } if(hunger>3) { mood=mood-2; health--; cout<<"再不给我吃东西,我就要饿死啦! "< } if(hunger<-2) { mood=mood-2; health--; cout<<"再给我吃东西,我就要撑死啦! "< } if(thirst==2) { mood--; cout<<"这天好热啊,主人,给口喝的吧。 "< } if(thirst==-2) { mood--; cout<<"面对着一大杯水,你的宠物恶心地说道: “主人,我已经喝够了。 ”"< } if(thirst>2) { mood=mood-2; health--; cout<<"伟大的主人在上,我已经渴的快受不鸟啦。 "< } if(thirst<-2) { mood=mood-2; health--; cout<<"好吧,我都喝撑的都吃不下饭了。 "< } if(hunger>6||thirst>5) { age=100; cout<<"“啊! 天呐,你降道雷劈了这个万恶的主人吧! ”说完,某宠物口吐白沫,到底不起。 "< } if(hunger>5&&thirst>4) { age=100; cout<<"如果有来世的话,打死我也不跟你混了。 "< } if(health<-6) { age=100; cout<<"你骨瘦如柴的宠物最后饱含深情地看了你一眼,缓缓倒下了。 "< } } inlinevoidpet: : touch(intw)//抚摸函数 { step=step-3; switch(w) { case0: mood=mood+2; break; case1: mood++; break; case2: mood++; break; case3: mood--; thirst--; break; case4: mood--; hunger--; break; } } inlinevoidpet: : fun(intw)//玩耍函数 { step=step-8; switch(w) { case0: mood=mood+2; break; case2: hunger++; thirst++; health++; break; case3: hunger=hunger+2; thirst++; health++; break; case4: hunger++; thirst++; health++; break; } } voidinstruction()//饲养说明 { cout<<"饲养说明: "< cout<<"每个时间段作为主人的你新增5点行动点,未用完可以累积到下一时段,初始有20点。 "< cout<<"当行动点相当低时,出现badend,饲养结束。 "< cout<<"每天有三个时间段,你可以进行5项操作,喂食、喂水、抚摸、玩耍、不做任何事。 "< cout<<"行动点对应消耗: 喂食5点,喂水3点,抚摸3点,玩耍8点。 "< cout<<"选择不做任何事,则跳到下一时间段。 "< cout<<"当饥饿与口渴相当低的时候,或者健康指数相当低的时候,会出现badend,宠物死亡。 "< cout<<"当宠物年龄到达寿命极限时,出现happyend,宠物寿终正寝。 "< cout<<"PS: 不同的天气对宠物的心情和健康又影响。 "< cout<<"在不同天气中进行抚摸玩耍会有特殊情况发生,请注意。 "< cout<<"-------------------------------------------------------------------------------"< } classrabbit: virtualpublicpet//pet类派生的兔子类 { public: rabbit(intl=31) { life=l; } intgetl() { returnlife; } voidaction(intact,intw,intemg);//主人对小牛的动作函数,虚函数成员 private: intlife;//兔子的寿命 }; inlinevoidrabbit: : action(intact,intw,intemg) { switch(act) { case0: instruction();break; case1: feed(); break; case2: drink(); break; case3: touch(w); break; case4: fun(w); break; case5: break; case6: emg=0; doctor(); break; } } classhorse: virtualpublicpet//pet类派生的小马驹类 { public: horse(intl=41) { life=l; } intgetl() { returnlife; } voidaction(intact,intw,intemg);//主人对小牛的动作函数,虚函数成员 private: intlife;//小马的寿命 }; inlinevoidhorse: : action(intact,intw,intemg) { switch(act) { case0: instruction();break; case1: feed();break; case2: drink();break; case3: touch(w);break; case4: fun(w);break; case5: break; case6: emg=0; doctor(); break; } } classcalf: virtualpublicpet//pet类派生的小牛类 { public: calf(intl=51){life=l;} intgetl() { returnlife; } voidaction(intact,intw,intemg);//主人对小牛的动作函数,虚函数成员 private: intlife;//小牛的寿命 }; inlinevoidcalf: : action(intact,intw,intemg) { switch(act) { case0: instruction();break; case1: feed();break; case2: drink();break; case3: touch(w);break; case4: fun(w);break; case5: break; case6: emg=0; doctor(); break; } } voidpetshop(intselect)//宠物外形 { switch(select) { case1: cout<<"恭喜,你已经领养了一只兔子。 "< cout<<"-------------------------------------------------------------------------------"< cout<<",-*,-*"< cout<<"((("< cout<<"\))_..-.._"< cout<<"__)/,*,**."< cout<<",***,--.*."< cout<<",*0*'*\"< cout<<"(Y(;**."< cout<<"*--.____,\,;"< cout<<"((_,----*,---*_,*_,*"< cout<<"(((_,-(((______,-*"< cout<<"-------------------------------------------------------------------------------"< break; case2: cout<<"恭喜,你已经领养了一匹小马驹。 "< cout<<"-------------------------------------------------------------------------------"< cout<<"~~%%%%%%%%_,_,"< cout<<"~~%%%%%%%%%-*/./"< cout<<"~~%%%%%%%-*/*."< cout<<"~~%%%%%%%%*.,--;"< cout<<"~~%%%%%%%%*: \O\"< cout<<"~~%%%%%%%%*: *."< cout<<"~~%%%%%%%%**.-,*"< cout<<"~~%%%%%%%%*.**-.-*."< cout<<"~~%%%%%%%%%*: *-.(,;"< cout<<"~~%%%%%%%%*: *.-\-.*"< cout<<"~~%%%%%%%*;"< cout<<"-------------------------------------------------------------------------------"< break; case3: cout<<"恭喜,你已经领养了一头小牛犊。 "< cout<<"-------------------------------------------------------------------------------"< cout<<"/~~~~//~~~~/"< cout<<"////"< cout<<"||_____||"< cout<<"/~~/"< cout<<"/~~~~~-_|/~~//~~~/|_-~~~~~\"< cout<<"/====/||O||O||/====/"< cout<<"~-__-~||_---+--+----_||~-__-~"< cout<<"|/~~/|"< cout<<"//"< cout<<"(OO)"< cout<<"/./"< cout<<"~-__________-~|"< cout<<"||/__/||"< cout<<"||/~~/||"< cout<<"|/~~~~~~~~~/|"< cout<<"||||"< cout<<"||||"< cout<<"-------------------------------------------------------------------------------"< break; } } voidperiod(intt)//时间函数 { t=t%3; switch(t) { case0: cout<<"上午。 "< case1: cout<<"下午。 "< case2: cout<<"晚上。 "< } }; intweather()//天气函数 { intw; w=rand()%5; switch(w) { case0: cout<<"天气: 晴。 "< case1: cout<<"天气: 阴。 "< case2: cout<<"天气: 小雨。 "< case3: cout<<"天气: 暴雨。 "< case4: cout<<"天气: 大风。 "< } returnw; } voidraise(intselect) { intlife,date,w,act; intt=0; intemg,Nstep,Nage; pet*petx;//动态建立对象,实现领养 switch(select) { case1: petx=newrabbit; break; case2: petx=newhorse; break; case3: petx=newcalf; break; } life=petx->getl();//提取寿命 for(date=1;date { cout<<"今天是你领养第"< w=weather();//调用天气函数 t=t%3;//t为控制时间整形 petx->p2();//调用饥饿、口渴以及心情随时间的变化函数 while(t<3)//控制时间的循环 { emg=rand()%20;//emg为随机数,控制是否生病 cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"< cout<<"现在是"; period(t);//调用时间函数 cout<<"-------------------------------------------------------------------------------"< cout< do { Nstep=petx->getst();//提取行动点数 cout<<"你的行动点数还有: "< cout<<"你的宠物"; petx->show();//调用数值输出函数 if((emg-19)==1) cout<<"健康状况: 生病"< else cout<<"健康状况: 良好"< cout<<"请选择你想做的事: "< cout<<"0、参看说明1、喂食2、喂水3、抚摸4、逗它玩耍5、不做任何事"; if((emg-19)==1) cout<<"6、看医生。 "< else cout< cout<<"请选择: "; cin>>act; cout<<"-------------------------------------------------------------------------------"< petx->action(act,w,emg);//调用主人动作函数 }while((act+1)%6); petx->emergency(emg);//调用特殊状态函数 Nage=petx->geta();//提取年龄 if(Nage>life) { cout<<"-------------------------------------------------------------------------------"< cout<<"很遗憾,由于饲养不善,你的宠物已经死亡。 "< cout<<"-------------------------------------------------------------------------------"< break; }//非正常死亡,badend if(Nstep<0) { cout<<"-------------------------------------------------------------------------------"< cout<<"真不幸,宠物的饲养已经让你感到心力交瘁。 "< cout<<"看来你和它共同的生活要就此告一段落了。 "< cout<<"唉,你的修行还未够班啊,继续努力吧。 "< cout<<"-------------------------------------------------------------------------------"< break; }//非正常结束饲养,badend t++; cout<<"-------------------------------------------------------------------------------"< } if(Nage>life||Nstep<0) break;//非正常死亡,跳出循环 system("cls");//调用系统清屏函数 } if(date==life) { cout<<"-------------------------------------------------------------------------------"< cout<<"恭喜,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序设计 电子 宠物