package com.hp.demo;
import java.util.Random;
/**
* 玩家类pk
* 增加暴击
* 增加物品掉落(击杀敌人后随机掉落物品)
*/
public class Player {
private String name; //姓名
private String type; //类型
private int life; //生命
private int defense; //防御
private int attack; //攻击力
//介绍自己的属性
public void say(){
System.out.print("姓名:"+name+"类型:"+type+"生命:"+life+"防御:"+defense+"攻击力:"+attack);
}
int flag = 0;
public void pk(Player p) {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Random a = new Random();
// this.say();
// p.say();
if (flag == 0) {
int sum = a.nextInt(3);
if (sum != 2) {
int b = a.nextInt(4);
if (b != 0 && b != 1) {
this.setAttack(this.attack * b);
System.out.println("猫仙打出" + b + "暴击");
}
}
int harm = this.attack - p.defense;
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.setAttack(this.attack * b);
System.out.println("田田打出" + b + "暴击");
}
}
int harm = p.attack - this.defense;
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(p.name + "打败了" + this.name);
System.out.println("战斗结束");
int sum = a.nextInt(3);
if (sum == 2) {
System.out.println("恭喜田田获得宝藏");
}
break;
}
if (p.life <= 0) {
System.out.println(this.name + "打败了" + p.name);
System.out.println("战斗结束");
int sum = a.nextInt(3);
if (sum == 2) {
System.out.println("恭喜猫仙获得水晶");
}
break;
}
}
}
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 int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
}
package com.hp.demo;
/**
* 测试玩家类
* 创建玩家对象pk
*/
public class PlayerTest {
public static void main(String[] args) {
//创建两个玩家对象
Player p1=new Player();
p1.setName("猫仙");
p1.setType("战士");
p1.setLife(400);
p1.setDefense(300);
p1.setAttack(100);
Player p2=new Player();
p2.setName("田田");
p2.setType("战士");
p2.setLife(350);
p2.setDefense(200);
p2.setAttack(300);
//战斗
p1.pk(p2);
}
}