文字游戏

package cn.hp.dome3;

import java.util.Random;

/*
玩家类
属性:名字,类型,生命值,防御值,攻击力,暴击
方法:自我介绍,pk
* * */
public class Player {
    //封装:把属性设为private,提供公共的get和set方法间接访问,提高安全性
    private String name;
    private String type;
    private int life;
    private int defense;
    private int attack;
    /**
     * 描述自己的属性
     */
    public void say(){
        System.out.print(name+","+type);
        System.out.println(",生命值:"+life+",防御力"+defense+",攻击力"+attack);
    }
    /**
     *我方开启的战斗
     * @paran p 敌对玩家
     */

    public void pk(Player p){
        //定义一个标记 0我方攻击 1敌方攻击
        int flag=0;//默认我方攻击
        String [] fall={"剑","布甲","头盔","鞋子","裤子","手套","500金币"};
        int [] baoji={1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2};
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        while(true){
            //显示一下当前战斗人员的信息
            this.say();
            p.say();
            if(flag == 0) {
                int baoj1=baoji[random.nextInt(baoji.length)];
                if (baoj1 >1) {
                    System.out.println(p.name+"暴击了!!");
                }
                //战斗 我方攻击力-敌方防御力=伤害值
                int harm=p.attack*baoj1 - this.defense;
                System.out.println(this.name+"掉血"+harm);
                this.life-=harm; //让敌方生命值-伤害值
                flag=1; // 改变标记,转换攻击角色
            }else {
                int baoj2=baoji[random.nextInt(baoji.length)];
                if (baoj2 >1) {
                    System.out.println(this.name+"暴击了!!");
                }
                //战斗 敌方攻击力-我方防御力=伤害值
                int harm=this.attack*baoj2 - p.defense;
                System.out.println(p.name + "掉血" + harm);
                p.life -= harm; //让我方生命值-伤害值
                flag=0; // 改变标记,转换攻击角色
            }
            if (this.life <= 0) {
                System.out.println(p.name+"打败了"+this.name);
                System.out.println("爆出装备:"+fall[random.nextInt(fall.length)]);
                break;
            }
            if (p.life <= 0) {
                System.out.println(this.name+"打败了"+p.name);
                System.out.println("爆出装备:"+fall[random.nextInt(fall.length)]);
                break;
            }
            //线程休眠
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     *构造器(建议如果定义了有参的构造器,一定定义一个无参构造器)
     */
    public Player() {
    }

    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;
    }

    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.dome3;

/**
 * 测试玩家类
 * 创建玩家对象pk
 */
public class PlayerTest {
    public static void main(String[] args) {
        //创建两个对象
        Player p1 = new Player("亚索", "战士", 500, 200, 300);
        Player p2 = new Player("小法", "法师", 600, 100, 200);
        //
        p1.pk(p2);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值