我正在开发一个21点游戏。到目前为止,除了这个小细节之外,一切都正常。基本上,开始游戏按钮只应在游戏之前和游戏之后可见。我没有问题,可以将其设置为隐藏,但是当我完成一局游戏时,它不起作用。
function startGame() { startGameButton.style.visibility = "hidden"; if (!player.playerName) { player.playerName = prompt("你叫什么名字?") player.chips = 100 playerEl.textContent = "玩家:" + player.playerName; } if (cards.length < 2) { let bet = prompt('你想下注多少(筹码:' + player.chips + ')') player.chips = player.chips - bet; chipsEl.textContent = "筹码:" + player.chips; } let firstCard = randomCard(); let secondCard = randomCard(); cards = [firstCard, secondCard]; let tableFirstCard = randomCard(); let tableSecondCard = randomCard(); tableCards = [tableFirstCard, tableSecondCard]; let message = ""; let hasBlackJack = false; let isAlive = true; sum = firstCard + secondCard; newCardButton.style.visibility = "visible"; startGameButton.textContent = "新游戏"; if (sum < 21) { message = "你想要抽一张新牌吗?"; } else if (sum === 21) { message = "你得到了21点!"; newCardButton.style.visibility = "hidden"; startGameButton.style.visibility = "visible"; player.chips = player.chips + bet * 2 hasBlackJack = true; } else { startGameButton.style.visibility = "visible"; isAlive = false; message = "运气不好!你已经离开游戏了!"; } messageEl.textContent = message; cardsEl.textContent = "牌:" + firstCard + " " + secondCard; tableCardsEl.textContent = "桌面牌:" + tableSecondCard + " " + tableSecondCard; sumEl.textContent = "总和:" + sum; console.log("21点:" + hasBlackJack); console.log("存活:" + isAlive); }
我甚至尝试交换顺序,看看是否有任何区别,但是发生的一切都是:
if (sum < 21) { message = "你想要抽一张新牌吗?"; } else if (sum === 21) { message = "你得到了21点!"; newCardButton.style.visibility = "hidden"; startGameButton.style.visibility = "visible"; player.chips = player.chips + bet * 2 hasBlackJack = true; } else { startGameButton.style.visibility = "visible"; isAlive = false; message = "运气不好!你已经离开游戏了!"; }
if语句中的所有内容都在发生,除了可见性部分。
如果需要更多上下文,请参考代码链接:https://github.com/pedrosilva410/blackjack-game
我已經在你的Github上查看了整個程式碼,問題在於你的
startGame
函數只在遊戲剛開始時呼叫(這是有道理的,顯然)。在遊戲進行中它什麼都不做。你關於使「開始遊戲」按鈕可見的指示是正確的,只是把它們放錯了位置。也把它們加到你的
drawCard
函數中。