您好,欢迎访问三七文档
当前位置:首页 > 医学/心理学 > 药学 > C++程序设计电子宠物
#includeiostream#includestdlib.husingnamespacestd;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(m5)mood=mood-2;elseif(m12)mood--;elsemood++;age++;hunger=hunger++;thirst=thirst++;}inlinevoidpet::show()//各个数值的输出函数{cout现在的状态:endl;cout年龄:age;cout饥饿度:hunger;cout口渴度:thirst;cout心情指数:mood;cout健康指数:health;}inlinevoidpet::emergency(intemg)//各种特殊状态函数{step=step+5;if(emg==20)health--;if(hunger==3){mood--;cout主人,我已经很饿了。endl;}if(hunger==-2){mood--;cout主人,我已经很饱了。endl;}if(hunger3){mood=mood-2;health--;cout再不给我吃东西,我就要饿死啦!endl;}if(hunger-2){mood=mood-2;health--;cout再给我吃东西,我就要撑死啦!endl;}if(thirst==2){mood--;cout这天好热啊,主人,给口喝的吧。endl;}if(thirst==-2){mood--;cout面对着一大杯水,你的宠物恶心地说道:“主人,我已经喝够了。”endl;}if(thirst2){mood=mood-2;health--;cout伟大的主人在上,我已经渴的快受不鸟啦。endl;}if(thirst-2){mood=mood-2;health--;cout好吧,我都喝撑的都吃不下饭了。endl;}if(hunger6||thirst5){age=100;cout“啊!天呐,你降道雷劈了这个万恶的主人吧!”说完,某宠物口吐白沫,到底不起。endl;}if(hunger5&&thirst4){age=100;cout如果有来世的话,打死我也不跟你混了。endl;}if(health-6){age=100;cout你骨瘦如柴的宠物最后饱含深情地看了你一眼,缓缓倒下了。endl;}}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饲养说明:endl;cout每个时间段作为主人的你新增5点行动点,未用完可以累积到下一时段,初始有20点。endl;cout当行动点相当低时,出现badend,饲养结束。endl;cout每天有三个时间段,你可以进行5项操作,喂食、喂水、抚摸、玩耍、不做任何事。endl;cout行动点对应消耗:喂食5点,喂水3点,抚摸3点,玩耍8点。endl;cout选择不做任何事,则跳到下一时间段。endl;cout当饥饿与口渴相当低的时候,或者健康指数相当低的时候,会出现badend,宠物死亡。endl;cout当宠物年龄到达寿命极限时,出现happyend,宠物寿终正寝。endl;coutPS:不同的天气对宠物的心情和健康又影响。endl;cout在不同天气中进行抚摸玩耍会有特殊情况发生,请注意。endl;cout-------------------------------------------------------------------------------endl;}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恭喜,你已经领养了一只兔子。endl;cout-------------------------------------------------------------------------------endl;cout,-*,-*endl;cout(((endl;cout\))_..-.._endl;cout__)/,*,**.endl;cout,***,--.*.endl;cout,*0*'*\endl;cout(Y(;**.endl;cout*--.____,\,;endl;cout((_,----*,---*_,*_,*endl;cout(((_,-(((______,-*endl;cout-------------------------------------------------------------------------------endl;break;case2:cout恭喜,你已经领养了一匹小马驹。endl;cout-------------------------------------------------------------------------------endl;cout~~%%%%%%%%_,_,endl;cout~~%%%%%%%%%-*/./endl;cout~~%%%%%%%-*/*.endl;cout~~%%%%%%%%*.,--;endl;cout~~%%%%%%%%*:\O\endl;cout~~%%%%%%%%*:*.endl;cout~~%%%%%%%%**.-,*endl;cout~~%%%%%%%%*.**-.-*.endl;cout~~%%%%%%%%%*:*-.(,;endl;cout~~%%%%%%%%*:*.-\-.*endl;cout~~%%%%%%%*;endl;cout-------------------------------------------------------------------------------endl;break;case3:cout恭喜,你已经领养了一头小牛犊。endl;cout-------------------------------------------------------------------------------endl;cout/~~~~//~~~~/endl;cout////endl;
本文标题:C++程序设计电子宠物
链接地址:https://www.777doc.com/doc-3163647 .html