C++程序設(shè)計電子寵物.doc_第1頁
C++程序設(shè)計電子寵物.doc_第2頁
C++程序設(shè)計電子寵物.doc_第3頁
C++程序設(shè)計電子寵物.doc_第4頁
C++程序設(shè)計電子寵物.doc_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、。#include #include using namespace std;class pet/建立一個寵物類public:pet(int a=0,int h=0,int t=0,int m=0,int he=0,int st=15)/構(gòu)造函數(shù)age=a;hunger=h;thirst=t;mood=m;health=he;step=st;intgeta()returnage;/提取各個私有成員int geth() return hunger;int gett() return thirst;int getm() return mood;int gethe() return health;i

2、nt getst() return step;void p2();/饑餓、口渴以及心情隨時間的變化函數(shù)void show();/各個數(shù)值的輸出函數(shù)void emergency(int w);/各種特殊狀態(tài)函數(shù)void feed()/喂食函數(shù)hunger-;step=step-5;void drink() thirst-;step=step-3; /喂水函數(shù)void doctor() step=step-10;/看醫(yī)生函數(shù)void touch(int w);/撫摸函數(shù)void fun(int w);/玩耍函數(shù)virtual int getl() return age;/提取壽命值virtual

3、void action(int act,int w,int emg) /主人采取各種動作函數(shù),定義為純虛函數(shù)private:int age;/年齡int hunger;/饑餓值int thirst;/口渴值int mood;/心情指數(shù)int health;/健康指數(shù)int step;/行動點數(shù);inline void pet:p2()/饑餓、口渴以及心情隨時間的變化函數(shù)int m;m=rand()%15;if(m5)mood=mood-2;。1。else if(m12)mood-;elsemood+;age+;hunger=hunger+;thirst=thirst+;inline void

4、pet:show()/各個數(shù)值的輸出函數(shù)cout 現(xiàn)在的狀態(tài):endl;cout 年齡: age;cout饑餓度: hunger;cout口渴度: thirst;cout心情指數(shù): mood;cout健康指數(shù): health;inline void pet:emergency(int emg)/各種特殊狀態(tài)函數(shù)step=step+5;if(emg=20)health-;if(hunger=3)mood-;cout 主人,我已經(jīng)很餓了。endl;if(hunger=-2)mood-;cout 主人,我已經(jīng)很飽了。3)mood=mood-2;health-;cout 再不給我吃東西,我就要餓死啦!

5、endl;if(hunger-2)mood=mood-2;health-;。2。cout 再給我吃東西,我就要撐死啦!endl;if(thirst=2)mood-;cout 這天好熱啊,主人,給口喝的吧。endl;if(thirst=-2)mood-;cout 面對著一大杯水, 你的寵物惡心地說道:“主人,我已經(jīng)喝夠了。 ”2)mood=mood-2;health-;cout 偉大的主人在上,我已經(jīng)渴的快受不鳥啦。endl;if(thirst-2)mood=mood-2;health-;cout 好吧,我都喝撐的都吃不下飯了。6|thirst5)age=100;cout “?。√靺?,你降道雷劈

6、了這個萬惡的主人吧!”說完,某寵物口吐白沫,到底不起。 5&thirst4)age=100;cout 如果有來世的話,打死我也不跟你混了。endl;if(health-6)age=100;cout 你骨瘦如柴的寵物最后飽含深情地看了你一眼,緩緩倒下了。endl;inline void pet:touch(int w)/撫摸函數(shù)。3。step=step-3;switch(w)case 0:mood=mood+2;break;case 1:mood+;break;case 2:mood+;break;case 3:mood-;thirst-;break;case 4:mood-;hunger-;b

7、reak;inline void pet:fun(int w)/玩耍函數(shù)step=step-8;switch(w)case 0:mood=mood+2;break;case 2:hunger+;thirst+;health+;break;case 3:hunger=hunger+2;thirst+;health+;break;case 4:hunger+;thirst+;。4。health+;break;void instruction()/飼養(yǎng)說明cout 飼養(yǎng)說明: endl;cout 每個時間段作為主人的你新增5 點行動點,未用完可以累積到下一時段,初始有 20 點。 endl;cout

8、 當(dāng)行動點相當(dāng)?shù)蜁r,出現(xiàn)bad end ,飼養(yǎng)結(jié)束。 endl;cout 每天有三個時間段,你可以進(jìn)行5 項操作,喂食、喂水、撫摸、玩耍、不做任何事。 endl;cout 行動點對應(yīng)消耗:喂食5 點,喂水3 點,撫摸3 點,玩耍8 點。 endl;cout 選擇不做任何事,則跳到下一時間段。endl;cout 當(dāng)饑餓與口渴相當(dāng)?shù)偷臅r候,或者健康指數(shù)相當(dāng)?shù)偷臅r候,會出現(xiàn)bad end ,寵物死亡。 endl;cout 當(dāng)寵物年齡到達(dá)壽命極限時,出現(xiàn)happy end ,寵物壽終正寢。endl;coutPS :不同的天氣對寵物的心情和健康又影響。endl;cout 在不同天氣中進(jìn)行撫摸玩耍會有特殊

9、情況發(fā)生,請注意。endl;cout-endl;class rabbit:virtual public pet/pet類派生的兔子類public:rabbit(int l=31)life=l;int getl()return life;void action(int act,intw,intemg);/主人對小牛的動作函數(shù),虛函數(shù)成員private:int life;/兔子的壽命;inline void rabbit:action(int act,int w,int emg)switch(act)。5。case 0:instruction();break;case 1:feed();break

10、;case 2:drink();break;case 3:touch(w);break;case 4:fun(w);break;case 5:break;case 6:emg=0;doctor();break;class horse:virtual public pet/pet類派生的小馬駒類public:horse(int l=41)life=l;int getl()return life;void action(int act,intw,intemg);/主人對小牛的動作函數(shù),虛函數(shù)成員private:int life;/小馬的壽命;inline void horse:action(int

11、 act,int w,int emg)switch(act)。6。case 0:instruction();break;case 1:feed();break;case 2:drink();break;case 3:touch(w);break;case 4:fun(w);break;case 5:break;case 6:emg=0;doctor();break;class calf:virtual public pet/pet類派生的小牛類public:calf(int l=51) life=l;int getl()return life;void action(int act,int w

12、,int emg);/主人對小牛的動作函數(shù),虛函數(shù)成員private:int life;/小牛的壽命;inline void calf:action(int act,int w,int emg)switch(act)case 0:instruction();break;case 1:feed();break;case 2:drink();break;。7。case 3:touch(w);break;case 4:fun(w);break;case 5:break;case 6:emg=0;doctor();break;void petshop(int select)/寵物外形switch(se

13、lect)case 1:cout 恭喜,你已經(jīng)領(lǐng)養(yǎng)了一只兔子。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-。8。-endl;break;case 2:cout 恭喜,你已經(jīng)領(lǐng)養(yǎng)了一匹小馬駒。endl;cout-endl;cout%_,_,endl;cout%-*/./endl;c

14、out%-*/ *.endl;cout%* .,-;endl;cout%*:Oendl;cout%*:*.endl;cout%*. -,*endl;cout%*.*-.-*.endl;cout%*:*-.(,;endl;cout%*:*.-.*endl;cout%*;endl;cout-endl;break;case 3:cout 恭喜,你已經(jīng)領(lǐng)養(yǎng)了一頭小牛犢。endl;cout-endl;cout/endl;cout/endl;cout|_|endl;。9。cout/endl;cout/-_|/|_-endl;cout/ = /| |O| |O| |/ = /endl;cout-_-|_-+

15、-+-_|-_-endl;cout|/|endl;cout/endl;cout(OO)endl;cout/./endl;cout-_-|endl;cout|/_/|endl;cout|/|endl;cout|/|endl;cout|endl;cout|endl;cout-endl;break;void period(int t)/時間函數(shù)t=t%3;switch(t)case 0:cout上午。 endl;break;case 1:cout下午。 endl;break;case 2:cout晚上。 endl;break;。10。;int weather()/天氣函數(shù)int w;w=rand()

16、%5;switch(w)case 0:cout天氣:晴。 endl;break;case 1:cout天氣:陰。 endl;break;case 2:cout天氣:小雨。endl;break;case 3:cout天氣:暴雨。endl;break;case 4:cout天氣:大風(fēng)。getl();/提取壽命for(date=1;datelife;date+)/開始飼養(yǎng),如無意外,則到壽命時循環(huán)結(jié)束cout 今天是你領(lǐng)養(yǎng)第date 日p2();/調(diào)用饑餓、口渴以及心情隨時間的變化函數(shù)while(t3)/控制時間的循環(huán)。11。emg=rand()%20;/emg為隨機(jī)數(shù),控制是否生病coutendl

17、;cout 現(xiàn)在是 ;period(t);/調(diào)用時間函數(shù)cout-endl;coutgetst();/提取行動點數(shù)cout 你的行動點數(shù)還有:Nstependl;coutshow();/調(diào)用數(shù)值輸出函數(shù)if(emg-19)=1)cout健康狀況:生病endl;elsecout健康狀況:良好endl;cout 請選擇你想做的事:endl;cout0、參看說明1 、喂食 2 、喂水 3 、撫摸 4 、逗它玩耍5 、不做任何事 ;if(emg-19)=1)cout 6、看醫(yī)生。 endl;elsecoutendl;coutact;cout-action(act,w,emg);/調(diào)用主人動作函數(shù)whi

18、le(act+1)%6);petx-emergency(emg);/調(diào)用特殊狀態(tài)函數(shù)Nage=petx-geta();/提取年齡if(Nagelife)cout-endl;cout 很遺憾,由于飼養(yǎng)不善,你的寵物已經(jīng)死亡。endl;cout-。12。-endl;break;/非正常死亡,bad endif(Nstep0)cout-endl;cout 真不幸,寵物的飼養(yǎng)已經(jīng)讓你感到心力交瘁。endl;cout 看來你和它共同的生活要就此告一段落了。endl;cout 唉,你的修行還未夠班啊,繼續(xù)努力吧。endl;cout-endl;break;/非正常結(jié)束飼養(yǎng),bad endt+;cout-life|Nstep0)break;/非正常死亡,跳出循環(huán)system(cls);/調(diào)用系統(tǒng)清屏函數(shù)if(date=life)cout-endl;cout 恭喜,你的寵物已經(jīng)壽終正寢。endl;/正常死亡i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論