生成随机数值,判断暴击

package co.hp.object;

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

    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 getFy() {
        return fy;
    }

    public void setFy(int fy) {
        this.fy = fy;
    }

    public int getGj() {
        return gj;
    }

    public void setGj(int gj) {
        this.gj = gj;
    }

    public Plays() {
    }

    public Plays(String name, String type, int life, int fy, int gj) {
        this.name = name;
        this.type = type;
        this.life = life;
        this.fy = fy;
        this.gj = gj;
    }

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

    /**
     * 我方开启战斗
     * @param p 敌方玩家
     */
    public void pk(Plays p){
        int flag = 0;//默认我方先攻击
        while (true){
            //显示一下当前战斗人员信息
            this.say();
            p.say();
            //生成一个1~10的随机数
            //(数据类型)(最小值+Math.random()*(最大值-最小值+1))
            int bj = (int) (1+Math.random()*(10-1+1));
            //定义一个标记,0我方攻击,1敌方攻击
            if(flag==0){
                if(bj<=3){
                    int harm = this.gj*2-p.fy;//伤害值
                    System.out.println("我方回合,暴击了"+this.gj*2);
                    System.out.println(p.name+"掉血"+harm);
                    p.life-=harm;//生命值-伤害值
                }else{
                    int harm = this.gj-p.fy;//伤害值
                    System.out.println(p.name+"掉血"+harm);
                    p.life-=harm;//生命值-伤害值
                }
                flag =1;
            }else {
                if (bj<=5){
                    int harm = p.gj*2-this.fy;//伤害值,掉血量
                    System.out.println("敌方回合,暴击了"+this.gj*2);
                    System.out.println(this.name+"掉血"+harm);
                    this.life-=harm;
                }else{
                    int harm = p.gj-this.fy;//伤害值,掉血量
                    System.out.println(this.name+"掉血"+harm);
                    this.life-=harm;
                }

                flag = 0;
            }
            int dl = (int)(1+Math.random()*(2-1+1));
            if (this.life<=0){
                System.out.println(p.name+"胜利了");
                if(dl == 1){
                    System.out.println("掉落了一个新的英雄:周英雄");
                }
                if(dl==2){
                    System.out.println("掉落了一个英雄碎片");
                }
                break;
            }
            if (p.life<=0){
                System.out.println(this.name+"胜利了");
                if(dl == 1){
                    System.out.println("掉落了一个新的英雄:周英雄");
                }
                if(dl==2){
                    System.out.println("掉落了一个英雄碎片");
                }
                break;
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
package co.hp.object;

public class PlayEntity {
    public static void main(String[] args) {
        Plays p1 = new Plays("小白兔","法师",100 ,20 ,30);
        Plays p2 = new Plays("大灰狼","战士",70 ,12 ,45);
        p2.pk(p1);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值