gpt4 book ai didi

javascript - 有什么简单的方法可以写这个吗?

转载 作者:行者123 更新时间:2023-12-02 16:07:51 24 4
gpt4 key购买 nike

我仍在开发我的文本 Angular 色扮演游戏,但我没有任何简单的方法来编写我的升级代码...这是我得到的:

var LEVEL = 1;
var XP = 0;
var ATTACK = 0;
var STRENGTH = 0;
var DEFENSE = 0;
var RANGED = 0;
var MAGIC = 0;
var AGILITY= 0;
var HEALTH = 0;

if(LEVEL = 1 && XP >= 20) {
LEVEL = 2;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 2 && XP >= 60) {
LEVEL = 3;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 3 && XP >= 120) {
LEVEL = 4;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 4 && XP >= 240) {
LEVEL = 5;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 5 && XP >= 480) {
LEVEL = 6;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 6 && XP >= 960) {
LEVEL = 7;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 7 && XP >= 1920) {
LEVEL = 8;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 8 && XP >= 3840) {
LEVEL = 9;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 9 && XP >= 7680) {
LEVEL = 10;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 10 && XP >= 15360) {
LEVEL = 11;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 11 && XP >= 30720) {
LEVEL = 12;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 12 && XP >= 61440) {
LEVEL = 13;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 13 && XP >= 122880) {
LEVEL = 14;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 14 && XP >= 245760) {
LEVEL = 15;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 15 && XP >= 491520) {
LEVEL = 16;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 16 && XP >= 983040) {
LEVEL = 17;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 17 && XP >= 1966080) {
LEVEL = 18;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 18 && XP >= 3932160) {
LEVEL = 19;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

} else if(LEVEL = 19 && XP >= 7864320) {
LEVEL = 20;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;

}

我不知道是否可以使用 for() 或 while() 循环,但如果有人可以帮助我,那将节省大量空间和时间! (顺便说一句,我每个级别所需的经验值都是双倍的)

最佳答案

您可以使用带有 levels 参数的单个函数来存储用于增加级别的 XP 值,例如

var incrementLevels = (function (){
var levels = {1:20,2:60,3:120,4:240}; // and so on…
return function() {
if (XP >= levels[LEVEL]) {
LEVEL++;
ATTACK++;
STRENGTH++;
DEFENCE++;
RANGED++;
MAGIC++;
AGILITY++;
}
}
}());

我同意 elclanrs 的观​​点,即变量最好使用小写字母。

编辑

上面创建了一个名为 incrementLevels 的函数,其调用方式如下:

incrementLevels();

它使用您分配的全局变量。最好将它们放在一个对象中,例如称为权力(或能力或其他):

var powers = {xp: 0, level: 1, attack: 0, strength: 0, 
defence: 0, ranged: 0, magic: 0, agility: 0};

然后,不要将值设置为全局变量,而是设置对象的属性值并根据需要将其传递给incrementLevels 函数。那么它可能是:

var incrementLevels = (function() {
var levels = {1:20, 2:60, 3:120, 4:240 }; // and so on…
var powerList = ['level','attack','strength','defence','ranged','magic','agility'];

return function(powers) {
if (powers.xp >= levels[powers.level]) {
powerList.forEach(function (p){powers[p]++});
}
}
}());

并称其为:

incrementLevels(powers);

您可以拥有多个与其他对象关联的powers对象,例如powersApowersB

请注意,包裹函数的方括号 () 并不是绝对必要的,但在您深入了解并查看之前,这对于指示该函数是立即调用的函数表达式 (IIFE) 很重要最后的()。另请参阅Why use (function(){})() or !function(){}()? .

一些例子:

powers.xp = 30;
incrementLevels(powers);
console.log(JSON.stringify(powers));
// {"xp":30,"level":2,"attack":1,"strength":1,"defence":1,"ranged":1,"magic":1,"agility":1}

powers.xp = 128;
incrementLevels(powers);
console.log(JSON.stringify(powers));
// {"xp":128,"level":3,"attack":2,"strength":2,"defence":2,"ranged":2,"magic":2,"agility":2}

关于javascript - 有什么简单的方法可以写这个吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30551703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com