龙争虎斗彼岸花

package lei;

import java.util.Random;

/*
* 玩家类
* 属性:名字、类型、生命值、防御值、攻击力
* 方法:自我介绍、pk
* */
public class Player {
    //封装:把属性设为私有,提供公共的get和set方法间接访问,提高安全性
    private String name;
    private String type;
    private  int life;
    private  int defence;
    private  int attack;

    private int crit;
    //随机数
    Random ra =new Random();
    //暴击率
    public int crit(){
        crit = ra.nextInt(100);
        return  crit;
    }
    //暴击参生的概率
    public int jihui(){
        int da = ra.nextInt(100);
        return da;
    }
    //掉装备
    public int diao(){
        int diao = ra.nextInt(100);
        return diao;
    }
    public void haha(){
        int ha = diao();
        if (ha==1){
            System.out.println("恭喜您捡到橙色武器-四十米的大刀-");
        }else if (ha==2){
            System.out.println("恭喜您获得免费武器-冒蓝光的加特林-");
        }else if (ha==3){
            System.out.println("恭喜您得到至尊武器-纹皮皮虾打篮球的方天画戟-");
        }else {
            System.out.println("恭喜您获得回家一趟");
        }
    }

    /*
    * 描述自己的属性
    * */
    public  void  say(){
        System.out.println("我叫"+name+",是一个"+type);
        System.out.println(",生命值高达"+life+",防御值"+defence+",攻击力"+attack+",暴击率"+crit());
    }

    public  void pk(Player p) {
        //定义一个标记,0为我方,1敌方攻击
        int flag = 0;//默认我方先攻击
        //提示当前战斗人员的信息
        this.say();
        p.say();
        while (true) {
            if (flag == 0) {
                //战斗 我方攻击力-敌方防御力=伤害值
                int harm = p.attack - this.defence;
                //暴击
                if (crit >= jihui()) {
                    this.life -= (2 * harm);
                    System.out.println("攻击方:"+p.name + "攻击.暴击" + this.name + "掉血" + (2 * harm) + "," + this.name + "剩余血量为" + this.life);
                    System.out.println();
                }else {
                    this.life -= harm;//让敌方生命值-伤害值
                    System.out.println("攻击方:" + p.name + "攻击," + this.name + "掉血" + harm + "," + this.name + "剩余血量为" + this.life);
                    System.out.println();
                }
                    flag = 1;//改变标记,转换攻击角色
            } else {
                //战斗 敌方攻击力-我方防御力=伤害值
                int harm = this.attack - p.defence;
                if (crit >= jihui()) {
                    this.life -= (2 * harm);
                    System.out.println("攻击方:"+this.name + "攻击.暴击" + p.name + "掉血" + (2 * harm) + "," + p.name + "剩余血量为" + p.life);
                    System.out.println();
                }else {
                    p.life -= harm;//让我方生命值-伤害值
                    System.out.println("攻击方:" + this.name + "攻击," + p.name + "掉血" + harm + "," + p.name + "剩余血量为" + p.life);
                    System.out.println();
                }
                flag = 0;
            }
            //有血量<=0,战斗结束
            if (this.life<=0){
                System.out.println(this.name+"打败了"+p.name);
                haha();
                break;
            }
            if (p.life<=0){
                System.out.println(p.name+"打败了"+this.name);
                haha();
                break;
            }

            //线程休眠
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //构造器:如果定义了一个有参的构造器,一定要构造无参构造器
    public Player(String name, String type, int life, int defence, int attack) {
        this.name = name;
        this.type = type;
        this.life = life;
        this.defence = defence;
        this.attack = attack;
    }

    public Player() {
    }


    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 getDefence() {
        return defence;
    }

    public void setDefence(int defence) {
        this.defence = defence;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值