package cn.hp.dome3;
import java.util.Random;
/*
玩家类
属性:名字,类型,生命值,防御值,攻击力,暴击
方法:自我介绍,pk
* * */
public class Player {
//封装:把属性设为private,提供公共的get和set方法间接访问,提高安全性
private String name;
private String type;
private int life;
private int defense;
private int attack;
/**
* 描述自己的属性
*/
public void say(){
System.out.print(name+","+type);
System.out.println(",生命值:"+life+",防御力"+defense+",攻击力"+attack);
}
/**
*我方开启的战斗
* @paran p 敌对玩家
*/
public void pk(Player p){
//定义一个标记 0我方攻击 1敌方攻击
int flag=0;//默认我方攻击
String [] fall={"剑","布甲","头盔","鞋子","裤子","手套","500金币"};
int [] baoji={1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2};
Random random=new Random();
StringBuffer sb=new StringBuffer();
while(true){
//显示一下当前战斗人员的信息
this.say();
p.say();
if(flag == 0) {
int baoj1=baoji[random.nextInt(baoji.length)];
if (baoj1 >1) {
System.out.println(p.name+"暴击了!!");
}
//战斗 我方攻击力-敌方防御力=伤害值
int harm=p.attack*baoj1 - this.defense;
System.out.println(this.name+"掉血"+harm);
this.life-=harm; //让敌方生命值-伤害值
flag=1; // 改变标记,转换攻击角色
}else {
int baoj2=baoji[random.nextInt(baoji.length)];
if (baoj2 >1) {
System.out.println(this.name+"暴击了!!");
}
//战斗 敌方攻击力-我方防御力=伤害值
int harm=this.attack*baoj2 - p.defense;
System.out.println(p.name + "掉血" + harm);
p.life -= harm; //让我方生命值-伤害值
flag=0; // 改变标记,转换攻击角色
}
if (this.life <= 0) {
System.out.println(p.name+"打败了"+this.name);
System.out.println("爆出装备:"+fall[random.nextInt(fall.length)]);
break;
}
if (p.life <= 0) {
System.out.println(this.name+"打败了"+p.name);
System.out.println("爆出装备:"+fall[random.nextInt(fall.length)]);
break;
}
//线程休眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
*构造器(建议如果定义了有参的构造器,一定定义一个无参构造器)
*/
public Player() {
}
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;
}
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;
}
}
package cn.hp.dome3;
/**
* 测试玩家类
* 创建玩家对象pk
*/
public class PlayerTest {
public static void main(String[] args) {
//创建两个对象
Player p1 = new Player("亚索", "战士", 500, 200, 300);
Player p2 = new Player("小法", "法师", 600, 100, 200);
//
p1.pk(p2);
}
}