下面的程序是否以最有效的方式编写?我刚开始使用Java脚本,我只是尝试做一些基本的事情,这样我就可以在我学到更多东西的时候进行练习。
我写它的方式,这个程序确实工作(除了它一直想要在它结束后再次运行?)但还有什么更好的方法可以让我做到这一点吗?
var charName = prompt("Hello Lost Stranger, Tell us your name!");
alert("Welcome " + charName + " we have been waiting for someone who can challenge this dragon!");
alert("A rather small but still intimidating dragon appears; it does not look happy.");
var charHP = 3000;
var mobHP = 3000;
var userAttack = "What type of attack will you use?(Choices: Sword, Charge, Spell)";
var diceOne = Math.floor(Math.random() * 100) + 1;
var diceTwo = Math.floor(Math.random() * 100) + 1;
var diceThree = Math.floor(Math.random() * 100) + 1;
function report() {
alert("You have " + charHP + " remaining and the dragon has " + mobHP + " remaining.");
}
function attack() {
function report() {
alert("You have " + charHP + " remaining and the dragon has " + mobHP + " remaining.");
}
var attackType = prompt(userAttack);
function evaluateAttack() {
if (attackType.toLowerCase() === "sword") {
mobHP = mobHP - diceOne - diceTwo - diceThree;
alert("You slash at the dragon three times drawing blood!");
} else if (attackType.toLowerCase() === "charge") {
mobHP = mobHP - (3 * diceOne);
alert("You charge the dragon at full speed drawing blood!");
} else if (attackType.toLowerCase() === "spell") {
mobHP = mobHP - (4 * diceTwo);
alert("You close your eyes and summon the fires of hell on the dragon");
}
}
if (mobHP > 0) {
report();
evaluateAttack();
} else {
alert("The dragon is dead! You have won!");
}
}
do {
attack();
} while (mobHP > 0);发布于 2017-05-10 23:34:44
好的,下面的内容并不一定是提高代码的效率(从执行速度上说),只是几个快速的观察/指针。
发布于 2017-05-18 21:53:40
我建议RPG能够很好地实现面向对象的编程。
下面的初始代码会更长,但是如果你想进一步开发这个游戏,那么OO风格会更好,更灵活。我们能明白为什么接近尾声..。
我们将构建我们的PlayerCharacter(PC)的功能,以保持(或失去)各种攻击方式,并封装命中点等。
function pCharacter(heroName, charHP) {
this.name = heroName;
this.HP = charHP;
// keep the attack options local to the PC: name, number of dice, dice size, attack text
var attackMethods = [];
attackMethods["sword"] = [2, 100, "You slash twice with your sword at the _monster_"];
attackMethods["charge"] = [1, 200, "You charge the _monster_ with all your might"];
attackMethods["spell"] = [3, 100, "You summon the fires of hell against the _monster_"];
// so that we can add attack options later when our PC gets more powerful
this.addAttackMethod = function(name, numDice, diceRange, desc) {
attackMethods[name] = [numDice, diceRange, desc];
};
// to return the current attack choices to the alert boxes
// we may add a new attack option later, or lose a weapon to a monster that can disarm us!!
this.getAttackMethods = function() {
var str = "";
for (var prop in attackMethods) {
if (attackMethods.hasOwnProperty(prop)) {
str += ", " + prop;
}
}
str = str.substring(2, str.length);
return str;
};
/*
keeping the attack as a PC method which takes the monster as a parameter
then we pass damage to the monster to process.
We could also tweak easily so that certain attacks don't damage certain monsters so much
if we decided to pass the attack option back to the sufferDamage method of the monster.
*/
this.attack = function(attackMethod, monster) {
var damage = 0;
for (var i = 1; i <= attackMethods[attackMethod][0]; i++) {
damage += Math.floor(Math.random() * attackMethods[attackMethod][1]) + 1;
}
monster.sufferDamage(damage); //pass damage for monster to process
alert(attackMethods[attackMethod][2].replace("_monster_", monster.type));
};
this.sufferDamage = function(damage) {
this.HP -= damage; // when you're ready to take damage from monsters!
};
}
//this constructor allows us to set up new monsters easily
function monster(monsterType, mobHP) {
this.type = monsterType;
this.HP = mobHP;
this.sufferDamage = function(damage) {
this.HP -= damage;
};
this.attack = function(PC) {
// fill out this function when you are ready to make the monsters attack the Hero
}
}
var charName = prompt("Hello Lost Stranger, Tell us your name!");
var hero = new pCharacter(charName, 1000);
var dragon = new monster("baby dragon", 500);
alert("Welcome " + hero.name + " we have been waiting for someone who can challenge this dragon!");
alert("A rather small but still intimidating dragon appears; it does not look happy.");
function attack(hero, monsterInstance) {
// make attackgeneric function to have a battle round between the hero and a monster
let report = function() {
alert("You have " + hero.HP + " remaining and the " +
monsterInstance.type + " has " + monsterInstance.HP + " remaining.");
};
var userAttack = "What type of attack will you use?(Choices: " +
hero.getAttackMethods() + ")"; //get current attack options
var attackType = prompt(userAttack);
hero.attack(attackType, monsterInstance);
if (monsterInstance.HP > 0) {
report();
} else {
alert("The " + monsterInstance.type + " is dead! You have won!");
}
}
do {
attack(hero, dragon);
} while (dragon.HP > 0);你的程序在这里结束-下面你可以看到它是多么容易继续游戏与新的怪物和添加更强大的武器给英雄。新战斧现在出现在攻击选择列表中。它滚动6*骰子(150)来造成伤害!
hero.addAttackMethod("battleaxe", 6, 150,
"You swing the enchanted Mighty BattleAxe, crashing it into the _monster_!");
alert("You gain the Mighty BattleAxe!");
alert("A massive intimidating dragon lord appears!! It does not look happy.");
// now see how easy to have the hero fight a new monster
var masterDragon = new monster("Dragon Lord", 3000);
do {
attack(hero, masterDragon);
} while (masterDragon.HP > 0);有更好的方法可以使用。您可以将攻击方法保留在“闭包”中,然后怪物可以检查所使用的攻击类型。我没干。
你可以让怪物反击,让PC也有防御反应!
这只是你如何安排事情的开始.
https://codereview.stackexchange.com/questions/163266
复制相似问题