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