package practice;
import java.util.Random;
public class Role {
private String name;
private int blood; //血量
private char sex; //性别
private String face; //外貌
String []boyFace={"风流俊雅","器宇轩昂","相貌平平","五官端正","相貌英俊","一塌糊涂","面目狰狞"};
String []girlFace={"美仑美绝","沉鱼落雁","亭亭玉立","身材姣好","相貌平平","相貌丑陋","惨不忍睹"};
//攻击描述
String []attcaks_desc={
"%s便出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去",
"%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s",
"%s大喝一声,身形下伏,一招【劈雷坠地】,锤向%s双腿",
"%s运气于掌,瞬间掌心变得血红,一式【草心雷】,推向%s。",
"%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s",
"%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s。",
};
//伤害描述
String []injureds_desc={
"结果%s退了半步,毫发无损",
"结果给%s造成了一处淤伤",
"结果一击命中,%s痛得弯下了腰",
"结果%s痛苦的闷哼了一声,显然受了点内伤",
"结果%s摇摇晃晃,摔倒在地",
"结果%s脸色变得一下惨白,连退了好几步",
"结果【啪】的一声,%s口中鲜血狂彪而出",
"结果%s一声惨叫,像瘫软的泥一般塌了下去"
};
public Role() {
}
public Role(String name, int blood,char sex) {
this.name = name;
this.blood = blood;
this.sex=sex;
setFace(this.sex);
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getFace() {
return face;
}
public void setFace(char sex ) {
Random r=new Random();
if(sex=='男'){
int index=r.nextInt(boyFace.length);
this.face=boyFace[index];
} else if(sex=='女'){
int index=r.nextInt(girlFace.length);
this.face=girlFace[index];
}else{
this.face="惨不忍睹";
}
}
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;
}
@Override
public String toString() {
return "Role{" +
"name='" + name + '\'' +
", blood=" + blood +
'}';
}
//定义一个方法攻击别人,
//a攻击b,
//Role a=new Role();
//Role b=new Role();
//a.attack(b); a.攻击(b)
//方法
public void sttack(Role role){
//攻击描述
Random r=new Random();
int index=r.nextInt(attcaks_desc.length);
String kungfu=attcaks_desc[index];
System.out.printf(kungfu,this.getName(),role.getName());
System.out.println();
//计算造成伤害
int hurt=r.nextInt(20)+1;
//计算剩余血量
int remainBlood=role.getBlood()-hurt;
remainBlood= remainBlood < 0 ? 0 : remainBlood;
//刷新被攻击人物的血量
role.setBlood(remainBlood);
//伤害描述;
if(remainBlood>90){
System.out.printf(injureds_desc[0],role.getName());
}else if(remainBlood>80){
System.out.printf(injureds_desc[1],role.getName());
}else if(remainBlood>70){
System.out.printf(injureds_desc[2],role.getName());
}else if(remainBlood>60){
System.out.printf(injureds_desc[3],role.getName());
} else if (remainBlood>40) {
System.out.printf(injureds_desc[4],role.getName());
} else if (remainBlood>20) {
System.out.printf(injureds_desc[5],role.getName());
} else if (remainBlood>10) {
System.out.printf(injureds_desc[6],role.getName());
}else{
System.out.printf(injureds_desc[7],role.getName());
}
System.out.println();
System.out.println();
}
public void showRoleInfo(){
System.out.println("姓名:"+getName());
System.out.println("血量:"+getBlood());
System.out.println("性别:"+getSex());
System.out.println("外貌:"+getFace());
}
}
package practice;
public class z9 {
public static void main(String[] args) {
Role a=new Role("z",100,'男');
Role b=new Role("h",100,'女');
System.out.println("游戏人物1:");
a.showRoleInfo();
System.out.println();
System.out.println("游戏人物2:");
b.showRoleInfo();
System.out.println();
while(true) {
a.sttack(b);
if(b.getBlood()==0){
System.out.println(a.getName()+"K.O了"+b.getName());
break;
}
b.sttack(a);
if(a.getBlood()==0){
System.out.println(b.getName()+"K.O了"+a.getName());
break;
}
}
}
}