【无标题】 玩家类pk * 增加暴击 * 增加物品掉落(击杀敌人后随机掉落物品)

package com.alibaba.demo02;

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();
            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.alibaba.demo02;
/**
 * 测试玩家类
 *  创建玩家对象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);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值