玩家类
package cn.hp.jwq3;
/*
玩家类
属性:名字,类型,血量,防御,攻击
行为:自我介绍 pk
*/
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);
}
public void pk(Player p){
int flag = 0;
while (true){
this.say();;
p.say();
if (flag == 0){
int harm = this.attack-p.defense;
int sj= (int) Math.round(Math.random()*(2-1)+1);
if (sj == 2){
System.out.println(p.name+"被暴击了--掉血"+harm*2);
}else {
System.out.println(p.name+"掉血"+ harm);
}
p.setLife(p.life-harm*sj);
System.out.println(p.name+"掉血"+harm);
flag = 1;
}else {
int harm = p.attack - this.defense;
int sj= (int) Math.round(Math.random()*(10-1)+1);
if (sj == 3||sj==9){
System.out.println(this.name+"被暴击了--掉血"+harm*2);
this.setLife(this.life-harm*2);
}else {
System.out.println(this.name + "掉血" + harm);
this.setLife(this.life - harm);
}
flag = 0;
}
if (this.life<=0){
System.out.println(this.name+"被ko了");
this.explosiveEquipment();
break;
}
if (p.life<=0){
System.out.println(p.name+"被ko了");
this.explosiveEquipment();
break;
}
try {
Thread.sleep(2000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
public void explosiveEquipment(){
String[]arr={"屠龙宝刀","方天画戟","五雷轰顶技能书","灭世套装","大还丹","10W金币"};
int sj = (int) Math.round(Math.random()*(arr.length-0)+0);
System.out.println("爆了:"+arr[sj]+"!!!");
}
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.jwq3;
public class PlayerTest {
public static void main(String[] args) {
//创建玩家
Player p1 = new Player();
p1.setName("龙傲天");
p1.setType("战士");
p1.setLife(100);
p1.setDefense(20);
p1.setAttack(30);
Player p2 = new Player();
p2.setName("赵日天");
p2.setType("法师");
p2.setLife(50);
p2.setDefense(20);
p2.setAttack(70);
p1.pk(p2);
}
}