Player
package co.hp.wanjialei;
import java.util.Random;
/*
* 玩家类
* 属性:名字,类型,生命值,防御力,攻击力
* 方法:自我介绍,pk
* */
public class Player {
//封装;把属性设为private,提供公共的get和set方法间接访问,提高安全性
private String name;
private String type;
private int hp;
private int fy;
private int gjl;
/*
* 描述自己的信息
* */
public void say(){
System.out.println("我是"+name+"一个"+type+"生命值"+hp+"防御力"+fy+"攻击力"+gjl);
}
/*
* pk
* 我方开启的战斗
* @param P 敌对玩家
* */
public void pk(Player p){
//定义一个标记,0我方攻击1敌方攻击
int flag = 0;
//回合制,直到一方死
while (true){
//显示当前战斗人员信息
this.say();
p.say();
Random a = new Random();
int bj = a.nextInt(100);
if(flag==0){
if(bj<=50){
int harm = this.gjl*2-p.fy;//伤害值
System.out.println("我方回合,暴击了"+this.gjl*2);
System.out.println(p.name+"掉血"+harm);
p.hp-=harm;//生命值-伤害值
}else{
int harm = this.gjl-p.fy;//伤害值
System.out.println(p.name+"掉血"+harm);
p.hp-=harm;//生命值-伤害值
}
flag =1;
}else {
if (bj<=50){
int harm = p.gjl*2-this.fy;//伤害值,掉血量
System.out.println("敌方回合,暴击了"+this.gjl*2);
System.out.println(this.name+"掉血"+harm);
this.hp-=harm;
}else{
int harm = p.gjl-this.fy;//伤害值,掉血量
System.out.println(this.name+"掉血"+harm);
this.hp-=hp;
}
flag = 0;
}
Random b = new Random();
int dl = b.nextInt(10);
//有血量<=0,战斗结束
if(this.hp<=0){
System.out.println(p.name+"战胜"+this.name);
if (dl == 0){
System.out.println("无掉落");
}
if (dl == 1){
System.out.println("皮肤碎片*2");
}
if (dl == 2){
System.out.println("英雄碎片*2");
}
if (dl == 3){
System.out.println("钻石*300");
}
if (dl == 4){
System.out.println("铭文碎片*200");
}
if (dl == 5){
System.out.println("铭文碎片*400");
}
if (dl == 6){
System.out.println("铭文碎片*250");
}
if (dl == 7){
System.out.println("铭文碎片*300");
}
if (dl == 8){
System.out.println("铭文碎片*350");
}
if (dl == 9){
System.out.println("荣耀积分*60");
}
if (dl == 10){
System.out.println("荣耀积分*40");
}
break;//结束
}
if(p.hp<=0) {
System.out.println(this.name+"战胜"+p.name);
if (dl == 0){
System.out.println("无掉落");
}
if (dl == 1){
System.out.println("皮肤碎片*2");
}
if (dl == 2){
System.out.println("英雄碎片*2");
}
if (dl == 3){
System.out.println("钻石*300");
}
if (dl == 4){
System.out.println("铭文碎片*200");
}
if (dl == 5){
System.out.println("铭文碎片*400");
}
if (dl == 6){
System.out.println("铭文碎片*250");
}
if (dl == 7){
System.out.println("铭文碎片*300");
}
if (dl == 8){
System.out.println("铭文碎片*350");
}
if (dl == 9){
System.out.println("荣耀积分*60");
}
if (dl == 10){
System.out.println("荣耀积分*40");
}
break;//结束
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//构造器(建议如果定义了有参的构造器,一定定义一个无参构造器)
public Player(String name, String type, int hp, int fy, int gjl) {
this.name = name;
this.type = type;
this.hp = hp;
this.fy = fy;
this.gjl = gjl;
}
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 getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getFy() {
return fy;
}
public void setFy(int fy) {
this.fy = fy;
}
public int getGjl() {
return gjl;
}
public void setGjl(int gjl) {
this.gjl = gjl;
}
}
PlayerTest
package co.hp.wanjialei;
/**
* 测试玩家类
* 船舰玩家对象PK
*/
public class PlayerTest {
public static void main(String[] args) {
Player p1= new Player("1号", "ll", 60000, 1600, 2200);
Player p2= new Player("2号", "hh", 60000, 1460, 2500);
p1.pk(p2);
}
}