玩家类pk 增加暴击 增加物品掉落(击杀敌人后随机掉落物品)


  玩家类
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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值