package cn.hp;
/**
* 玩家类
* 属性:名字 类型,生命值,防御值,攻击力
* 方法:自我介绍
*/
public class Player {
private String name;
private String type;
private int life;
private int defense;
private int attack;
/**
* 概述自己的属性
*/
public void say() {
System.out.println("我叫" + name + ",是一个" + type + "生命值" + life + "防御值" + defense + "攻击力" + attack);
}
public void pk(Player p) {
//定义一个标记,0我方攻击,1敌方攻击
int flag = 0; //默认我方先出击
//回合制,直到一方死亡为止
while (true) {
//显示一下当前战斗人员的信息
this.say();
p.say();
if (flag == 0) {
//战斗 我方攻击力-敌方防御力=伤害值
int harm = this.attack - p.defense;
System.out.println(p.name + "掉血" + harm);
p.life = harm;//让敌方生命值-伤害值
flag = 1;//改变标记,转换攻击角色
} else {
//战斗 敌方攻击力-我方防御力=伤害值
int harm = p.attack - this.defense;
System.out.println(this.name + "掉血" + harm);
this.life -= harm;//让我方生命值-伤害值
flag = 0;//改变标记,转换攻击角色
}
//有血量<=0,战斗结束
if (this.life <= 0) {
System.out.println(p.name + "打败了" + this.name);
break;
}
if (p.life <= 0) {
System.out.println(this.name + "打败了" + p.name);
break;
}
//线程休眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//物品掉落方法 爆装备
public void explosiveEquipment(){
String[]arr={"屠龙宝刀","麻痹戒指","倚天剑","还魂丹","10w金币","方天画戟"};
int x= (int) Math.round(Math.random()*(arr.length-1-0)+0);
System.out.println("爆了一个["+arr[x]+"]! !");
}
//构造器(建议如果定义了有参的构造器,也要写无参)
public Player() {
}
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 getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public Player(String name, String type, int life, int defense, int attack) {
this.name = name;
this.type = type;
this.life = life;
this.defense = defense;
this.attack = attack;
}
}
package cn.hp;
public class PlayerTest {
public static void main(String[] args) {
/**
* 测试玩家类
* 创建玩家对象pk*/
Player p1=new Player("龙傲天","战士",600,200,300);
Player p2=new Player("亚瑟","战士",500,200,300);
p1.pk(p2);
}
}