Java实现简单非图形化小游戏 (控制台输出)

public class Demo03Class {

    private String name;
    private String sex;
    private String type;
    private int life;
    private int defense;
    private int attack;
    private double baoji;

    public double getBaoji() {
        return baoji;
    }

    public void setBaoji(double baoji) {
        this.baoji = baoji;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    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 void say(){
        System.out.println("我叫"+name+"是一个"+type+"生命值高达"+life+"防御"+defense+"攻击力"+attack+"性别为"+sex);
    }

    public void pk(Demo03Class p){
        int flag = 1;//默认我方先进攻
        //回合制pk,直到一方死亡
        while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //每次都显示剩余的生命值
            this.say();
            p.say();

            //定义一个标记,0表示我方进攻,1表示敌方进攻

            //创造一个随机数
            Random rd = new Random();
            //赋值给变量a 并且设置随机数为0-100
            int a = rd.nextInt(100);

            if(flag==0){
                //获取暴击率
                double b = this.getBaoji();
                //我方进攻: (我方攻击力-敌方防御力)
                int harm = this.attack-p.defense;//得到伤害
                //如果随机的数小于获取的暴击率那么暴击
                if (a <= b*100){
                    double y = harm*1.5;
                    p.setLife(p.life-(int) y);//敌人掉血
                    System.out.println(p.name+"掉血" + y+"暴击!!!!");
                }else {
                    p.setLife(p.life-harm);//敌人掉血
                    System.out.println(p.name+"掉血" + harm);
                }
                flag = 1 ; //改变进攻方
            }else{
                //获取敌人的暴击率
                double b = p.getBaoji();
                //敌方进攻: (敌方攻击力-我方防御力)
                int harm = p.attack-this.defense;//得到伤害
                if (a <= b*100){
                    double y = harm*1.5;
                    this.setLife(this.life-(int) y);//主方掉血
                    System.out.println(this.name+"掉血" + y+"暴击!!!!");
                }else {
                    this.setLife(this.life-harm);//敌人掉血
                    System.out.println(this.name+"掉血" + harm);
                }
                flag = 0 ; //改变进攻方
            }

            //判别血量
            if(this.life<=0){
                System.out.println(this.name+"被KO了");
                //如果随机数大于等于50那么获得装备
                if (a>=50){
                    System.out.println("恭喜获得|倚天屠龙剑|!!");
                }else {
                    System.out.println("没有掉落装备");
                }
                break;//有人倒下,停止战斗
            }
            if(p.life<=0){
                System.out.println(p.name+"被KO了");
                if (a>=50){
                    System.out.println("恭喜获得|倚天屠龙剑|!!");
                }else {
                    System.out.println("没有掉落装备");
                }
                break;//有人倒下,停止战斗
            }

        }
    }

 设置对战玩家参数并运行

public static void main(String[] args) {

    Demo03Class play = new Demo03Class();
    play.setName("自己");
    play.setSex("男");
    play.setLife(800);
    play.setType("战士");
    play.setDefense(60);
    play.setAttack(70);
    play.setBaoji(0.2);

    Demo03Class play2 = new Demo03Class();
    play2.setName("玩家");
    play2.setSex("女");
    play2.setLife(523);
    play2.setType("刺客");
    play2.setDefense(34);
    play2.setAttack(120);
    play.setBaoji(0.5);

    play.pk(play2);

}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值