java纯文字游戏
java纯文字小游戏
package Test2;
import java.util.Random;
public class Role {
private String name ;
private int blood;
private char gender;
private String face;
public Role() {
}
public Role(String name, int blood) {
this.name = name;
this.blood = blood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
//定义一个方法用于共计别人,思考:谁攻击谁
//Role r1 = new Role(); Role r2 = new Role();
//r1.fangfa(r2);
public void attack(Role role) {
//计算随机伤害1~20
Random r = new Random();
int hurt = r.nextInt(20) + 1;
//剩余血量
int remainBlood = role.getBlood() - hurt;
//对剩余血量进行验证,如果为负,就修改为0
remainBlood = (remainBlood >= 0) ? remainBlood : 0;
//修改一下挨揍人的血量
role.setBlood(remainBlood);
System.out.println(this.getName() + "举起拳头,打了" + role.getName() + "一下,造成了" + hurt + "点伤害," + role.getName() + "还剩下" + remainBlood + "点血量");
}
}
package Test2;
public class gameTest {
public static void main(String[] args){
Role r1 =new Role("光头强",100);
Role r2 =new Role("熊二",100);
while (true){
//r1开始攻击r2
r1.attack(r2);
//判断r2的剩余血量
if (r2.getBlood() == 0){
System.out.println(r1.getName() + "k.o了" + r2.getName());
break;
}
//r2开始攻击r1
r2.attack(r1);
//r2的剩余血量
if(r1.getBlood() == 0){
System.out.println(r2.getName() + "k.o了" + r1.getName());
break;
}
}
}
}
原文地址:https://blog.csdn.net/2302_80471041/article/details/137888140
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!