我是編程新手,所以請放輕松。
對于我的編程課,我們需要做一個小游戲,英雄在不同的回合中與龍戰斗。
英雄和龍獲得了 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();
}
};
這也沒有用。(記錄“沒有人贏”)。
我想要記錄到控制臺的內容:
英雄贏了!(停頓 2 秒)龍贏了!(停頓 2 秒)英雄贏了!(停頓 2 秒)英雄贏得了這一回合!.....
uj5u.com熱心網友回復:
這是您想要的作業版本。您必須在使用異步代碼的函式中使用異步等待
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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/536584.html
標籤:for循环减速
下一篇:如何使用回圈將多個串列寫入資料框
