文字格斗小游戏
主程序
public class WordGameTest {
public static void main(String[] args) {
//1.创建对象角色1和角色2
WordGame r1 = new WordGame("张三", 100, "男");
WordGame r2 = new WordGame("李四", 100, "女");
System.out.println("====================对战开始===================");
//显示角色信息
r1.showRoleInfo();
System.out.println("===============");
r2.showRoleInfo();
System.out.println("===============");
//2.r1攻击r2,r2再攻击r1
//如果r1/r2血量不大于0则停止攻击,循环结束,
while (true) {
//r1打r2,打完显示r2血量,如果小于等于0,则r1赢了,循环结束
r1.attack(r2);
System.out.println(r2.getRole() + "剩余血量为:" + r2.getBlood());
if (r2.getBlood() == 0) {
System.out.println(r1.getRole() + "剩余血量为:" + r1.getBlood());
System.out.println(r1.getRole() + "赢得了最终胜利");
break;
}
//r2打r1,打完显示r1血量,如果小于等于0,则r2赢了,循环结束
r2.attack(r1);
System.out.println(r1.getRole() + "剩余血量:" + r1.getBlood());
if (r1.getBlood() == 0) {
System.out.println(r2.getRole() + "剩余血量为:" + r2.getBlood());
System.out.println(r2.getRole() + "赢得了最终胜利");
break;
}
}
}
}
JavaBean类文件
import java.util.Random;
public class WordGame {
//成员变量
private String role;
private int blood;
private String gender;
private String face; //长相随机
//创建面容特征数组
String[] boyface = {"风流俊雅", "气宇轩昂", "相貌英俊", "五官端正", "相貌平平", "一塌糊涂", "面目狰狞" };
String[] girlface = {"美奂绝伦", "沉鱼落雁", "婷婷玉立", "身材娇好", "相貌平平", "相貌简陋", "惨不忍睹" };
//攻击方式
String[] attackDesc = {
"%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去,",
"%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s,",
"%s大喝一声,身形下伏,一招【劈雷坠地】,捶向%s双腿,",
"%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s,",
"%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s,",
"%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s,"
};
//受伤方式
String[] injuredDesc = {
"结果%s退了半步,毫发无损", "结果给%s造成一处瘀伤",
"结果一击命中,%s痛得弯下腰",
"结果%s痛苦地闷哼了一声,显然受了点内伤", "结果%s摇摇晃晃,一跤摔倒在地",
"结果%s脸色一下变得惨白,连退了好几步", "结果『轰』的一声," + "%s口中鲜血狂喷而出",
"结果%s一声惨叫,像滩软泥般塌了下去"
};
//=============================================
//成员方法
// 1.构造方法
//空参的构造方法
public WordGame() {
}
//带参的构造方法
public WordGame(String role, int blood, String gender) {
this.role = role;
this.blood = blood;
this.gender = gender;
setFace(gender);
}
//=============================================
// 2.Role部分方法
//接收外部给的数据赋值给成员变量
public void setRole(String role) {
this.role = role;
}
//向外部发送成员变量的值
public String getRole() {
return role;
}
//=============================================
// 3.Blood部分方法
//接收外部给的数据赋值给成员变量
public void setBlood(int blood) {
this.blood = blood;
}
//向外部发送成员变量的值
public int getBlood() {
return blood;
}
//=============================================
// 4.攻击行为方法
public void attack(WordGame role) {
//随机造成的伤害(1-20)
Random r = new Random();
String ad=attackDesc[r.nextInt(attackDesc.length)];
int hurt = r.nextInt(20) + 1;
role.blood = role.blood - hurt;
if (role.blood <= 0) {
role.blood = 0;
}
if (hurt <= 10) {
System.out.printf(ad+"造成了"+hurt+"点伤害 ",this.getRole(),role.getRole());
//System.out.print(this.getRole() + "举起小拳拳,打了" + role.getRole() + "一下,造成了" + hurt + "点伤害,");
} else {
System.out.printf(ad+"造成了"+hurt+"点伤害 ",this.getRole(),role.getRole());
//System.out.print(this.getRole() + "举起小拳拳,打了" + role.getRole() + "一下,触发了暴击,造成了" + hurt + "点伤害,");
}
//如果被攻击者血量小于等于0,则血量赋值为0
}
//=============================================
// 5.性别方法
//接收外部给的数据赋值给成员变量
public void setGender(String gender) {
if (gender == "男" || gender == "女") {
this.gender = gender;
} else {
System.out.println("性别输入错误!!!");
}
}
//接收外部给的数据赋值给成员变量
public String getGender() {
return gender;
}
//=============================================
// 6.面容方法
public void setFace(String face) {
//创建随机变量r
Random r = new Random();
if (gender == "男") {
//从boyface里面随机长相
this.face = boyface[r.nextInt(boyface.length)];
} else if (gender == "女") {
//从girlface里面随机长相
this.face = girlface[r.nextInt(girlface.length)];
} else {
this.face = boyface[boyface.length - 1];
}
}
public String getFace() {
return face;
}
//=============================================
//=============================================
// x.其他用法
//1.显示角色信息
public void showRoleInfo() {
System.out.println("角色:" + getRole());
System.out.println("血量:" + getBlood());
System.out.println("性别:" + getGender());
System.out.println("颜值:" + getFace());
}
//=============================================
}