package cn.hp.demo03;
public class Demo01 {
public static void main(String[] args) {
//给人物赋值
Player a = new Player();
a.setname("亚瑟");
a.settype("战士");
a.setlife(400);
a.setfangyu(300);
a.setgongjili(100);
Player b = new Player();
b.setname("王昭君");
b.settype("法师");
b.setlife(350);
b.setfangyu(200);
b.setgongjili(300);
//开始PK
a.pk(b);
}
}
package cn.hp.demo03;
public class Player {
private String name;//玩家名字
private String type;//玩家类型
private int life;//生命
private int fangyu;//防御
private int gongjili;//攻击力
//自我介绍
public void say(){
System.out.println("名字"+name+"类型"+type+"生命值:"+life+"防御:"+fangyu+"攻击力:"+gongjili);
}
//默认首先进攻方
int flag = 0;
//回合制PK直到一方阵亡
public void pk(Player 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.setGongjili(this.gongjili*b);
System.out.println("亚瑟打出"+ b+"倍暴击造成");
}
}
//攻击力-防御力
int harm =this.gongjili - p.fangyu;
p.setlife(p.life - harm);
//掉血
System.out.println(p.name + "掉血" + harm);
//剩余血量
System.out.println("王昭君剩余血量"+p.life);
flag = 1;
} else {
//触发暴击
int sum = a.nextInt(3);
if(sum!=2){
int b = a.nextInt(4);
if(b!=0 && b!=1){
this.setGongjili(this.gongjili*b);
System.out.println("王昭君打出"+ b+"倍暴击造成");
}
}
//攻击力-防御力
int harm =p.gongjili - this.fangyu;
this.setlife(this.life - harm);
//掉血
System.out.println(this.name + "掉血" + harm);
//剩余血量
System.out.println("亚瑟剩余血量"+this.life);
flag = 0;
}
//判别血量
if (this.life <= 0) {
System.out.println(this.name + "被KO");
System.out.println("战斗结束");
int sum = a.nextInt(3);
if(sum==2){
System.out.println("恭喜王昭君获得水晶石");
}
break;//被ko结束战斗
}
if (p.life <= 0) {
System.out.println(p.name + "被ko");
System.out.println("战斗结束");
int sum = a.nextInt(3);
if(sum==2){
System.out.println("恭喜亚瑟获得水晶石");
}
break;
}
}
}
//get和set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getFangyu() {
return fangyu;
}
public void setFangyu(int fangyu) {
this.fangyu = fangyu;
}
public int getGongjili() {
return gongjili;
}
public void setGongjili(int gongjili) {
this.gongjili = gongjili;
}
}