奥特曼打怪兽

<script>

        var hero = new Fight('凹凸曼', 400, 300, 900, 80);

        hero.output();

        var monster = new Fight('怪兽', 450, 240, 1500, 20);

        monster.output();

        console.log('-----------------');

        function Fight(name, att, def, hp, crit) {

            // 名字,攻,防,血,暴击率

            this.name = name;

            this.att = att;

            this.def = def;

            this.hp = hp;

            this.crit = crit;

            // 双方选手介绍

            this.output = function () {

                console.log(`${this.name}\t攻击力:${this.att} 防御力:${this.def} 血量:${this.hp} 暴击率:${this.crit}`);

            }

            // 双方选手入场

            this.fighting = function (other) {

                // 伤害

                var damage = this.att - other.def;

                // 是否暴击

                var ccrit = false;

                // 总概率

                var critAll = Math.round(Math.random() * 100);

                if (this.crit >= critAll) {

                    damage = damage * 2;

                    ccrit = true;

                }

                // 伤害最小为1

                if (this.att <= other.def) {

                    damage = 1;

                }

                // 被打的人的血量

                other.hp = other.hp - damage;

                // 最少0血

                if (other.hp < 0) {

                    other.hp = 0;

                }

                // 战斗过程

                console.log(`${ccrit ? '暴击' : '\t'}\t${this.name}攻击了${other.name},造成<${damage}>点伤害,${other.name}剩余<${other.hp}>滴血`);

                // 一方为0结束战斗

                return other.hp == 0

            }

        }

        // 大战300回合(滑稽.jpg)

        for (var i = 0; i < 300; i++) {

            if (hero.fighting(monster)) {

                break

            }

            if (monster.fighting(hero)) {

                break

            }

        }

        console.log('-----------------');

        // 输出战斗结果

        hero.output();

        monster.output();

    </script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值