📄 gameword.java
字号:
package NamePK;
import java.util.Random;
public class GameWord implements Runnable {
private String name1, name2;
InputFun ifun = new InputFun();
Rule r1, r2;
Random r = new Random();
Thread thread;
String Attack_Str = "", Recovery_Str = "";// 攻击信息,防御信息
int operter1 = 0;// 对敌方造成的伤害
int operter2 = 0;// 防御所造成的伤害
boolean loop = true;// 标识当前的出招的人
private boolean ispoisoning;// 是否中毒
public GameWord() {
init();
r1 = new Rule(name1);
r2 = new Rule(name2);
getRuleAttribult();
System.out.println("比赛开始 GO~~");
}
public void init() {
System.out.println("名字PK大战");
while (name1 == null || name1.equals("")) {// 循环获取正确输入
System.out.print("请输入挑战者的名字:");
name1 = ifun.input();
}
while (name2 == null || name2.equals("")) {// 循环获取正确输入
System.out.print("请输入被挑战者的名字:");
name2 = ifun.input();
}
}
public void starThread() {// 开始游戏线程
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
public void getRuleAttribult() {// 获取角色的属性
r1.getAttribult();
r2.getAttribult();
}
public void getResule() {// 获取最终结果
System.out
.println("************************************************************");
getRuleAttribult();
if (r1.hp <= 0) {
System.out.println("真遗憾,【" + r1.playName + "】被打败了.");
} else if (r2.hp <= 0) {
System.out.println("真遗憾,【" + r2.playName + "】被打败了.");
}
}
public void update(Rule rule1, Rule rule2) {// 更新角色状态,发动进攻和防御
rule_Attack(rule1, rule2);// 调用攻击方法
rule_Recovery(rule1, rule2);// 调用防御方法
}
public int attributeDown(Rule r) {// 全部降低属性
r.attack -= 10;
r.hit -= 10;
r.luck -= 10;
r.rate -= 10;
r.recovery -= 10;
return 0;
}
public int attributeUp(Rule r) {// 全部提升属性
r.attack += 10;
r.hit += 10;
r.luck += 10;
r.rate += 10;
r.recovery += 10;
return 0;
}
/**
* 攻击方法
*/
public void rule_Attack(Rule rule1, Rule rule2) {
int state = rule1.attack;
int value = getRandom();
value = (state + value) % 15;
switch (value) {
case 0:
Attack_Str = "【" + rule1.playName + "】向" + "【" + rule2.playName
+ "】发动攻击";
operter1 = 100;
case 1:
Attack_Str = "【" + rule1.playName + "】向" + "【" + rule2.playName
+ "】发动攻击";
operter1 = 80;
case 2:
Attack_Str = "【" + rule1.playName + "】向" + "【" + rule2.playName
+ "】发动攻击";
operter1 = 50;
case 3:
Attack_Str = "【" + rule1.playName + "】向" + "【" + rule2.playName
+ "】发动攻击";
operter1 = 35;
break;
case 4:
operter1 = 60;
Attack_Str = "【" + rule1.playName + "】拿开水向" + "【" + rule2.playName
+ "】泼过去";
break;
case 5:
operter1 = 80;
Attack_Str = "【" + rule1.playName + "】冲向前狂 咬" + "【"
+ rule2.playName + "】一口";
break;
case 6:
operter1 = -30;
Attack_Str = "【" + rule1.playName + "】向" + "【" + rule2.playName
+ "】发动攻击,结果摔到在地";
break;
case 7:
operter1 = r1.hp / 2;
Attack_Str = "【" + rule1.playName + "】向" + "【" + rule2.playName
+ "】投毒";
ispoisoning = true;// 表示中毒
break;
case 8:
operter1 = 60;
Attack_Str = "【" + rule1.playName + "】发怒对" + "【" + rule2.playName
+ "】进行暴打";
break;
case 9:
operter1 = 20;
Attack_Str = "【" + rule1.playName + "】 诅咒 " + "【" + rule2.playName
+ "】";
break;
case 10:
operter1 = 150;
Attack_Str = "【" + rule1.playName + "】招来一群人对 " + "【"
+ rule2.playName + "】群殴";
break;
case 11:
operter1 = -200;
Attack_Str = "【" + rule1.playName + "】在" + "【" + rule2.playName
+ "】面前自杀";
break;
case 12:
operter1 = 200;
Attack_Str = "【" + rule1.playName + "】潜能被激发,亮出武器向" + "【"
+ rule2.playName + "】发动攻击,各项数值上升";
attributeUp(rule1);
break;
default:
operter1 = -20;
attributeUp(rule1);
Attack_Str = "【" + rule1.playName + "】恢复体力";
break;
}
}
/**
* 防御的方法
*/
public void rule_Recovery(Rule rule1, Rule rule2) {
int state = rule2.recovery;
int value = getRandom();
value = (state + value) % 10;
if (operter1 > 0) {
switch (value) {
case 2:
operter2 = operter1;
Recovery_Str = "【" + rule2.playName + "】受了内伤.";
break;
case 3:
operter2 = operter1 / 10 + 10;
Recovery_Str = "【" + rule2.playName + "】小躲了一下,但仍";
break;
case 4:
operter2 = operter1 / 10;
Recovery_Str = "【" + rule2.playName + "】 防御了一下下.但仍";
break;
case 5:
case 0:
case 1:
operter2 = operter1 / 2 * 3;
Recovery_Str = "【" + rule2.playName + "】被正面击中了.结果";
if (ispoisoning) {
Recovery_Str += "属性全面降低了10点,并";
attributeDown(rule2);
}
break;
case 6:
operter2 = -operter1;
Recovery_Str = "【" + rule2.playName + "】刚好躲过了攻击,不受伤.";
break;
case 7:
operter2 = operter1 / 10 + 4;
Recovery_Str = "【" + rule2.playName + "】 挣脱了.但是仍";
break;
case 8:
operter2 = 2 * operter1;
Recovery_Str = "【" + rule2.playName + "】被打晕了.";
break;
case 9:
operter2 = -operter1 + 50;
Recovery_Str = "【" + rule2.playName + "】垂死奋斗.";
break;
}
int result = operter1 + operter2;
if (result != 0) {
Recovery_Str += "受到 " + (result) + " 点伤害.";
rule2.hp -= (result);
}
} else {
Recovery_Str = "【" + rule1.playName + "】 自己受到了 " + (-operter1)
+ " 点伤害.";
rule1.hp += operter1;
}
ispoisoning = false;
}
/**
* 返回个随机的状态
*/
public int getRandom() {
int state = Math.abs(r.nextInt(100));
return state;
}
/**
* 在屏幕上绘制 当前回合作战的结果
*/
public void paint() {//绘制 当前回合作战的结果
System.out.print(" " + Attack_Str + "; ");//打印出攻击信息
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Recovery_Str + " 经过本轮角逐:【" + r1.playName
+ "】的hp = " + r1.hp + " 【" + r2.playName + "】的hp = " + r2.hp);//打印出防御信息和本轮角逐结果
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {// 游戏循环
int debug = 0;
try {
while (r1.hp > 0 && r2.hp > 0) {// 当两个人的生命值都不小于0
long time = java.lang.System.currentTimeMillis() + 50;
debug = 1;
if (loop) {// 互换相互出招
update(r1, r2);
loop = false;
} else {
update(r2, r1);
loop = true;
}
debug = 2;
paint();
java.lang.Thread.yield();
while (time > java.lang.System.currentTimeMillis()) {
;
}
}
getResule();// 有一个人生命值小于0,游戏结束
} catch (Exception e) {
System.err.println("run error:" + e + " debug:" + debug);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -