【无标题】pk小游戏

 人物赋值属性

package cn.hp8;

public class Demo1 {
    public static void main(String[] args) {
        //给人物赋值
        Demo3 a = new Demo3();
        a.setNPC("成龙");
        a.setType("战士");
        a.setHP(50);
        a.setDefense(25);
        a.setAttack(15);

        Demo3 b = new Demo3();
        b.setNPC("渣渣灰");
        b.setType("法师");
        b.setHP(35);
        b.setDefense(10);
        b.setAttack(35);

        //开始PK
        a.pk(b);
    }
}

 回合制 暴击 神装

package cn.hp8;

import java.util.Random;

public class Demo3 {
    //各种属性
    private String NPC;
    private String type;
    private int HP;
    private int defense;
    private int attack;


    private int crit;

    public int getCrit() {
        return crit;
    }

    public void setCrit(int crit) {
        this.crit = crit;
    }

    public String getNPC() {
        return NPC;
    }

    public void setNPC(String NPC) {
        this.NPC = NPC;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getHP() {
        return HP;
    }

    public void setHP(int HP) {
        this.HP = HP;
    }

    public int getDefense() {
        return defense;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }

    public int getAttack() {
        return attack;
    }

    public int setAttack(int attack) {
        this.attack = attack;
        return attack;
    }
    //自我介绍
    public  void say(){
        System.out.println("人物:"+NPC+"职业"+type+"生命值:"+HP+"防御:"+defense+"攻击力:"+attack);
    }

            //默认首先进攻方
             int flag = 0;
    //回合制PK直到一方阵亡
    public void pk(Demo3 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.setHP(p.HP - harm);
                //掉血
                System.out.println(p.NPC + "掉血" + harm);
                //剩余血量
                System.out.println("渣渣灰剩余血量"+p.HP);
                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.setHP(this.HP - harm);
                //掉血
                System.out.println(this.NPC + "掉血" + harm);
                //剩余血量
                System.out.println("成龙剩余血量"+this.HP);
                flag = 0;
            }


            //判别血量
            if (this.HP <= 0) {
                System.out.println(this.NPC + "被ko");
                System.out.println("战斗结束");
                int sum = a.nextInt(3);
                if(sum==2){
                    System.out.println("恭喜渣渣灰获得倚天宝剑");
                }
                break;//被ko结束战斗
            }
            if (p.HP <= 0) {
                System.out.println(p.NPC + "被ko");
                System.out.println("战斗结束");
                int sum = a.nextInt(3);
                if(sum==2){
                    System.out.println("恭喜成龙获得屠龙宝刀");
                }
                break;
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值