玩家类PK

package cn.hp;
/**
 * 玩家类
 * 属性:名字 类型,生命值,防御值,攻击力
 * 方法:自我介绍
  */
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 + "攻击力" + attack);
    }

    public void pk(Player p) {
        //定义一个标记,0我方攻击,1敌方攻击
        int flag = 0; //默认我方先出击
        //回合制,直到一方死亡为止
        while (true) {
            //显示一下当前战斗人员的信息
            this.say();
            p.say();
            if (flag == 0) {
                //战斗 我方攻击力-敌方防御力=伤害值
                int harm = this.attack - p.defense;
                System.out.println(p.name + "掉血" + harm);
                p.life = harm;//让敌方生命值-伤害值
                flag = 1;//改变标记,转换攻击角色
            } else {
                //战斗 敌方攻击力-我方防御力=伤害值
                int harm = p.attack - this.defense;
                System.out.println(this.name + "掉血" + harm);
                this.life -= harm;//让我方生命值-伤害值

                flag = 0;//改变标记,转换攻击角色
            }
            //有血量<=0,战斗结束
            if (this.life <= 0) {
                System.out.println(p.name + "打败了" + this.name);
                break;
            }
            if (p.life <= 0) {
                System.out.println(this.name + "打败了" + p.name);
                break;
            }
            //线程休眠
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //物品掉落方法 爆装备
    public void explosiveEquipment(){
        String[]arr={"屠龙宝刀","麻痹戒指","倚天剑","还魂丹","10w金币","方天画戟"};
        int x= (int) Math.round(Math.random()*(arr.length-1-0)+0);
        System.out.println("爆了一个["+arr[x]+"]! !");
    }
    //构造器(建议如果定义了有参的构造器,也要写无参)
    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 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 Player(String name, String type, int life, int defense, int attack) {
        this.name = name;
        this.type = type;
        this.life = life;
        this.defense = defense;
        this.attack = attack;
    }

}





package cn.hp;

public class PlayerTest {
    public static void main(String[] args) {
        /**
         * 测试玩家类
         * 创建玩家对象pk*/
        Player p1=new  Player("龙傲天","战士",600,200,300);
        Player p2=new  Player("亚瑟","战士",500,200,300);
        p1.pk(p2);
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值