如何在JavaScript中减慢函数或for循环?



我是个编程新手,请对我宽容点。

在我的编程课上,我们需要做一个小游戏,其中一个英雄在不同的回合中与龙战斗。

英雄和龙得到了10条生命。每轮(三回合)后英雄或龙失去生命。

通过掷骰子来选择获胜者。在每一次转身之后,我都想记录下"英雄赢了"。或者"龙不赢"。因为这是一款游戏,所以我想通过放慢主机速度来让游戏变得更有趣。

现在如果我开始游戏,它会在不到一秒的时间内结束。有没有可能放慢速度?

function startQuest() {
console.log("The Quest got started!");
startGame();
}
function restartQuest() {
console.log("Restart got clicked!");
}


// Klasse als Vorlage für Object:
class Fighter {
constructor (name, lifeEnergy) {
this.name = name;

this.lifeEnergy = lifeEnergy;
}
getAttack() {
let randomNumber = Math.floor(Math.random()*5) + 2;  


return randomNumber;

};

};


// __________________________
// Object erstellen mit Hilfe der Vorlage:
// ___________________________
let dragon = new Fighter("Dragon",10)
let hero = new Fighter("Hero",10)

// ___________________________
// Funktion zum Ermitteln des Gewinners des Zugs:
// ___________________________
function getTurnWinner(attackHero, attackDragon) {

if (attackHero > attackDragon) {
return -1;
}

else if (attackHero < attackDragon) {
return 1;
}
else {

return 0;
}
};
// _______________________________
// Arrays zum Speichern der Punkte:
// ________________________________
let dragonPointsRoundEnd = [];
let heroPointsRoundEnd = [];
// ______________________________
// Funktion zur Vergabe der Punkte pro Zug:
// _______________________________
function getTurnPoints() {   

// ADD DELAY HERE?? 

let turnWinner = getTurnWinner(hero.getAttack(), dragon.getAttack());
if (turnWinner === -1) {
console.log("The Hero won");
heroPointsRoundEnd.push(3);
dragonPointsRoundEnd.push(0);
}
else if (turnWinner === 1) {
console.log("The Dragon won")
heroPointsRoundEnd.push(0);
dragonPointsRoundEnd.push(3);
}
else {
console.log("Nobody won")
heroPointsRoundEnd.push(1);
dragonPointsRoundEnd.push(1);
}

};
// _____________________________
// Funktion speichert die Punkte pro Zug in die Arrays oben:
// _____________________________

function getRoundWinner() {
for (let i = 0; i < 3; i++) {
// ADD DELAY HERE??

getTurnPoints();
}
};
// ____________________________
// In den folgenden Variablen werden die errechneten Gesamtpunkte der Runde gespeichert:
// ____________________________

let dragonCalculatedPoints = "";
let heroCalculatedPoints = "";
// ___________________________
// in dieser Funktion werden die Punkte errechnet welche in den Variablen oberhalb gespeichert werden:
// ___________________________
function calculateRoundPoints() {
getRoundWinner();
dragonCalculatedPoints = dragonPointsRoundEnd[0] + dragonPointsRoundEnd[1] + dragonPointsRoundEnd[2];
heroCalculatedPoints = heroPointsRoundEnd[0] + heroPointsRoundEnd[1] + heroPointsRoundEnd[2];
};
function calculateLifeRoundEnd() {
calculateRoundPoints();
if (dragonCalculatedPoints > heroCalculatedPoints && dragonCalculatedPoints >= 7) {
console.log("The Dragon won the round");
hero.lifeEnergy -= 3;
}
else if (dragonCalculatedPoints > heroCalculatedPoints) {
console.log("The Dragon won the round");
hero.lifeEnergy -= 1;
}
else if (heroCalculatedPoints > dragonCalculatedPoints && heroCalculatedPoints >= 7) {
console.log("The Hero won the round")
dragon.lifeEnergy -= 3;
}
else if (heroCalculatedPoints > dragonCalculatedPoints) {
console.log("The Hero won the round")

dragon.lifeEnergy -= 1;
}
else {
console.log("Nobody won the round");
}
};

// console.log(dragon.lifeEnergy);
// console.log(hero.lifeEnergy);

function startGame() {

while (dragon.lifeEnergy >= 1 && hero.lifeEnergy >= 1) {
calculateLifeRoundEnd();
dragonPointsRoundEnd = [];
heroPointsRoundEnd = [];
if (dragon.lifeEnergy <= 0) {
console.log("The Hero won the battle with " + hero.lifeEnergy + " to 0");

break;

}
else if (hero.lifeEnergy <= 0) {
console.log("The Dragon won the battle with " + dragon.lifeEnergy + " to 0");
break;

}

};
};

我尝试了setTimeout(getTurnPoints,2000),但是回合没有结束(无尽循环)。

我还尝试用:

减慢getRoundWinner函数的循环速度
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

async function getRoundWinner() {
for (let i = 0; i < 3; i++) {

await this.timeout(2000);
getTurnPoints();
}
};

这个也不起作用。(日志'Nobody won').

我想要登录到控制台的内容:

英雄赢了!(停顿2秒)龙赢了!(停顿2秒)英雄赢了!(停顿2秒)英雄赢了一局!…

这是您想要的工作版本。你必须在使用async代码的函数中使用async await

function startQuest() {
console.log("The Quest got started!");
startGame();
}
function restartQuest() {
console.log("Restart got clicked!");
}


// Klasse als Vorlage für Object:
class Fighter {
constructor (name, lifeEnergy) {
this.name = name;

this.lifeEnergy = lifeEnergy;
}
getAttack() {
let randomNumber = Math.floor(Math.random()*5) + 2;  


return randomNumber;

};

};


// __________________________
// Object erstellen mit Hilfe der Vorlage:
// ___________________________
let dragon = new Fighter("Dragon",10)
let hero = new Fighter("Hero",10)

// ___________________________
// Funktion zum Ermitteln des Gewinners des Zugs:
// ___________________________
function getTurnWinner(attackHero, attackDragon) {

if (attackHero > attackDragon) {
return -1;
}

else if (attackHero < attackDragon) {
return 1;
}
else {

return 0;
}
};
// _______________________________
// Arrays zum Speichern der Punkte:
// ________________________________
let dragonPointsRoundEnd = [];
let heroPointsRoundEnd = [];
// ______________________________
// Funktion zur Vergabe der Punkte pro Zug:
// _______________________________
function getTurnPoints() {   

// ADD DELAY HERE?? 

let turnWinner = getTurnWinner(hero.getAttack(), dragon.getAttack());
if (turnWinner === -1) {
console.log("The Hero won");
heroPointsRoundEnd.push(3);
dragonPointsRoundEnd.push(0);
}
else if (turnWinner === 1) {
console.log("The Dragon won")
heroPointsRoundEnd.push(0);
dragonPointsRoundEnd.push(3);
}
else {
console.log("Nobody won")
heroPointsRoundEnd.push(1);
dragonPointsRoundEnd.push(1);
}

};
// _____________________________
// Funktion speichert die Punkte pro Zug in die Arrays oben:
// _____________________________

async function getRoundWinner() {
for (let i = 0; i < 3; i++) {
// ADD DELAY HERE??
await timeout(2000);
getTurnPoints();
}
};
// ____________________________
// In den folgenden Variablen werden die errechneten Gesamtpunkte der Runde gespeichert:
// ____________________________

let dragonCalculatedPoints = "";
let heroCalculatedPoints = "";
// ___________________________
// in dieser Funktion werden die Punkte errechnet welche in den Variablen oberhalb gespeichert werden:
// ___________________________
async function calculateRoundPoints() {
await getRoundWinner();
dragonCalculatedPoints = dragonPointsRoundEnd[0] + dragonPointsRoundEnd[1] + dragonPointsRoundEnd[2];
heroCalculatedPoints = heroPointsRoundEnd[0] + heroPointsRoundEnd[1] + heroPointsRoundEnd[2];
};
async function calculateLifeRoundEnd() {
await calculateRoundPoints();
if (dragonCalculatedPoints > heroCalculatedPoints && dragonCalculatedPoints >= 7) {
console.log("The Dragon won the round");
hero.lifeEnergy -= 3;
}
else if (dragonCalculatedPoints > heroCalculatedPoints) {
console.log("The Dragon won the round");
hero.lifeEnergy -= 1;
}
else if (heroCalculatedPoints > dragonCalculatedPoints && heroCalculatedPoints >= 7) {
console.log("The Hero won the round")
dragon.lifeEnergy -= 3;
}
else if (heroCalculatedPoints > dragonCalculatedPoints) {
console.log("The Hero won the round")

dragon.lifeEnergy -= 1;
}
else {
console.log("Nobody won the round");
}
};

// console.log(dragon.lifeEnergy);
// console.log(hero.lifeEnergy);

async function startGame() {

while (dragon.lifeEnergy >= 1 && hero.lifeEnergy >= 1) {
await calculateLifeRoundEnd();
dragonPointsRoundEnd = [];
heroPointsRoundEnd = [];
if (dragon.lifeEnergy <= 0) {
console.log("The Hero won the battle with " + hero.lifeEnergy + " to 0");

break;

}
else if (hero.lifeEnergy <= 0) {
console.log("The Dragon won the battle with " + dragon.lifeEnergy + " to 0");
break;

}

};
};
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
startGame();

相关内容

  • 没有找到相关文章

最新更新