为了完善上一篇的文字格斗游戏的细节,所以加了些代码,使得交互更加的具体有趣!
效果
大家可以多运行几次代码,得到不同的战况!!
代码实现
1.bean类
java
import java.util.Random;
public class TextGame {
private String name;
private int blood;
private String face;
private char gender;
String[] boyface = {"风流俊雅","气字轩品","相毂关俊","五官正","相较平平","一塌糊涂","面目狰狞"};
String[] girlface = {"美奂绝伦","沉鱼落厂","婷婷玉立","身材娇好","相貌平平","相貌简所","不忍睹"};
String[] attack_desc = {
"%s使出了一招【背心钉】,转到对方的身后,一零向%s背心的关台穴拍去。",
"%s使出了一招【游空探爪】。飞起身形自半空中变章为抓锁向%s。",
"%s人喝一声,身形下伙,一招【劈雷坠地】,捶向%s双腿。",
"%s运气于景,一瞬间常心变得血红,一式【京心需】,推向%s。",
"%s明手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s。",
"%s上步抢身,招中套报,一招【劈挂连环】,连环攻向%s。"};
String[] injured_desc= {
"结果%s退了半步,毫发无损",
"结果给%s适成一处察伤",
"结果一击命中。%s痛得弯下服",
"结果%s痛苦地闷哼了一声,是然受了点内伤",
"结果xs摇摇晃晃,一跤摔倒作地",
"结果%s脸色一下变得惨白,连逖了好几步",
"结果「轰】的一声,%s口中鲜血狂喷面出",
"结果%s一声惨叫,像滩软泥般塌了下去"};
public TextGame() {
}
public TextGame(String name, int blood,char gender) {
this.name = name;
this.blood = blood;
this.gender = gender;
setface(gender);
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return blood
*/
public int getBlood() {
return blood;
}
/**
* 设置
* @param blood
*/
public void setBlood(int blood) {
this.blood = blood;
}
public String toString() {
return "TextGame{name = " + name + ", blood = " + blood + "}";
}
public void setface(char gender){
Random random = new Random();
if(gender == '男'){
int index = random.nextInt(boyface.length);
this.face = boyface[index];
}else if(gender == '女') {
int index = random.nextInt(girlface.length);
this.face = girlface[index];
}else{
this.face = "面目狰狞";
}
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getFace(){
return this.face;
}
public void attack(TextGame role){
//攻击方的威力
Random r1 = new Random();
int index = r1.nextInt(attack_desc.length);
String kungfu = attack_desc[index];
//攻击效果
System.out.printf(kungfu,this.getName(),role.getName());
System.out.println();
//被揍的人伤害点
Random r = new Random() ;
int hurt = r.nextInt(20) + 1;
//修改挨揍人的血量
int remainBlood = Math.max(0 , (role.getBlood() - hurt));
role.setBlood(remainBlood);
// System.out.println(this.getName() + "了举起拳头,打了"+ role.getName() +"一下,造成了"+hurt+"点伤害,"+role.getName()+"还剩下"+remainBlood+"血量");
if(remainBlood > 90){
System.out.printf(injured_desc[0],role.getName());
} else if (remainBlood > 80 && remainBlood <= 90) {
System.out.printf(injured_desc[1],role.getName());
} else if (remainBlood >70 && remainBlood <= 80) {
System.out.printf(injured_desc[2],role.getName());
}else if (remainBlood >60 && remainBlood <= 70) {
System.out.printf(injured_desc[3],role.getName());
}else if (remainBlood >40 && remainBlood <= 60) {
System.out.printf(injured_desc[4],role.getName());
}else if (remainBlood >20 && remainBlood <= 40) {
System.out.printf(injured_desc[5],role.getName());
}else if (remainBlood >10 && remainBlood <= 20) {
System.out.printf(injured_desc[6],role.getName());
}else{
System.out.printf(injured_desc[7],role.getName());
}
System.out.println();
}
public void showRoleInfo(){
System.out.println("姓名为:"+getName());
System.out.println("血型为:"+getBlood());
System.out.println("性别为:"+getGender());
System.out.println("长相为:"+getFace());
}
}
2.测试类
java
public class TextGameText {
public static void main(String[] args) {
TextGame q1 = new TextGame("乔峰", 100,'男');
TextGame q2 = new TextGame("鸠摩智", 100,'男');
q1.showRoleInfo();
q2.showRoleInfo();
while (true){
q1.attack(q2);
if(q2.getBlood() == 0){
System.out.println(q1.getName() +"KO." + q2.getName());
break;
}
q2.attack(q1);
if(q1.getBlood() == 0){
System.out.println(q2.getName() +"KO." + q1.getName());
break;
}
}
}
}
这样就丰富了一场格斗大战的游戏细节!!!