package co.hp.object;
/**
* 玩家类
* 属性:名字、类型、生命值、防御类、攻击类
* 方法:自我介绍、PK
*/
public class Plays {
//封装:把属性设为私有private,提供get set方法间接访问,提高安全性
private String name;//名字
private String type;//类型
private int life;//生命值
private int fy;//防御值
private int gj;//攻击力
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 getFy() {
return fy;
}
public void setFy(int fy) {
this.fy = fy;
}
public int getGj() {
return gj;
}
public void setGj(int gj) {
this.gj = gj;
}
public Plays() {
}
public Plays(String name, String type, int life, int fy, int gj) {
this.name = name;
this.type = type;
this.life = life;
this.fy = fy;
this.gj = gj;
}
/**
* 描述自己的属性
*/
public void say(){
System.out.println("我叫"+name+",是一个"+type+",生命值高达"+life+",防御值"+fy);
}
/**
* 我方开启战斗
* @param p 敌方玩家
*/
public void pk(Plays p){
int flag = 0;//默认我方先攻击
while (true){
//显示一下当前战斗人员信息
this.say();
p.say();
//生成一个1~10的随机数
//(数据类型)(最小值+Math.random()*(最大值-最小值+1))
int bj = (int) (1+Math.random()*(10-1+1));
//定义一个标记,0我方攻击,1敌方攻击
if(flag==0){
if(bj<=3){
int harm = this.gj*2-p.fy;//伤害值
System.out.println("我方回合,暴击了"+this.gj*2);
System.out.println(p.name+"掉血"+harm);
p.life-=harm;//生命值-伤害值
}else{
int harm = this.gj-p.fy;//伤害值
System.out.println(p.name+"掉血"+harm);
p.life-=harm;//生命值-伤害值
}
flag =1;
}else {
if (bj<=5){
int harm = p.gj*2-this.fy;//伤害值,掉血量
System.out.println("敌方回合,暴击了"+this.gj*2);
System.out.println(this.name+"掉血"+harm);
this.life-=harm;
}else{
int harm = p.gj-this.fy;//伤害值,掉血量
System.out.println(this.name+"掉血"+harm);
this.life-=harm;
}
flag = 0;
}
int dl = (int)(1+Math.random()*(2-1+1));
if (this.life<=0){
System.out.println(p.name+"胜利了");
if(dl == 1){
System.out.println("掉落了一个新的英雄:周英雄");
}
if(dl==2){
System.out.println("掉落了一个英雄碎片");
}
break;
}
if (p.life<=0){
System.out.println(this.name+"胜利了");
if(dl == 1){
System.out.println("掉落了一个新的英雄:周英雄");
}
if(dl==2){
System.out.println("掉落了一个英雄碎片");
}
break;
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package co.hp.object;
public class PlayEntity {
public static void main(String[] args) {
Plays p1 = new Plays("小白兔","法师",100 ,20 ,30);
Plays p2 = new Plays("大灰狼","战士",70 ,12 ,45);
p2.pk(p1);
}
}