[YTU]_2635(P4 游戏中的Human角色)
生活随笔
收集整理的這篇文章主要介紹了
[YTU]_2635(P4 游戏中的Human角色)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
在一個平面打斗游戲中,任何的角色(Role)都有血量(blood)和位置loc(此處loc是Location類的實例)屬性。有了Role類,可以派生出不同的角色,如人、神仙、怪獸等。如下程序中,定義了Location類和Role類,人類(Human)中新增了姓名和攻擊力數據成員,請為Human類設計成員函數,并實現Role類中的moveTo和addBlood兩個成員函數。 請在begin和end中間寫下需要的代碼。你只能編輯并提交begin和end之間的代碼。 #include <iostream> using namespace std; class Location { private: int x, y; public: Location(int a, int b):x(a),y(b) {} int getX(){return x;} int getY(){return y;} void setXY(int a,int b) {x=a;y=b;}; ?//設置位置坐標 }; class Role { public: Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {} void moveTo(int rx, int ry); ?//移動到(rx, ty)處,要改變loc的值 void addBlood(int b); //增加血量,參數為負時,代表減少 protected: int blood; Location loc; }; void Role::moveTo(int rx, int ry){
loc.setXY(rx,ry);
}
void Role::addBlood(int b)
{
blood+=b;
} //************* begin ***************** class Human: public Role { public: private: string name; ?// 姓名 int attack; ? // 攻擊力 }; //************* end ***************** int main() { string name; int att, blood, x, y; cin>>name>>att>>blood>>x>>y; Human hum(name,att,blood,x,y); //人name的攻擊力att,血量blood,在(x,y)處 hum.show(); int incAtt, incBlood, newx, newy ; cin>>incAtt; cin>>incBlood; cin>>newx>>newy; hum.addAttack(incAtt); ?//攻擊力增加incAtt hum.addBlood(incBlood); //血量增加incBlood hum.moveTo(newx,newy); ?//人移到了(news,newy)處 hum.show(); return 0; }
輸入
第一行:名字 血量 攻擊力 位置,其中位置處是兩個整數,代表平面x、y坐標 第二行:增加的攻擊力 第三行:要增加的血量 第四行:新位置,用兩個整數代表 輸入的各部分之間用空格隔開輸出
分別用兩行顯示游戲角色的初始狀態和調整參數后的狀態 如“Avanda has 500 attack and 1000 blood in (4,3)”表示Avanda有500攻擊力1000血量,在(4,3)位置處樣例輸入
Avanda 500 1000 4 3 -300 200 2 5樣例輸出
Avanda has 500 attack and 1000 blood in (4,3) Avanda has 200 attack and 1200 blood in (2,5)#include <iostream> using namespace std; class Location { private:int x, y; public:Location(int a, int b):x(a),y(b) {}int getX(){return x;}int getY(){return y;}void setXY(int a,int b) {x=a;y=b;}; //設置位置坐標 };class Role { public:Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {}void moveTo(int rx, int ry); //移動到(rx, ty)處,要改變loc的值void addBlood(int b); //增加血量,參數為負時,代表減少 protected:int blood;Location loc; }; void Role::moveTo(int rx, int ry) {loc.setXY(rx,ry); } void Role::addBlood(int b) {blood+=b; }class Human:public Role { public:Human(string nam,int att,int b,int rx,int ry):Role(b,rx,ry),name(nam),attack(att){}void show(){cout<<name<<" has "<<attack<<" attack and "<<blood<<" blood in ("<<loc.getX()<<','<<loc.getY()<<')'<<endl;}void addAttack(int att){attack+=att;} private:string name;int attack; }; int main() {string name;int att, blood, x, y;cin>>name>>att>>blood>>x>>y;Human hum(name,att,blood,x,y); //人name的攻擊力att,血量blood,在(x,y)處hum.show();int incAtt, incBlood, newx, newy ;cin>>incAtt;cin>>incBlood;cin>>newx>>newy;hum.addAttack(incAtt); //攻擊力增加incAtthum.addBlood(incBlood); //血量增加incBloodhum.moveTo(newx,newy); //人移到了(news,newy)處hum.show();return 0;總結
以上是生活随笔為你收集整理的[YTU]_2635(P4 游戏中的Human角色)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2476(E3 继承了,成员
- 下一篇: [YTU]_2906( 多重继承 日期与