人物赋值属性
package cn.hp8;
public class Demo1 {
public static void main(String[] args) {
//给人物赋值
Demo3 a = new Demo3();
a.setNPC("成龙");
a.setType("战士");
a.setHP(50);
a.setDefense(25);
a.setAttack(15);
Demo3 b = new Demo3();
b.setNPC("渣渣灰");
b.setType("法师");
b.setHP(35);
b.setDefense(10);
b.setAttack(35);
//开始PK
a.pk(b);
}
}
回合制 暴击 神装
package cn.hp8;
import java.util.Random;
public class Demo3 {
//各种属性
private String NPC;
private String type;
private int HP;
private int defense;
private int attack;
private int crit;
public int getCrit() {
return crit;
}
public void setCrit(int crit) {
this.crit = crit;
}
public String getNPC() {
return NPC;
}
public void setNPC(String NPC) {
this.NPC = NPC;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getHP() {
return HP;
}
public void setHP(int HP) {
this.HP = HP;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public int getAttack() {
return attack;
}
public int setAttack(int attack) {
this.attack = attack;
return attack;
}
//自我介绍
public void say(){
System.out.println("人物:"+NPC+"职业"+type+"生命值:"+HP+"防御:"+defense+"攻击力:"+attack);
}
//默认首先进攻方
int flag = 0;
//回合制PK直到一方阵亡
public void pk(Demo3 p) {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Random a = new Random();
//判别进攻方
if (flag == 0) {
//触发暴击
int sum = a.nextInt(3);
if(sum!=2){
int b = a.nextInt(4);
if(b!=0 && b!=1){
this.setAttack(this.attack*b);
System.out.println("成龙打出"+ b+"倍暴击造成");
}
}
//攻击力-防御力
int harm =this.attack - p.defense;
p.setHP(p.HP - harm);
//掉血
System.out.println(p.NPC + "掉血" + harm);
//剩余血量
System.out.println("渣渣灰剩余血量"+p.HP);
flag = 1;
} else {
//触发暴击
int sum = a.nextInt(3);
if(sum!=2){
int b = a.nextInt(4);
if(b!=0 && b!=1){
this.setAttack(this.attack*b);
System.out.println("渣渣灰打出"+ b+"倍暴击造成");
}
}
//攻击力-防御力
int harm =p.attack - this.defense;
this.setHP(this.HP - harm);
//掉血
System.out.println(this.NPC + "掉血" + harm);
//剩余血量
System.out.println("成龙剩余血量"+this.HP);
flag = 0;
}
//判别血量
if (this.HP <= 0) {
System.out.println(this.NPC + "被ko");
System.out.println("战斗结束");
int sum = a.nextInt(3);
if(sum==2){
System.out.println("恭喜渣渣灰获得倚天宝剑");
}
break;//被ko结束战斗
}
if (p.HP <= 0) {
System.out.println(p.NPC + "被ko");
System.out.println("战斗结束");
int sum = a.nextInt(3);
if(sum==2){
System.out.println("恭喜成龙获得屠龙宝刀");
}
break;
}
}
}
}