亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Vanilla Javascript - 當玩家達到一定分數時結束游戲

Vanilla Javascript - 當玩家達到一定分數時結束游戲

鳳凰求蠱 2023-04-27 16:40:59
我按照教程制作了剪刀石頭布游戲,現在我想更進一步,在玩家或計算機得分達到 10 分時添加消息/結束游戲。我無法確定出我需要做的事情。  let user = ["Player", "Computer"];  let pScore = 0;  let cScore = 0;  let gameIsOver = false;    // Some other functions in here...  // End Game  if (pScore === 10) {    isGameOver = true;    winner.textContent = `${user[0]} Wins the Game!`;    return;  } else if (cScore === 10) {    isGameOver = true;    winner.textContent = `${user[1]} Wins the Game!`;    return;  } else {    isGameOver = false;  }  // Some other functions here...
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

每次分數變化時,您都需要檢查條件。為此,將您的 if 語句放在一個單獨的函數中,并從有人得分時觸發的函數中調用它。我創建小函數是因為,在我看來,這是最佳實踐,但對于這么小的操作來說并不是必需的。


let pScore = 0;

let cScore = 0;


let user = ["Player", "Computer"];



let isGameOver = (score) => {

  if (pScore === 10 || cScore === 10) {

    return true;

  }

  return false;

}


function gameOver() {

  let winner = pScore === 10 ? user[0] : user[1];

  console.log(winner);

}


function theFunctionThatChangesTheScores() {

  // after the code that changes the score

  if ( isGameOver() ) {

    // you can code in this block, but ideally.

    // create another function and call it:

    return gameOver();

  }

  return console.log("game is still on");

}


theFunctionThatChangesTheScores();

 


查看完整回答
反對 回復 2023-04-27
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號