第5周项目2(1) 游戏中的角色类
2016-04-01 20:11
183 查看
/*
All rights reserced. 文件名称:main.cpp 作者:孙亚茹 完成日期:2016.4.1
问题描述:设计游戏中的规则,使角色能按规则行动或改变状态。
*/
#include <iostream> #include<cmath> using namespace std; class Role { public: void show(); void attack(); void eat(); void beAttack(); bool islife(); void setRole(string nam,int n); private: string name; int blood; bool life; }; void Role::setRole(string nam,int n) { name=nam; blood=n; } bool Role::islife() { if(blood>0) { return true; } else return false; } void Role::attack() { if(islife()) blood++; else cout<<name<<"他已经死了...."<<endl; } void Role::eat() { if (islife()) { blood+=2; } else cout<<name<<"他已经死了"<<endl; } void Role::beAttack() {if (islife()) blood--; else cout<<name<<"他已经死了"<<endl; } void Role::show() { if(blood>0) cout<<"姓名是:"<<name<<'\t'<<"blood为: "<<blood<<"滴"<<"他还活着"<<endl; else cout<<name<<"他已经死了"<<endl; } int main() { Role mary; mary.setRole("Mary",4); mary.show(); int blood=4; while(blood>=0) { int n; cout<<"1. attack"<<'\t'<<"2. eat"<<'\t'<<"3. beAttack"<<endl; cout<<"您的选择是: "; cin>>n; if(n==1) { mary.attack(); } else if(n==2) { mary.eat(); } else if(n==3) { mary.beAttack(); } else { break; } mary.show(); cout<<endl<<endl<<endl; } return 0; }
<img src="http://img.blog.csdn.net/20160401201335027" alt="" />
相关文章推荐
- 记录一下我使用的vim的配置文件
- MVC,MVP 和 MVVM 的图示
- 在eclipse中如何产看源码
- 关于python的标准库
- nrf51822 ---协议栈时钟源选择(32.768khz)
- 数据结构实验之排序一:一趟快排
- “开会” 引发的思考
- Delphi透明组件开发(去掉自己的csOpaque,去掉父控件的WS_CLIPCHILDREN,增加WS_EX_TRANSPARENT,截获WM_ERASEBKGND,然后在WM_DRAWITEM里画) good
- LC91 Decode Ways
- Java泛型入门
- 30了还在写初级的代码.
- CAShapeLayer简介
- Android杂记(2):RecyclerView中Button点击问题
- 读一读Scktsrvr.exe的源程序
- PDO中事物处理
- 子类父类构造方法执行顺序举例
- Android WebView 缓存处理
- 62. Unique Paths 动态规划算法浅谈
- JavaScript之jQuery-3 jQuery操作DOM(查询、样式操作、遍历节点、创建插入删除、替换、复制)
- cocos2d-JS 第一炮HelloWorld(很适合小白)